WebAPIContrib.Core: CSV under .NETStandard 2.0 library running on .NET 4.6.1 gives exceptions

Method not found: ‘Microsoft.Net.Http.Headers.MediaTypeHeaderValue Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(System.String)’.

Seems to be caused by: https://github.com/aspnet/HttpAbstractions/issues/847 They changed the method implementations from string to StringSegment.

And there seems to be an implic cast in StringSegment, fixing it compile time, but giving two distinct assemblies at runtime.

public static implicit operator StringSegment(string value)
{
    return new StringSegment(value);
}

when compiling the WebApiContrib.Core solution locally in vs2017 i seem to get:

.maxstack 2
.locals init (
[0] bool V_0
)

IL_0000: ldarg.0  this
IL_0001: call         instance void [Microsoft.AspNetCore.Mvc.Core]Microsoft.AspNetCore.Mvc.Formatters.InputFormatter::.ctor()
IL_0006: nop          
IL_0007: nop          
IL_0008: ldarg.0  this
IL_0009: call         instance class [Microsoft.AspNetCore.Mvc.Core]Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection [Microsoft.AspNetCore.Mvc.Core]Microsoft.AspNetCore.Mvc.Formatters.InputFormatter::get_SupportedMediaTypes()
IL_000e: ldstr        "text/csv"
IL_0013: call         valuetype [Microsoft.Extensions.Primitives]Microsoft.Extensions.Primitives.StringSegment [Microsoft.Extensions.Primitives]Microsoft.Extensions.Primitives.StringSegment::op_Implicit(string)
IL_0018: call         class [Microsoft.Net.Http.Headers]Microsoft.Net.Http.Headers.MediaTypeHeaderValue [Microsoft.Net.Http.Headers]Microsoft.Net.Http.Headers.MediaTypeHeaderValue::Parse(valuetype [Microsoft.Extensions.Primitives]Microsoft.Extensions.Primitives.StringSegment)
IL_001d: callvirt     instance void [Microsoft.AspNetCore.Mvc.Core]Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection::Add(class [Microsoft.Net.Http.Headers]Microsoft.Net.Http.Headers.MediaTypeHeaderValue)
IL_0022: nop          
IL_0023: ldarg.1  csvFormatterOptions
IL_0024: ldnull       
IL_0025: ceq          
IL_0027: stloc.0  V_0
IL_0028: ldloc.0  V_0
IL_0029: brfalse.s    IL_0037
IL_002b: nop          
IL_002c: ldstr        "csvFormatterOptions"
IL_0031: newobj       instance void [mscorlib]System.ArgumentNullException::.ctor(string)
IL_0036: throw        
IL_0037: ldarg.0  this
IL_0038: ldarg.1  csvFormatterOptions
IL_0039: stfld        class WebApiContrib.Core.Formatter.Csv.CsvFormatterOptions WebApiContrib.Core.Formatter.Csv.CsvInputFormatter::_options
IL_003e: ret   

when looking into dll that msbuild got copied into my output folder i find:

.maxstack 8
il_0000: ldarg.0  this
il_0001: call         instance void [microsoft.aspnetcore.mvc.core]microsoft.aspnetcore.mvc.formatters.inputformatter::.ctor()
il_0006: ldarg.0  this
il_0007: call         instance class [microsoft.aspnetcore.mvc.core]microsoft.aspnetcore.mvc.formatters.mediatypecollection [microsoft.aspnetcore.mvc.core]microsoft.aspnetcore.mvc.formatters.inputformatter::get_supportedmediatypes()
il_000c: ldstr        "text/csv"
il_0011: call         instance class [microsoft.net.http.headers]microsoft.net.http.headers.mediatypeheadervalue [microsoft.net.http.headers]microsoft.net.http.headers.mediatypeheadervalue::parse(string)
il_0016: callvirt     instance void [microsoft.aspnetcore.mvc.core]microsoft.aspnetcore.mvc.formatters.mediatypecollection::add(class [microsoft.net.http.headers]microsoft.net.http.headers.mediatypeheadervalue)
il_001b: ldarg.1  csvformatteroptions
il_001c: brtrue.s     il_0029
il_001e: ldstr        "csvformatteroptions"
il_0023: newobj       instance void [mscorlib]system.argumentnullexception::.ctor(string)
il_0028: throw        
il_0029: ldarg.0  this
il_002a: ldarg.1  csvformatteroptions
il_002b: stfld        class webapicontrib.core.formatter.csv.csvformatteroptions webapicontrib.core.formatter.csv.csvinputformatter::_options
il_0030: ret   

this seems to match with the net451 dll in the nuget.

So i’m guessing here that msbuild says: oh, net461, that’s compatible with net451, lets use that dll, and doesn’t even bother with .netstandard2.0 anymore. However, because our libary uses .NETStandard it also copies over those dlls to the output folder, so it doesn’t use the Framework 4.6.1 dlls but the Standard 2.0 dlls.

Our project structure is:

"ServiceHost" (.NET Framework 4.6.1 executable csproj) --project reference--> "ServiceHosting" (.NET Standard 2.0 class library csproj) --nuget reference--> "WebAPIContrib.Core.Formatter.Csv" 

I assume this is an issue for nuget to fix (if at all possible), and WebAPIContrib to work around?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@damienbod I think the simplest solution is to drop .netstandard1.6 and .net451 and just target netstandard2.0 It will not be the same, since netstandard2.0 supports .net461, but I don’t think it’s a big deal 😀

For those receiving the error System.AggregateException : One or more errors occurred. (Method not found: ‘Microsoft.Net.Http.Headers.MediaTypeHeaderValue Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(System.String)’ when running Test project please add explicitly reference to Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.StaticFiles to your dependencies:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
....
  </ItemGroup>
</Project>

Do it in your csproj file of your Test project.

I will create a new version which only supports 2.0. Nuget and .NET Standard seem to have problems with different versions.