James Thurley
James Thurley

Reputation: 2900

The type 'Exception' is defined in an assembly that is not referenced in ASP.NET 5 RC1

This issue is very easy to reproduce, but I'm not sure of the fix. I'm on Windows 10, Visual Studio 2015. I've installed the RC1 of ASP.NET 5.

To reproduce:

New Project -> Class Library (Package)

Edit project.json, add EnterpriseLibrary.TransientFaultHandling to net451 dependencies.

"frameworks": {
  "net451": {
    "dependencies": {
      "EnterpriseLibrary.TransientFaultHandling": "6.0.1304"
    }
  },
  "dotnet5.4": {
    "dependencies": {
      "Microsoft.CSharp": "4.0.1-beta-23516",
      "System.Collections": "4.0.11-beta-23516",
      "System.Linq": "4.0.1-beta-23516",
      "System.Runtime": "4.0.21-beta-23516",
      "System.Threading": "4.0.11-beta-23516"
    }
  }
}

Make Class1 implement ITransientErrorDetectionStrategy.

public class Class1 : ITransientErrorDetectionStrategy
{
    public Class1()
    {
    }

    public bool IsTransient(Exception ex)
    {
        throw new NotImplementedException();
    }
}

Build and you get the error: .NET Framework 4.5.1 error CS0012: The type 'Exception' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I've tried adding "System.Runtime": "" to net451 dependencies in project.json but it can't be resolved. If I try to add a reference, "System.Runtime" doesn't exist in the list of assemblies.

I think I need to use Class Library (Package) as I'm referencing it from an ASP.NET 5 Web API project.

In the short term I can live without dotnet5.4 support, as I understand the TransientFaultHandling library doesn't support it yet.

But how do I make it compile for .NET 4.x?

Upvotes: 1

Views: 2866

Answers (1)

Vadim Martynov
Vadim Martynov

Reputation: 8902

For Class Library (Package) project type you should add dependency clearly to the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.1\Facades\System.Runtime.dll

  1. Navigate to your managed desktop app project in the Solution Explorer.
  2. Right-click the References node and click Add Reference.
  3. Click the Browse tab.
  4. Click Browse….
  5. Navigate to the System.Runtime.dll façade. You can generally find this in a path similar to: %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll

via MSDN

For asp.net MVC projects with PCL there is a similar error message. Try to replace web.config section to avoid this.

<compilation debug="true" targetFramework="4.5"/>

to

<compilation debug="true" targetFramework="4.5">
  <assemblies>     
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  </assemblies>
</compilation>

Via

Upvotes: 2

Related Questions