My project builds with MSBuild 4 but not with MSBu

2019-04-11 21:29发布

问题:

When I build my solution using MSBuild 4 it compiles successfully:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe MySolution.sln

Build succeeded.

0 Warning(s)
0 Error(s)

But when I try to do the same using MSBuild 3.5 I get the following error, even though the source is the same and I am using the same libraries and the same version of the .NET Framework.

C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe MySolution.sln

error CS1501: No overload for method 'InitializeClientContextFromStringSid' takes '2' arguments

error CS1501: No overload for method 'GetRoles' takes '0' arguments

The error that I get is related to Authorization Manager (AzMan).

The method 'InitializeClientContextFromStringSid' in the first error belongs to the public interface IAzApplication, a member of Microsoft.Interop.Security.AzRoles.

The method 'GetRoles' in the second error belongs to the public interface IAzClientContext, also a member of Microsoft.Interop.Security.AzRoles.

I am using the methods in the following way:

var clientContext = _azApplication.InitializeClientContextFromStringSid(member, 0);

where the variable member is a string containing the Windows Active Directory SID from an user and _azApplication is of type IAzApplication.

clientContext.GetRoles()

where clientContext is of type IAzClientContext.

Why does my solution builds with MSBuild 4 but not with MSBuild 3.5 even though I'm targeting the same version of the .NET Framework (3.5)?

回答1:

It looks like InitializeClientContextFromStringSid has an optional parameter in the specification. Though MSBuild in .Net Framework 4.0 supports the use of optional parameters in C# by allowing you to leave them out of the function call, previous versions of the MSBuild do not support this approach. So you must to supply the parameter even if it's not being used when building with older version of the Framework.

HRESULT InitializeClientContextFromStringSid(
  [in]            BSTR SidString,
  [in]            LONG lOptions,
  [in, optional]  VARIANT varReserved,
  [out]           IAzClientContext **ppClientContext
);

The same issue is happening with the GetRolesmethod.

From my understanding, the reason you can build using the 4.0 version of MSBuild and target 3.5 Framework is that the CLR already supported the use of Optional parameters, for example VB.NET has always supported them. However, when using MSBuild 3.5, it is going to use the older rules/specification that did not allow support for Optional parameters in C# and thus will give you build errors.