2

I'm migrating a legacy .NET 4.6.2 library to the new .NET Core framework (for .NET Standard 1.6) using VS2017 (RTM). The library should be consumed by both .NET Core and .NET462 clients, so its .csproj file targets both frameworks e.g. (notice the pluralized tag):

<TargetFrameworks>netstandard1.6;net462</TargetFrameworks>

My only issue is that some code uses XML schema validation; this is not available in .NET Core. AFAIK, after a first period of uncertainty (see http://xmlprime.blogspot.it/2016/07/net-core-and-state-of-xml.html), from later posts like XSD/Schema validation workaround in .net core? it seems that it's forthcoming, but meantime I have to go on, at least by supporting it for .NET462 environments. The solution should be simple:

  1. in the library using XSDL, add a conditional reference to the XSDL library. In this example, I'm referencing a .NET462 wrapper around it:

    <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <Reference Include="TheLibUsingXSDL">
            <HintPath>..\Lib\TheLibUsingXSDL.dll</HintPath>
        </Reference>
    </ItemGroup>
    
  2. it seems that having multiple target frameworks is not enough to get the compilation constants right in your project, as the editor grayed out all my .NET 462 code in the library. I need NET462 defined when targeting .NET 462, so that I can wrap XSDL-related code in #if NET462 ... #endif directives. To this end, after googling a bit I found this documentation: https://learn.microsoft.com/en-us/dotnet/articles/core/tools/project-json-to-csproj. It does not exactly cover my case, but I tried this with a bit of guesswork, and it seems to work:

    <PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <DefineConstants>$(DefineConstants);NET462</DefineConstants>
    </PropertyGroup>
    

    Now the .NET 462 code is no more grayed out, and the project compiles. I can create a package, and store it in my own local NuGet feed.

  3. I then created a .NET core console app targeting both .NET core and .NET462, and added to it this package from my local feed. There, the code consuming XSDL-based functionality must be conditional too, so I added the above property group also to the console app project.

Yet, when I try to compile this code in the console app, I get a type not found compile error. Could anyone help? Maybe I'm just missing something obvious, but VS2017 is fairly new and so it its csproj format. The essential parts of my csproj files follow.

  1. the library using XSDL (TheLibUsingXSDL in the sample above) is a full .NET 4.6.2 library. Another .NET Core library with multiple targets wraps it, and it is right this library to be referenced by the consumer console app. Its csproj is like:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFrameworks>netstandard1.6;net462</TargetFrameworks>
        <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
        <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
        <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
        <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
        <Version>1.0.3</Version>
      </PropertyGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <Reference Include="System.Xml" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System" />
        <Reference Include="Microsoft.CSharp" />
      </ItemGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <Reference Include="TheLibUsingXSDL">
          <HintPath>..\Lib\TheLibUsingXSDL.dll</HintPath>
        </Reference>
      </ItemGroup>
      <PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <DefineConstants>$(DefineConstants);NET462</DefineConstants>
      </PropertyGroup>
    </Project>
    
  2. the consumer console .NET Core app csproj is like:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFrameworks>netcoreapp1.1;net462</TargetFrameworks>
        <RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.6' ">1.1.1</RuntimeFrameworkVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="TheLibUsingXSDL" Version="1.0.3" />
        <PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.0" />
        <PackageReference Include="NLog" Version="5.0.0-beta06" />
      </ItemGroup>
      <PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <DefineConstants>$(DefineConstants);NET462</DefineConstants>
      </PropertyGroup>
    </Project>
    
Community
  • 1
  • 1
Naftis
  • 4,393
  • 7
  • 63
  • 91
  • A quick glance at the consumer console project: the RuntimeFrameworkVersion condition calls for netcoreapp1.6, not netcoreapp1.1 – JZimmerman May 19 '17 at 21:31

0 Answers0