How to reference a strongly-named DLL at compile t

2019-08-18 19:34发布

问题:

I have an ASP.NET 4.7.2 Framework project. At runtime, upon loading a view, it says System.Runtime version 4.0.0.0 cannot be found. We have written about this extensively here: ASP.NET: .NET Framework/Standard/Core DLL collisions, caused by Nuget. "You must add a reference to assembly System.Runtime..."

It's been over a week and we still don't have a solution. All development has been halted.

Yesterday we tried side-by-side loading by simply calling on application start up:

Assembly systemRuntime = Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

Upon start up. We don't do anything with systemRuntime. It's only there for us to confirm that indeed 4.0.0.0 was loaded. Unfortunately, that didn't fix anything.

This makes me think that maybe since views are compiled at run-time, maybe somehow they are not referencing the right System.Runtime, or that we need to do the side-by-side loading there.

This problem is so severe we've started porting the site to .NET Core, because no one has any ideas how to solve it. According to analyzer, we'll have to rewrite approximately 15% of the site. We cannot go without deploying to production for potentially weeks, so a temporary solution must be found as soon as physically possible. We already have fixes that need to go to up.

回答1:

Dai, in the first comment, directed me in the right direction: the view engine doesn't necessarily use the same references as the main project.

After some Googling, I found a way to reference assemblies for the view engine. In web.config:

<configuration>
...
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" batch="false">
      <assemblies>
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
    </compilation>
  </system.web>
...
</configuration>

I also followed Dai's recommendation to try precompiling views. This is done by editing the MVC project's .csproj file with:

<MvcBuildViews>true</MvcBuildViews>

Precompiling the views is not necessary, but did prove it was indeed views compiling differently than the main project, and made it a tiny bit faster to test because I didn't have to run the site to see it blow up.