2

I'm tasked with starting a "slow migration" from .NET Framework (4.7.1) to .NET Core (2.1) at work. We have a very large application, and aren't able to just stop development and create a brand new stack simply in .NET Core. I've worked through having a .NET Standard (2.0) library as the "go between" for the .NET Framework and the .NET Core projects. However, I'm running into an issue with some Core-specific code.

My .NET Core .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.1;netstandard2.0</TargetFrameworks>
  </PropertyGroup>

</Project>

I had to add the netstandard2.0 and make it multi-framework to include the interfaces in our Standard library.

When I do that, I get all sorts of build errors when trying to use code that is only in Core. For example, I get the following error:

'File' does not contain a definition for 'AppendAllTextAsync'.

Is there any way to utilize or even have the new functionality in our application before we remove the .NET Framework and Standard code? (I have tried a brand new .NET Core Project in a new Solution, and it worked fine until I changed the .csproj to TargetFrameworks.

1
  • 1
    Multi-targeting in this context means that an assembly can be compiled against multiple platforms but if they use different set of APIs it cannot work equally on different platforms. So in order to only HAVE new functionality you'd need supply conditions how to compile the code against different platforms with preprocessor directives like #if, etc. In order to UTILIZE you'd need to go farther and provide adequate substitution of desirable behavior for each platform you're going to support. Commented Sep 25, 2018 at 12:39

1 Answer 1

1

You are building against .NET Core 2.1 AND .NET Standard 2.0. .NET Core is a superset of .NET Standard, meaning that it has the standard and some more. File.AppendAllTextAsync is one of them.

.NET Core: https://learn.microsoft.com/en-us/dotnet/api/?view=netcore-2.1&term=AppendAllTextAsync

.NET Standard: https://learn.microsoft.com/en-us/dotnet/api/?view=netstandard-2.0&term=AppendAllTextAsync

So unfortunately you cannot just simply crosscompile that code against the .NET Standard. If you wish, you can go with the #if notations to write framework specific code. Or, you can stick with the code that is supported in .NET Standard, you can for example use the non-async variant: https://learn.microsoft.com/en-us/dotnet/api/?view=netstandard-2.0&term=AppendAllText

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

2 Comments

Thanks for that clarification. I'm going to go with the #if NETCOREAPP2_1 directives for now, and polyfill some functionality for now. That way, once we complete the switch over to .NET Core, all we have to do is drop the other side of those directives, and all the other code is good to go.
Sounds like a fair plan. The API browser really helped me with finding the suitable APIs for .NET Standard. Good luck!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.