Docs: Error CS8701: Target runtime doesn't support default interface implementation.

Created on 31 Jul 2019  ·  14Comments  ·  Source: dotnet/docs

This tutorial uses the term "library" repeatedly yet the sample repo on Github uses a Console App. This is a problem because as far as I can tell, the default interface implementation feature does not work with class libraries. I get error CS8701: Target runtime doesn't support default interface implementation when I try to use this feature in a class library. I am using .NET Core 3.0 Preview 7 on Windows and macOS. Since Microsoft has said that Preview 7 has a Go Live license, this worries me. Please update your tutorial to show that the interface can be in a class library as would be the case in the real world. Thank you.


Document details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide P1 doc-bug support-request

Most helpful comment

Ahh yes. .NET Standard 2.0 doesn't support them either since that was locked down before they were introduced. You need to use .NET Standard 2.1

I do agree though, the tutorial should specify this requirement.

Thanks for filing the issue.

All 14 comments

Can you share your csproj file? Even though you are using .NET Core 3.0 to compile, your project file can still indicate that you're compiling for a target runtime that doesn't support default interface implementations, such as .NET Framework.

For example, here is a project I have that I use with the .NET Core SDK from the command line. The project is configured to target .NET Framework 4.7.2 and therefore wouldn't work with default interfaces:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

</Project>

Thanks for responding quickly, Thraka.

I had two .csproj files because I want the interface with a default implementation of one of its methods in a class library (created with dotnet new classlib) and it will be used in a console app (created with dotnet new console). I use Visual Studio Code on macOS and Windows.

Here is the .csproj file for the class library:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

</Project>

Here's the class:

using static System.Console;

namespace Packt.Shared
{
  public interface IPlayable
  {
    void Play();

    void Pause();

    //void Stop();

    // you cannot use default interface implementations in a class library (yet)
    void Stop()
    {
      WriteLine("Default implementation of Stop.");
    }
  }
}

In Visual Studio Code's Terminal:

$ dotnet --version
3.0.100-preview7-012821

$ dotnet build
Microsoft (R) Build Engine version 16.3.0-preview-19329-01+d31fdbf01 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 69.63 ms for /Users/markjprice/Code/Chapter06/PacktLibrary/PacktLibrary.csproj.
  You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
IPlayable.cs(14,10): error CS8701: Target runtime doesn't support default interface implementation. [/Users/markjprice/Code/Chapter06/PacktLibrary/PacktLibrary.csproj]

Build FAILED.

IPlayable.cs(14,10): error CS8701: Target runtime doesn't support default interface implementation. [/Users/markjprice/Code/Chapter06/PacktLibrary/PacktLibrary.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:03.37

If I change netstandard2.0 to netcoreapp3.0 then everything works. I think your tutorial would really benefit from showing how you need to change a class library project created with dotnet new classlib to support this feature by changing its target framework. Every example I have seen just puts the interface and implementation code in a Exe project which kinda misses the whole point of a "library".

Thanks!

Ahh yes. .NET Standard 2.0 doesn't support them either since that was locked down before they were introduced. You need to use .NET Standard 2.1

I do agree though, the tutorial should specify this requirement.

Thanks for filing the issue.

No, trying netstandard2.1 was one of the first fixes I tried when netstandard2.0 failed, but that gives the same error as netstandard2.0. It does not seem to be possible to create a .NET Standard class library that supports default interface implementation. You have to create class library that is tied to a specific runtime like netcoreapp3.0.

I just tried on preview 6 and 7 and sure enough, you're right. net standard 2.1 isn't working with that feature. I'll send an email asking about why this is so. I did get it to work in preview 8 (unreleased) so it's probably already fixed and only related to targeting standard 2.1. I'll send an email around just to double check.

However, I do think this tutorial should be clearer about what is required for targeting.

That's great news that it works with Preview 8! It worried me that it didn't work with netstandard2.0 or 2.1 when Preview 7 was supposed to be Go Live. At least I now know that in a few weeks when Preview 8 is released it'll work. :)

OK confirmed. 2.1 never supported the default interface members of c#8 and it's coming. it may not ship in preview 8, i can't really say that.

Either way, we should note on this article the requirements 😄

Thank you for your diligent work on this issue, Thraka. Microsoft's continuing openness really helps build trust in your platforms to everyone's benefit.

Leaving this open so we can track getting that updated (whenever it comes to be)

You could always take a crack at submitting the fix yourself by editing the original article. Click on the Content Source link at the bottom of your original comment. To learn how to edit, see the Editing files in a repository article from GitHub.

Thanks again!

@Thraka @markjprice

I just verified that a class library that targets .NET Stanard 2.1 does support default interface implementation. I'm using preview 9.

@markjprice If you're happy with that resolution, we can close this.

@BillWagner perhaps we should escalate this? According to https://github.com/dotnet/standard/pull/1019 this should be supported.

@Thraka It is supported. I just tried in preview 9. I think we should close this now.

OH my for some reason I totally read that you said DOES NOT support. 🐑

I think we're good to close.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stjepan picture stjepan  ·  3Comments

FrancescoBonizzi picture FrancescoBonizzi  ·  3Comments

ygoe picture ygoe  ·  3Comments

svick picture svick  ·  3Comments

sime3000 picture sime3000  ·  3Comments