Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

"Could not load System.Runtime.dll" because it was not copied to the output folder #887

Closed
GSPP opened this issue Oct 3, 2018 · 7 comments

Comments

@GSPP
Copy link

GSPP commented Oct 3, 2018

I will attach a repro solution that I have created as follows:

  1. New web application targeting 4.7.1
  2. Add a class library
  3. Reference the library from the web app and call code in it
  4. In the class library add Microsoft Unity and use it
  5. Add the System.Net.Http package to both projects

When this application is started it immediately crashes:

[FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]

[FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
   NuGetUnityRepro.Library.Class1.Run() in ...\NuGetUnityRepro\NuGetUnityRepro.Library\Class1.cs:15
   NuGetUnityRepro.MvcApplication.Application_Start() in ...\NuGetUnityRepro\NuGetUnityRepro\Global.asax.cs:21

[HttpException (0x80004005): Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +476
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +220
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +657
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +188

Copying text from my other comment:

So Unity now wants to use System.Runtime. Unfortunately, it was not copied to the bin directory.
I'm unsure about how to fix this. Deleting the binding redirect in web.config works but I'm hesitant to modify what the NuGet package automatically did.

This seems to be a tooling bug. Should this not just work?

NuGetUnityRepro.zip

@GSPP
Copy link
Author

GSPP commented Oct 9, 2018

@joperezr
Copy link
Member

joperezr commented Oct 9, 2018

I believe that what is going on in this one is that you are not actually running on .NET 4.7.1. System.Runtime.dll comes in the GAC on 4.7.1 so you shouldn't have any problems loading it if you are actually running on that platform. Non-web applications will always ensure that the platform you target is installed on the machine running it, but unfortunately, Web apps do not behave this way.

@GSPP
Copy link
Author

GSPP commented Oct 10, 2018

I was definitely running on 4.7.1 and targeting 4.7.1. I have now upgraded to 4.7.2 and it still reproduces. I just unpacked that archive, restored packages and pressed F5.

Reflector shows System.Runtime in the GAC:

image

@GSPP
Copy link
Author

GSPP commented Oct 10, 2018

One more puzzle piece in dealing with these errors is the "Auto-generate binding redirects" feature:

image

This feature seems to edit the app.config file that is being placed in the bin directory. This feature can override binding redirects made in the normal app.config with wrong ones. This explains why editing app.config does not seem to have any effect.

Currently, it seems that this feature generates redirects to the wrong version (or to the correct version and something else causes that version to not be available).

For web apps this feature does not exist. It comes to bear in console applications for example.

@joperezr
Copy link
Member

joperezr commented Oct 17, 2018

Ok I understand what is going on here. This is the same issue as https://github.com/dotnet/corefx/issues/32757.

The issue here is very specific to web projects that use packages.config. In non-web project types, this error would go away by enabling auto-generate binding redirects or by migrating to PackageReference. Here is more info:

The problem here is that NuGet package manager is adding some binding redirects to your web.config but it doesn't have the full picture of your project references, so it fails to read the Framework list to know that System.Net.Http is part of the framework already. The way to tell NuGet package manager to not do that is to either turn on auto-generate binding redirects or to migrate to PackageReference, but unfortunately, that is not straightforward with web projects. With web projects, enabling auto redirects simply won't work, as web.config behaves very different than app.config. PackageReference will work, but the problem is that it is not trivial to migrate since there is no context menu option from VS to migrate a project to packageReferences for web projects, so you would have to do that work manually.

Honestly, I think there are two bugs here that should be fixed. First, I would log a bug on vs feedback in order to have the project templates use PackageReference by default since packages.config is just a source of problems, especially when consuming libraries that target different frameworks. The other one seems to be a missing feature on web projects since you can't easily migrate references to PackageReference and today you have to do that manually which is a pain. I would probably also log that bug in vs feedback, since they would know how to route this accordingly.

I tried your repro project but migrated (manually) to PackageReference and that fixed the issue at runtime.

I'll close this issue given that it is not a problem with dotnet standard, but external instead: vs template system + vs lack of migration feature for web projects + NuGet package manager (incorrectly) trying to add binding redirects automatically.

@GSPP
Copy link
Author

GSPP commented Oct 19, 2018

Are you saying there will not be a fix besides migrating to PackageReference?

PackageReference is not yet appropriate for all development. Not all packages are supported and other reasons.

I suggest that you use Microsoft internal channels to properly route these issues.

@joperezr
Copy link
Member

Are you saying there will not be a fix besides migrating to PackageReference?

I'm not. I don't know if/when the NuGet Package manager will fix these issues, I was simply putting that the package examples that you pointed out, PackageReference would make the scenario work. I was also stating that if the templates used PackageReference by default, then most user's wouldn't hit these type of issues.

I suggest that you use Microsoft internal channels to properly route these issues.

We have definitely been doing that, and working with partner teams on how to make this whole experience better than its current state.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants