8

I am currently developing a desktop application in WPF, which uses an .NET Core library to make porting to different platforms easier. However, I cannot seem to reference the .NET Core library from the WPF app.

I tried the follwing solutions:

  • Reference the project: Visual Studio complains about the project not being an .exe or .dll even though it is.

  • Reference the compiled .dll: This is really ugly, but it seems to work at first. Intellisense is OK with it and the WPF project compiles just fine. But as soon as I want to use any functionality from the .NET Core project a BadImageFormatException is thrown.

  • dotnet pack the project and reference the .nupkg: Installs a bunch of additional packages and throws a BadImageFormatException when any functionality is used.

From what I can gather there are two options here:

  • Do something really hacky like making a .NET Core Console project and passing all objects as strings between the two programs

Or:

  • Just give up on .NET Core and use EF6.

Here's my project.json:

{
  "version": "1.0.0-*",

  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
    "Microsoft.EntityFrameworkCore.Design": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    },
    "dnx451": {}
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  }
}

I tried both dnx451 and net451. The WPF project is also targeting .net 4.5.1. I am using "Visual Studio 2015 Update 3" with ".NET Core 1.0.1 VS 2015 Tooling Preview 2".

1 Answer 1

4

Your project.json isn't correct for a library. A library project should look like:

{
  "dependencies": {
      "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
      "Microsoft.EntityFrameworkCore.Design": {
        "version": "1.0.0-preview2-final",
        "type": "build"
      }
  },
  "frameworks": {
    "net451": { },
    "netstandard1.3": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  }
}

When you run dotnet pack, two DLLs will be produced: one for .NET 4.5.1 and one for .NET Standard 1.3 (or whichever netstandard you want to target). The .NET 4.5.1 DLL should be compatible with your WPF project.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for taking so long to accept, but your answer works fine, except for the fact that dotnet ef doesn't work if I don't emit an entry point. The workaround is quite simple (just put the line back in), but I thought it was worth mentioning.
@AlexanderHoischen Interesting, I hadn't seen that before.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.