ASP.NET Core 2.0 Could not load file or assembly System.ServiceModel - asp.net-core

I am working on porting an "ASP.NET Core Web Application" that was compiling under the .NET Framework 4.6.1 (i.e. full framework) over to compiling against .NET Core 2.0. I have some dependencies which still require the full framework but with .NET 2.0 my understanding is that I can now reference full framework assemblies from within a .NET Core 2.0 compiled application.
When I attempt to run the project, I get the following error:
Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
I looked through all the NuGet packages and projects I'm referencing and none of them reference System.ServiceModel.Web but I'm not convinced that's the same as the System.ServiceModel. When I open the projectname.deps.json file located in the bin folder, I see a reference to System.ServiceModel.Web but no reference to System.ServiceModel under the "Microsoft.NETCore.App/2.0.0" section which contains the following line:
"ref/netcoreapp2.0/System.ServiceModel.Web.dll": {},
I also poked around in the "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0" folder and I see a System.ServiceModel.Web.dll.
I'm not doing any WCF work and as I stated I've looked at all the dependencies of the libraries I'm using and none of them appear to be referencing System.ServiceModel.
Has anyone else run into this issue? I appreciate any and all insights anyone might have.

I solved this problem installing the Nuget package:
Install-Package Microsoft.Windows.Compatibility
I was using ASP.net Core 2.2 but maybe works for 2.0v too

In Asp.Net Core, when the Microsoft WCF Web Service Reference Provider is used under the connected services, code generator automatically includes System.ServiceModel.Duplex, System.ServiceModel.Http, System.ServiceModel.NetTcp, System.ServiceModel.Security'assemblies.
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
In your occasion, System.ServiceModel.Primitives is needed to be added aswell. System.ServiceModel.Primitives assembly includes System.ServiceModel.dll
System.ServiceModel, Version=4.0.0.0 is not used anymore.
please also see: https://github.com/dotnet/standard/issues/575#issuecomment-380479584

Related

ASP.NET Core referencing TPL NuGet package Cannot Find File or Assembly System.Threading

I am making a new ASP.NET Core 2.0 project, essentially a Web API. I have to reference several assemblies that are .NET 3.5 assemblies, so I am running this as a .NET Framework project, just using the .NET Core improvements where possible.
The referenced .NET framework 3.5 assembly references TaskParallelLibrary (1.0.2856). And I think that's where my issues are coming in. It's worth noting the Web API worked on its sample endpoints and all worked as expected.
When I run the application and try and use the referenced library, the call fails and I receive the following exception:
Could not load file or assembly 'System.Threading, Version=1.0.2856.102, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
or one of its dependencies. The system cannot find the file specified.
I have tried several things, including:
Adding an App.configx file to try and force it to target .NET framework 4's System.Threading, on the assumption that that will be available. Inspired by a page I can't find the link for:
Here's the content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- this is only here for binding redirects -->
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Directly adding the TaskTPLLibrary package at that version to the project. So far, it hasn't helped.
Adding the Microsoft.Bcl.Async library to my project. Again, nothing.
Trying to directly import System.Threading from "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Threading.dll". I get an error stating "The reference is invalid or unsupported."
I've tried several other things, including re-cloning, cleaning, re-cleaning, swearing vociferously and coming back at it another day, all to no avail.
My reading list includes (on this computer):
https://github.com/dotnet/corefx/issues/26084
Could not load file or assembly System.Threading.Tasks, Version=2.5.19.0
ASP.NET Core 2.0 Site - FileIOException System.Runtime
And probably others that I can't remember
Some additional information as this issue continues:
Creating a .NET Framework ASP.NET MVC app using .NET Framework 4.6.2 with the same underlying library does work. It does it indirectly using an intermediary .NET Framework 3.5 one (just for convenience as much as anything else).
A .NET Framework 4.6.2 Console App that uses the library directly also runs into this problem. So it seems not to be ASP.NET Core itself. I'll change the tags accordingly.
Making the 4.6.2 console use the intermediary 3.5 library doesn't resolve the issue.
Any help in resolving this issue much appreciated.
As there's no reasonable answer forthcoming, I'm assuming that having a .NET 3.5 Framework library referenced that uses the TaskParallelLibrary will in essence mean that using any .NET above 4 will cause this error. Under unspecified conditions. Most of the time.
The only solution to this seems to be rewrite everything to multi-target .NET 3.5 and .NET Standard and have the Standard implementation use the real TPL and have the 3.5 one use the NuGet package.

Multitargeting .NET Standard 2.0 missing some framework libraries

Had a working ASP.NET Core using a .NET Standard DLL I wrote. I'd like to use TransactionScope in my DLL now, but getting this issue:
.NET Standard and .NET Core 2.0 both recognize System.Transactions, but .NET Framework 4.6.1 is giving an error and won't compile with MSBuild. Visual Studio 2017 offers the following helpful information:
I cannot seem to find a way to force the project to recognize System.Transactions.dll from my system's Framework/Framework64 folders.
Yes, I can use dotnet build to compile the .NET Core version, but that's not what I want... I want to be able to compile for .NET 4.6.1, too.
You need to add a framework referenced conditioned on the target framework.
Add this to your csproj file:
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System.Transactions" />
</ItemGroup>

There was an error running the selected code generator: Could not load file or assembly Microsoft.EntityFrameworkCore, version = 2.0.1.0

When I was creating a Controller and a View by MVC Controller with views, using Entity Framework I got an error.
The Error is:
There was an error running the selected code generator: 'Could not
load file or assembly Microsoft.EntityFrameworkCore, version =
2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' the located assembly's manifest definition does not match the assembly reference
Creating MVC Controller with views, using Entity Framework:
How can I solve this problem?
I use Visual Studio Version 15.5.2 and version of Microsoft.AspNetCore.All is 2.0.0
I updated the Microsoft.AspNetCore.All to version 2.0.3 and now it does work right.
Go to NugetPackage Manager and update the Microsoft.AspNetCore.All package
For me none of the above solutions worked. I had to add Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design packages even though I have Microsoft.AspNetCore.All package added to my project.
Edit project in VS 2017 and add these lines
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.3" />
Update your packages or it can happen when you have an older SDK. Download a new .NET Core SDK and runtime from www.microsoft.com/net
I had a similar issue but mine was something with the versioning of visual studio.
I went to Visual Studio Installer and it notified me that I had to restart my computer
I got this same error:
Running Preview 2019 and .NET Core 3.
I moved the Nuget pkg sources up in my list.
I was attemping to add a new Controller and I would get this error every time.
I also noticed that for some reason I chose not to set up SSL but I had the setting in my launchSettings.json.
I deleted the setting for ssl and built the app and ran it.
After the successful run I could add the Controller without the error.
Follow these Steps:-
From Tools
Select NuGet Package Manager
-> Manage NuGet Packages For Solution
click on ->Updates Select the checkbox Select All
->Select Your Project from right hand side -> Click on Upadate
Having the same error, after downloaded VS2019"PREVIEW" and then opened a core 3.0 project there I was able to scaffold content no errors at all..

Consuming WCF service in Xamarin WIndows Phone project

I have a bit of a problem with my Xamarin.Forms project. I have generated web service proxy class with SlSvcutil.exe and have imported that class in my PCL library that is shared amongs platform specific projects / iOs, Android and WindowsPhone.
All the references from System.ServiceModel are missing because, ofcourse, that reference does not exist in my project. How do I add it? I tried to do it with NuGet but it just fails all the time with same error.
You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
I tried selecting 'Ignore All' for Dependacy Behavior when installing package but to no avail.
This is driving me crazy as I have so much problems with this Xamarin and I havent really started programming yet.
You should not need to install an extra package. Just add System.ServiceModel, System.Runtime.Serialization and System.Xml to your references. If you need to, you can just open your csproj file in a text editor and add these to the same area where you see other references in the file:
<Reference Include="System.ServiceModel" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
And make sure that you are using slsvcutil.exe from the Silverlight 5 SDK, not an older one. Taking a look at a working WCF client in one of our projects, I see this exact tool version:
// This code was auto-generated by SlSvcUtil, version 5.0.61118.0

Compile .net 4.0 project on build server with .net 4.5

We're having the seemingly common error
Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
in a project that needs to be compiled against .Net 4.0 but is built on a build server running Windows Server 2012 (with .Net 4.5). The project is a web application that gets deployed to a web server running 2003, where installing .Net 4.5 isn't an option. There it runs against "classic" .Net 4.0
From similar questions, we're trying command-line options to MSBuild:
/property:FrameworkPathOverride="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
We also tried various combinations of
/property:ReferencePath="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
/property:NoStdLib=true
/property:NoCompilerStandardLib=true
The reference assemblies (including the .dll files) are in fact installed in that place on the build server. But when we deploy the website and visit the home page, we get that error. (Interestingly, on a page reload, the error disappears, and the site operates normally.) What are the MSBuild parameters necessary to compile against the .Net 4.0 assemblies?
Update
I turned on ludicrous-level logging on MSBuild, and I see that apparently it is building against the .Net 4.0 reference assemblies:
Resolved file path is "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll
and I didn't see any mention of assemblies outside that folder or the build server's working directory. So it appears to compile properly, but when deployed on the web server, it throws the exception.
As to the exception going away on a page reload, I wonder if that's related to the markup pre-compile step. We're running aspnet_compile on the build server. Maybe if there's an exception coming from a generated assembly, the web server will re-compile it. And the recompiled assembly is fine, because it was created with true .Net 4.0.
Well the answer turned out to be borderline embarrassing. After we confirmed from detailed MSBuild output that it was in fact building the website project against the right reference assemblies, we realized that there are several in-house NuGet packages in the project that had been built against .Net 4.5. One of them was chock full of extension methods, which is what causes the exception. Rebuilding them against .Net 4.0 fixed the problem.
Which brings up an interesting issue. If a 3rd-party NuGet package is compiled for 4.0 but using 4.5 references, we'd be in the same situation, but not able to fix it. So the lesson for package publishers is to ensure your 4.0 version is compiled against the reference assemblies.