A common class library consumed by both .NET Core and .Net 4.5.2

前端 未结 2 2185
礼貌的吻别
礼貌的吻别 2021-02-13 15:31

I\'m fairly new to .Net Core, but have made a working Asp.Net Core WebAPI site - now I want to share some code with another project...

  • I have Visual Studio 2015 wi
相关标签:
2条回答
  • 2021-02-13 15:54

    The only way I have found to make this work, is to hack reference the .csproj file of the Client library: https://github.com/JonnyWideFoot/netcore-prototype/blob/master/src/JE.API.Experiment.Client/JE.API.Experiment.Client.csproj

    <Reference Include="JE.Api.Experiment.Contract, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\JE.Api.Experiment.Contract\bin\$(Configuration)\net452\JE.Api.Experiment.Contract.dll</HintPath> </Reference>

    By hard-coding the path to the correct output folder from the contracts library, all is fine.

    ... thinking this could be a bug in visual studio.

    0 讨论(0)
  • 2021-02-13 16:05

    Here's how I create shared libraries that can be consumed from both .NET Core projects and .NET 4.5 projects:

    SharedLibrary\project.json

    "dependencies": { },
    "frameworks": {
      "net45": { },
      "netstandard1.1": {
        "dependencies": {
          "NETStandard.Library": "1.6.0"
        }
      }
    },
    "version": "1.0.0"
    

    A consuming (.NET Core) library in the same solution references it like this:

    "dependencies": {
      "SharedLibrary": {
        "target": "project",
        "version": "1.0.0"
      }
    },
    "frameworks": {
      "netstandard1.1": { }
      }
    }
    

    A consuming .NET 4.5 project using project.json would look the same with the exception of net45 in the frameworks section. Installing in a csproj-based .NET 4.5 project works too, if a NuGet package for SharedLibrary is produced.

    According to the .NET Platform Standard docs, simply targeting netstandard1.1 should allow the shared library to be installed in .NET 4.5+ projects as well. I've run into strange issues with that, but it may have been the result of beta tooling.

    0 讨论(0)
提交回复
热议问题