9

I am using Visual Studio 2019 preview 2.1. I have a .NET Framework 4.6.1 class library C# project which has some Azure references in the old csproj project format (ToolsVersion="15.0"). This old csproj currently builds and works just fine. I am attempting to migrate to the new project format. Everything is going well except for the fact that I have one reference which doesn't come from NuGet -- it comes from the GAC: Microsoft.WindowsAzure.ServiceRuntime. However, I haven't figured out how to get the new project format to find the assembly. For example here is the new project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <None Include="app.config" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.KeyVault" Version="2.3.2" />
    <PackageReference Include="Microsoft.Azure.KeyVault.Core" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.KeyVault.WebKey" Version="2.0.7" />
    <PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.19.1" />
    <PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.8" />
    <PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.7" />
    <PackageReference Include="Microsoft.WindowsAzure.ConfigurationManager" Version="3.2.3" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.WindowsAzure.ServiceRuntime, Version=2.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Runtime.Caching" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
</Project>

In fact, that Microsoft.WindowsAzure.ServiceRuntime reference is the exact line from the old format. When loading this in VS it produces the following warning:

Warning MSB3245 Could not resolve this reference. Could not locate the assembly "Microsoft.WindowsAzure.ServiceRuntime, Version=2.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. MyProject C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2114

I attempted to remove the version:

<Reference Include="Microsoft.WindowsAzure.ServiceRuntime" />

But that did not help. If I remove it and try to add the reference from the VS user interface, I see there are a bunch of Microsoft.* and System.* options in 'Framework' (the GAC), but Microsoft.WindowsAzure.ServiceRuntime is not one of them. Just to confirm I started up ILSpy and chose "Open From GAC..." and sure enough it is there along with a bunch of other assemblies which do not appear in the VS user interface.

How can I get VS to load this reference from the GAC?

Thanks

4

2 回答 2

6

So I found this thread on GitHub which indicates <Reference> not discovering assemblies added to the GAC is expected behavior:

This behaviour is by-design. You can opt back in as shown here. This should not be considered a workaround, but rather a deliberate and supported opt-in where you accept the consequences of having potentially different (and even incorrect) build behaviour on different machines.

The solution is to do the following:

<PropertyGroup>
  <AssemblySearchPaths>$(AssemblySearchPaths);{GAC}</AssemblySearchPaths>
</PropertyGroup>

I can confirm this resolves my issue completely even without using a HintPath.

But the natural next question is -- if resolving added assemblies in the GAC will not work by default, why doesn't Microsoft leverage NuGet for the Azure SDK? The only official Azure reference in NuGet is old. Does this mean any Azure project using the new CSPROJ format will need to manually add GAC into AssemblySearchPaths?

于 2019-02-11T20:49:13.380 回答
0

The format of that "<Reference Include="Microsoft.WindowsAzure.ServiceRuntime, Version=2.7.0.0, …" />" is Right. And this issue seems to result from the VS can't find that assembly from right position. Suggestions:

If the assembly exists in the default folder:

  1. Add it manually: In VS2019 preview, the default position of it is"C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\ref" , navigate to the folder to find the assembly. Then go Reference=>add Reference=>Browser to add it manually.
  2. Also, we can remove and add the reference from VS user interface, but the assembly should in 'Extensions' instead of 'Framework'. Because the assembly seems not to locate in GAC, I haven’t found it in GAC. enter image description here

If the assembly doesn’t exist in machine:

1.We can download the assembly by nuget. After that, remove other assemblies we don't need.

2.We can copy the assembly from the PC where old project exists to current machine. And browse to add it manully.

Hope it helps. Any update feel free to share here.

于 2019-02-04T05:52:11.050 回答