Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference GAC assemblies in new CSPROJ format?

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

like image 830
Agendum Avatar asked Jan 27 '26 07:01

Agendum


1 Answers

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?

like image 87
Agendum Avatar answered Jan 30 '26 07:01

Agendum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!