RBasniak RBasniak - 4 months ago 28
C# Question

Can I use dynamic in a .NET Standard class library?

I'm having a hard time trying to migrate from regular Windows desktop development to ASP.NET Core MVC. One issue I'm coming across is to create my solution. I would like to remove everything that is not UI related from the default project that ships with VS 2015 and put into a separated project.

I noticed that ASP MVC Core references to

.NETCoreApp
and the Class Library project references to
.NETStandard
.

My problem is that I need to use the dynamic keyworkd in the class library and it does not support it. The MVC project supports without any problem. I guess it's because of the differente .NET versions.


  1. What is the difference between NETStandard and NETCoreApp?

  2. Can I create a class library that uses the same reference as the MVC project so I can use the dynamic keyword on it?

  3. Or should I stick with a one project solution with all domain, infrastructure, etc in the same place?


Answer

Yes, it's possible to put non-UI code into a separate library. As you guessed, netcoreapp1.0 is for applications (console or web), and netstandard1.X is for class libraries.

A .NET Standard class library shouldn't have any problem with the dynamic keyword. You do need to reference NETStandard.Library, though. Here's a barebones working library example (from Targeting .NET Standard):

MyLibrary/project.json

{
   "description": "My awesome library",
   "dependencies": {
      "NETStandard.Library": "1.6.0"
   },
   "frameworks": {
      "netstandard1.3": { }
   }
}

Your ASP.NET Core web application can reference this library if it's part of the same solution like this:

{
  (... other stuff)

  "dependencies": {
    "MyLibrary": {
      "target": "project"
    }
  }
}