System.ServiceModel not found in .NET Core project - c#

I have a .NET Core xUnit project. I'm trying to call a WCF service from it but get the following exception:
System.InvalidOperationException occurred
HResult=0x80131509
Message=An error occurred while loading attribute 'ServiceContractAttribute' on type 'IMyContract'. Please see InnerException for more details.
Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
It works with a Framework 4.7 project with the same Nuget Package System.ServiceModel.Http.4.3.0.

Microsoft has made available the relevant assemblies as packages on NuGet now.
System.ServiceModel.Primitives is the base package; add the others if necessary to your project.
Update (April 28th, 2022):
WCF has been partially ported to .NET 5+ with an official library called CoreWCF: https://devblogs.microsoft.com/dotnet/corewcf-v1-released/

If you are using .NET Standard 2.0 (that's what I tested with), you can install compatible NuGet packages.
The basic service model is available in System.ServiceModel.Primitives (currently v4.4.0).
If required, install System.ServiceModel.Http as well.

The Microsoft WCF Web Service Reference Provider wraps SvcUtil.exe and will generate a .NET Standard project from your endpoint. Look in the project file and you'll see the ServiceModel references that will work for you.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.3.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.3.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
</ItemGroup>
</Project>
When I needed to do this I was able to use the generated class library in my .NET Core project.

I had same issue, i had to add .asmx web service into my class library asp.net core project, i tried to add reference directly by add connected service option but i wan unable to see any config file and lots of references errors also, below are the steps by which i solve my problem.
1-Create library project name Service Mapping
2-Create folder Web References and then create inside folder ClientHotel
3-Open terminal in visual studio from view ---> terminal --- command prompt.
4-Integrate asmx service by command
svcutil http://abctest.asmx (.asmx URL)
inside inside ClientHotel folder.
5-it will create .cs class and output.config file with references in it.
6-Now i had errors of references like
the type or namespace servicecontractattribute does not exist in namespace System.ServiceModel etc.
7-Install Package System.ServiceModel.Primitives
Then all references errors will be gone.
That is how i was able to add .asmx service to my class library project.

Related

Cannot Migrate Blazor App to .Net 5 Due to System.Runtime Error

I have been working through an awesome tutorial within Udemy to learn more about Blazor (https://www.udemy.com/course/programming-in-blazor-aspnet-core/), but have hit a stumbling block that I'm not entirely sure what to do with.
Short Version
When upgrading to .Net 5 from .Net Standard 2.1, I end up with this error when trying to run this sample Blazor application as soon as it loads up (so it's not hitting any of my code): System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') I see a similar problem with this SO link, but it didn't really give me much to go off of.
Detailed Version
With prior versions of .Net, you installed the latest, then Visual Studio picked that up, you switched projects and away you went - everything was seamless and just worked. With some of the newer stuff though, Microsoft's messaging has been extremely confusing and the problem I'm hitting now is inside that Udemy tutorial I need to utilize the IJSObjectReference interface to do something. When I first added that to the code, the type reference couldn't be resolved so a quick search pointed me to needing to move the project to .Net 5 by changing this:
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
to this (because Visual Studio doesn't always show .Net 5 as an option):
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
Seemed simple enough, so I changed the Client Blazor project to this and tried to compile. That gives me this error: Project BlazorMovies.Client is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project BlazorMovies.Client supports: net5.0 (.NETCoreApp,Version=v5.0). I figured okay, I'll bump Server to 5.0 next and then everything compiles fine, but as soon as I pull it up, I get this error: System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'). Then I remembered not recalling if I'd installed .Net 5 yet, so I went to check and (via learn.microsoft.com) I only have 4.8.03752 installed. I then did some searches to try and find the .Net installers and there were multiple (see here) - even the layout of the page is really overwhelming, with ~20 install links scattered throughout. I knew I needed at least x64, so I first installed the SDK since it said Visual Studio support and that went significantly faster than I expected (based on prior installs of .Net), but now VS is showing .Net 5 which seemed promising! I re-checked the registry though, and it still says 4.8.03752 and when I went to Add/Remove programs, .Net 5 doesn't show up like all the other versions. I next installed the Hosting Bundle which said it was successful, but the sample app still has the exact same error.
Any advice? I know Blazor is quite new, but with Microsoft's extremely confusing messaging between .Net Framework, .Net Standard, .Net Core and now a migration back into .Net 5 that seems to need multiple installers, I don't really know where to go next. That error is entirely generated from within the Web Assembly code according to the stack trace, so it doesn't appear to be anything related to what I'm doing. Here's a screenshot of everything Chrome shows me in the console:
To migrate Blazor Webassembly Application from netstandard2.1 to .NET 5, you could refer the following steps:
Install or update Visual Studio 2019 to version 16.8.0+, and install the .NET 5 SDK.
Change the Blazor Webassembly project setting.
Create a Blazor Webassembly project (When create the application, select .net core 3.1, then, the TargetFrameWork will be .netstandard2.1), Open the application .csproj file:
Update the SDK from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk.BlazorWebAssembly
Set the TargetFramework to net5.0
In case you have it, remove the package reference to Microsoft.AspNetCore.Components.WebAssembly.Build.
After updating, the .csproj file content as below:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
</Project>
Update the Nuget dependencies version.
The result like this:
Then, the final .csproj file content looks as below:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.2" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>
</Project>
Clean the entire solution, otherwise the build engine won’t be able to re-generate all the required files with the updated framework.
Then running the application, the website works well.
Have you changed the header node in the *.csproj too?
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
...
</Project>

Microsoft.AspNetCore.Mvc.ViewFeatures version mismatch (asks for 3.0.0, but the latest version is 2.2.0)

I get this error when I'm building my ASP.NET Core MVC application:
Error CS0012 The type 'Controller' is defined in an assembly that is
not referenced. You must add a reference to assembly
'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=adb9793829ddae60'
And when I go to NuGet site, I see that the latest version of Microsoft.AspNetCore.Mvc.ViewFeatures is 2.2.0.
What should I do?
I had the same problem in my Class Library project. I found a solution by adding the following code to the project's .csproj file. I hope it works for you, too.
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
ASP.NET Core applications should use the web SDK. If you create a new ASP.NET Core MVC project, then inspect the project file, you'll see it has Sdk="Microsoft.NET.Sdk.Web". If yours just says Microsoft.NET.Sdk, you might want to change it.
If you're not using the web SDK for a good reason (for example, your project is a class library, not an application/exe), then given that the version number it's complaining about, it's clear you're using .NET Core 3.0. In 3.0 and higher, framework libraries are no longer distributed as NuGet packages, but instead use a new versionless FrameworkReference MSBuild item. Once everything targets .NET Core 3.0 and higher a LOT of package versioning issues are going to disappear.
Anyway, the ASP.NET docs page has a good example on the page for migrating from 2.2 to 3.0. There's also a little more information on FrameworkReference.
Here's the example I like that shows the difference between 3.0 and earlier versions:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
</ItemGroup>
</Project>
You have probably updated a nuget or referenced something that causes the mismatch.
open your solution go to the project
check references -> Microsoft...ViewFeatures
right click check properties window, you should see version there
the problem can be that some dll requires a higher version of this package
consolidating/updating some nuget that delivers this package as well might help, the dll/package might have been put into another nuget instead of shipping it separately

Why do I need Razor pages dependencies to write Integration Tests for a .NET Core Web Api?

I'm trying to write integration tests for a .NET Core Web Api I'm building.
Following the documentation found Here I have referenced the Packages Microsoft.AspNetCore.App and Microsoft.AspNetCore.Mvc.Testing via Nuget.
However, when I try to run my tests, I get an exception with the following message:
Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
It sounds like it's complaining because a Razor pages dependency is missing? My Api doesn't use Razor pages. Why do I need this dependency? Am I doing something wrong?
As a related side note: What is the different between the Microsoft.AspNetCore.Mvc.Testing and the Microsoft.AspNetCore.TestHost? The former is mentioned on the doc page I linked above, and the latter is mentioned under the Test Controllers heading on the same page, with little to no explanation.
EDIT: I have used Nuget to install the requested package in the error message, Microsoft.AspNetCore.Razor.Runtime, and I still get the same error. Now I'm really confused.
EDIT 2: As requested, the .csproj file for the test project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Api\Api.csproj" />
</ItemGroup>
</Project>
My test setup is given below:
public class StartupIntegrationTests
: IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _webAppFactory;
public StartupIntegrationTests(WebApplicationFactory<Startup> webAppFactory)
{
_webAppFactory = webAppFactory.WithWebHostBuilder(builder => builder
.UseStartup<Startup>()
.UseSetting("https_port", "443"));
}
[Theory]
[InlineData("/Users")]
public async Task ShouldUseHttpsForAllRequestsIfClientDidAutoRedirect(string url)
{
var client = _webAppFactory.CreateClient();
var response = await client.GetAsync(url);
// Ensure that the returned URL is an https url and that there were no errors
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.StartsWith("https://localhost:443/",
response.RequestMessage.RequestUri.OriginalString);
}
// Some more tests omitted to keep things short
}
This is probably due to a version mismatch between 2.1 and 2.2: you're targeting .NET Core 2.1 via <TargetFramework>netcoreapp2.1</TargetFramework> but you're including 2.2.0 packages. Either downgrade the NuGet packages to 2.1.x versions or upgrade your application to 2.2.0:
Download the .NET Core 2.2 SDK from here
Update the target framework of your application from netcoreapp.2.1 to netcoreapp2.2: <TargetFramework>netcoreapp2.1</TargetFramework>. Make sure you do this for both your application and your test application
Update all package references of Microsoft.AspNetCore to the latest version (2.2.0 or higher).
Also make sure to include a package reference to Microsoft.AspNetCore.App in your test project by including this in your project file:
<PackageReference Include="Microsoft.AspNetCore.App" />
Regarding your question about Microsoft.AspNetCore.Mvc.Testing vs. Microsoft.AspNetCore.TestHost: the TestHost namespace consists of a set of types (including TestServer) to enable hosting your application in-memory for testing purposes. The Microsoft.AspNetCore.Mvc.Testing package provides infrastructure to enable functional testing of applications (including the TestServer) by way of using the WebApplicationFactory class.

dotnet pack for .NET Core project produces .nupkg file with sources

I have a simple .NET Core 2.1 class library project and need to pack it as a Nuget package. For that I'm using this command:
dotnet pack <project>.csproj --output <outputFolder> /p:Configuration=Release
But, I'm getting this warning:
C:\Program Files\dotnet\sdk\2.1.502\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(202,5): warning NU5100: The assembly 'obj\Release\netcoreapp2.1\<assembly>.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
I understand that lib folders are for targeting multiple frameworks, but I don't want that. I only want to target .NET Core 2.1.
At the end my .nupkg file is created, but it has all the source code (don't know why) and when used in other projects, they cannot reference the assemblies, because they are inside the bin\Release\netcoreapp2.1 folder.
I've seen some guides to create Nuget packages from .Net Core projects and none of them mention something related to lib folders.
I need to understand if something is missing or if I'm doing something wrong.
Thanks.
Edit: Added the project file and the .nuspec file
Project File:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Company>xxx</Company>
<RootNamespace>xxxx</RootNamespace>
<Product>xxxx</Product>
<Authors>xxxx</Authors>
<NuspecFile>xxxxx.nuspec</NuspecFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
</ItemGroup>
</Project>
.nuspec File:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>xxxxxx</id>
<version>1.0.0</version>
<title>xxxx</title>
<owners>xxxx</owners>
<description>xxxx</description>
<authors>xxxx.</authors>
<copyright>Copyright 2019 xxxx.</copyright>
<summary>xxx is a common code library for internal projects.</summary>
</metadata>
</package>
There appear to be a few misunderstandings here.
Firstly, the lib folder in the nupkg is not for multitargetting, but for all dlls that are part of the build. If your package only supports 1 target framework moniker (TFM), then your nupkg will only have a single folder under lib, for example lib/netstandard2.0/MyLib.dll, or possibly lib/netcoreapp2.1/MyLib.dll if your app uses APIs that are part of the .NET Core 2.1 runtime, but not netstandard. If your project only uses APIs that are part of netstandard, there's no benefit to targetting netcoreapp and has potential problems which might cause you issues in the future, even if it works fine today.
Secondly, a simple class library (to me this means the project only contains .cs files and no content files, no build file, the package will only contain the dll and NuGet's own files) shouldn't need to know anything about what's inside a nupkg. Even more complex projects that multitarget don't need to care about the lib folder when using an SDK style project. Just specify your target TFMs in the <TargetFrameworks> element, and let the SDK pack the nupkg itself. It knows what to do. If you've done anything in your csproj to try to force the output dll to a different location within the nupkg, it's more likely to cause problem than improve things.
Without seeing your .csproj, I cant' guess what you could have done to get that warning message on pack, but like I said, a brand new dotnet new classlib packs just fine, and if your project only contains code files, you shouldn't need anything in the csproj related to pack paths.

Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0

First of, some context information:
The platform this is running on has .Net Framework 4.7.1 installed. I have a Class Library that is in the .Net Standard 2 specification in order to support .Net Core in the future. Now parts of dependencies, namely Dapper, uses System.Data.SqlClient. This library works just fine on my own machine but I run into problems when I deploy and test it on my Windows 2012 server. Namely, I have a runtime error when Dapper is used: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=kfddsnfsjnfs' or one of its dependencies. The system cannot find the file specified.
Mind you I first had version 4.5.1.0 installed. I then downgraded to 4.4.0.0 and rerrun the code. Now I got the same error but this time regarding 4.2.0.0. But I cannot seem to find this particular version on Nuget. After this I googled. A lot. First I tried adding rebindining the old version with a new by adding both a
appsettings.json:
{
"dependentAssembly": {
"assemblyIdentity": {
"name": "System.Data.SqlClient",
"publicKeyToken": "kfddsnfsjnfs",
"culture": "neutral"
},
"bindingRedirect": {
"oldVersion ": "4.4.0.0",
"newVersion": "4.5.1"
}
}
}
and app.config:
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlClient" publicKeyToken="kfddsnfsjnfs" culture="neutral" />
<bindingRedirect oldVersion="4.4.0.0" newVersion="4.5.1.0" />
</dependentAssembly>
However it didn't make a difference. I have also tried older versions of the SqlClient and multiple reinstalls. I also found people who said to double check the csproj file so it didn't reference something in the gac, but it does not:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>me</Authors>
<Product />
<Company />
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.8</Version>
</PropertyGroup>
<ItemGroup>
<Content Include="TaskMetadata.json">
<PackagePath>TaskMetadata.json</PackagePath>
<Pack>True</Pack>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="dapper" Version="1.50.5" />
<PackageReference Include="itextsharp" Version="5.5.13" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="External\" />
</ItemGroup>
</Project>
Each library runs under the process of the main application. So the main application needs to know to load the SqlClient DLL. So the parent project (the .NET 4.7.1 project) needs to have SqlClient added as a reference, either by installing the NuGet package, or just adding a reference by browsing to the DLL under the .NET Standard project.
Old answer: That version exists in NuGet: https://www.nuget.org/packages/System.Data.SqlClient/4.4.0
In the Package Manager Console (make sure the 'Default project' drop-down is set to the right one), try uninstalling and then reinstalling that specific version:
Uninstall-Package System.Data.SqlClient
Install-Package System.Data.SqlClient -Version 4.4.0
Update: Or, in your binding redirect, just use 4.2.0.0 as the oldVersion.
install version 4.8.2 in both project
Install-Package System.Data.SqlClient -Version 4.8.2
I had the same issue, Basically I was developing a win form app. with .net framework, and my class library was .net standard.
I tried install and uninstall System.Data.SqlClient ten times, it did not work
Then something later crossed my mind as I installed same reference in the UI layer as well then it worked. even though, I did not have any code talking to Sql on UI layer, then another issue raised as the UI did not recognize reference to Dapper as well, and I had to install Dapper reference on UI too. Then everything was fine.
I really surprises how that did not work. Once you referenced the UI to the class library, the class library supposed to do what is required and pass the data to UI!!
Switch from System.Data.SqlClient to Microsoft.Data.SqlClient.
https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/
Try adding System.Data.SqlClient again via NuGet although you may already have it handy as a part of the .NET Framework. This solution resolved my problem.
I was facing this issue while running unit test cases. {"Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.":"System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"} System.IO.FileNotFoundException
When I install System.Data.SqlClient, Version=4.4.0.0 then it did not work.
But this got fixed when I installed the latest version of System.Data.SqlClient nuget package.
I was getting this error with .NET Core (using the latest Microsoft.Data.SqlClient) and had to change my DBProvider from
Microsoft.Data.SqlClient
to
Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient
in order for NLog to load Microsoft.Data.SqlClient
I was getting this error at the console in one of my webjobs in Azure. In the publish profile I changed configuration from "debug" to "release" and then it worked.

Categories