Chris Pickford
Chris Pickford

Reputation: 8991

FileNotFoundException exists in both System.IO and System.Runtime

I've started a simple new ASP.NET Core web project which needs to read the contents of files.

In a new Class Library (Package) (Visual C# > Web) project, which by default targets the following frameworks. I've added System.IO.FileSystem also.

"frameworks": {
  "net451": { },
  "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.IO.FileSystem": "4.0.0",
      "System.Runtime": "4.0.21-beta-23516",
      "System.Threading": "4.0.11-beta-23516"
    }
  }
}

In one of my classes, I am trying to throw a FileNotFoundException but I get the following error against .NET Platform 5.4:

CS0433 The type 'FileNotFoundException' exists in both 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I've tried removing the reference to System.Runtime but this is futile as System.IO.FileSystem itself lists it as a dependency.

This only seems to manifest itself when building against .NET Platform 5.4 as seen here:

VS2015 screenshot

And the offending code:

if (!File.Exists(file))
    throw new System.IO.FileNotFoundException();

Have I misconfigured my project dependencies? Is there an alternative IO namespace I should be using?

Upvotes: 3

Views: 1298

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141492

Have I misconfigured my project dependencies? Is there an alternative IO namespace I should be using?

You have misconfigured your project dependencies. The error message tells us that much. There is not an alternative IO namespace to use. Rather, you need to reconfigure your project dependencies to avoid naming collisions.

Option 1, Stick with RC1 and specify compatible dependencies

global.json > target RC1 of the SDK.

"sdk": {
  "version": "1.0.0-rc1-update1"
}

project.json > change the dependencies to be compatible. It turns out that the 4.0.0 versions of System.IO and System.Runtime are compatible whereas the betas you were using are not.

"frameworks": {
  "net451": { },
  "dotnet5.4": {
    "dependencies": {
      "Microsoft.CSharp": "4.0.1-beta-23516",
      "System.Console": "4.0.0-beta-23516",
      "System.IO": "4.0.0",
      "System.Runtime": "4.0.0"
    }
  }
}

Option 2, Upgrade to RC2 and use NET Standard Platform

If you are ready to target the RC2 SDK, then you can target a specific .NET Platform Standard. Here is an example that targets the rc2-20221 SDK and netstandard1.3.

global.json > target RC2 of the SDK.

"sdk": {
  "version": "1.0.0-rc2-20221"
}

project.json > use the netstandard to ensure a compatible API surface area.

"dependencies": {
  "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*"
},
"frameworks": {
  "netstandard1.3": {
    "dependencies": {
      "System.Console": "4.0.0-beta-*"
    },
    "imports": [
      "net451",
      "dotnet5.4"
    ]
  }
}

Test Harness

This is a small program that is building and running on my machine. It works using either option 1 or option 2.

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            throw new System.IO.FileNotFoundException();
        }
        catch(System.Exception ex)
        {
            System.Console.Write(ex.ToString());
            System.Console.ReadKey();
        }
    }
}

Upvotes: 1

Related Questions