question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
filipwcommented, Sep 1, 2017

@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 😀

1reaction
seccijrcommented, Nov 5, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - .NET 4.6.1 project referencing a .NET Standard 2.0 ...
Building it with a hosted agent on VSTS gives the following error: The type 'Object' is defined in an assembly that is not...
Read more >
NET Standard
NET Standard allows a library to use more APIs but means it can only be used on more recent versions of .NET. Targeting...
Read more >
FileStream Class (System.IO)
Provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.
Read more >
Please stop lying about .NET Standard 2.0 support!
In this post I have a bit of a rant about Microsoft's NuGet packages lying about supporting .NET Standard 2.0 when they kinda...
Read more >
Refit 7.0.0
The automatic type-safe REST library for Xamarin and .NET. ... NET Standard 2.0 . ... The relative URL of the resource is specified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found