Why am I getting error "The type 'IReturn<>' is defined in an assembly that is not referenced" using ServiceStack in VIsualStudio 2017
I am running Visual Studio 2017 15.6.3.
I have a .NET Standard 2.0 DLL project which contains Request and Response classes for use with ServiceStack. The Request classes implement IReturn<>.
I have a .NET Core 2.0 console EXE project which references the .NET Standard DLL. This EXE uses a ServiceStack JsonServiceClient to send requests to a ServiceStack service. It compiles and works fine.
I added a .NET Framework 4.6.1 console EXE project which also references the .NET Standard 2.0 DLL. It has to be a Framework app because it references other DLLs which are not compatible with Core or Standard. This EXE uses a ServiceStack JsonServiceClient to send requests to a ServiceStack service exactly like the .NET Core EXE, but this program will not compile. Framework 4.6.1 is supposed to support .NET Standard 2.0 DLLs, but for some reason it has a conflict with the IReturn<> interface.
var extentRequest = new ExtentRequest { ... };
using (var client = new JsonServiceClient(baseUrl))
{
return client.Post(extentRequest);
}
The error returned is: "The type 'IReturn<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null'."
I can't think of any reason why it isn't working:
- When I look at the list of References for the project, I see ServiceStack.Interfaces and its Properties say it is version 5.0.0.0.
- I can use "Peek Definition" within the Framework executable project code and work my way through the chain of inheritance to find IReturn<> so it knows the type.
- All three projects use ServiceStack version 5.0.2 acquired with NuGet.
- All projects are configured and compiled for x64.
I assume the problem is some sort of mismatch between Framework and Standard versions. Can anyone tell me why I'm getting this error?
Solution 1:
You can't share a .NET Framework .dll with .NET Framework dependencies in .NET Core or .NET Standard projects and vice-versa.
Most ServiceStack NuGet packages contain both .NET v4.5 and .NET Standard 2.0 builds:
-
net45
- Contains support for running ASP.NET Web or Self-Hosting HttpListener App Hosts -
netstandard2.0
- Contains support for only running on ASP.NET Core App Hosts
Only the .NET v4.5 builds contains support for hosting on classic ASP.NET or SelfHost HttpListener Hosts (i.e. AspNetRequest
/AspNetResponse
). You cannot use .NET Standard builds on ServiceStack in classic ASP.NET Web projects as the builds do not physically contain the dependencies and functionality needed - which isn't available in .NET Standard 2.0 (which only covers running ASP.NET Core Apps).
There are 2 solutions for being able to share the same project in .NET Framework v4.6.1+ and .NET Standard / .NET Core projects:
Only reference .Core .NET Standard packages
To be able to share the same .dll
with .NET Framework projects you need to either reference the .NET Standard only .Core
packages so that when NuGet packages are installed your .NET v4.6.1+ project is referencing the same .NET Standard 2.0 dlls as your .NET Standard or .NET Core projects are using.
Create Multi-targed .NET Framework and .NET Standard 2.0 projects
The alternative is to maintain multi-targeted projects which creates both .NET Framework and .NET Standard builds. This is the approach that the Hello Mobile Shared Gateway project uses to support both .NET Framework and Mobile .NET Standard clients and the ServiceStack Server.Common project uses for the same ServiceStack Server implementation to be used in:
- Server.NetCore - hosting the ServiceStack Services in a ASP.NET Core 2.0 App
- Server.NetCoreFx - hosting in a ASP.NET Core App on the .NET Framework
- Server.AspNet - hosting classic ASP.NET Framework Web Applications
- Server.HttpListener - host in a .NET Framework Self-Hosting HttpListener AppHost