IdentityModel: System.MissingMethodException when using `new DiscoveryClient(uri)`

When executing this

[HttpGet]
public async Task<IHttpActionResult> GetOpenId(string code, string state) {
    var discoveryClient = new DiscoveryClient(issuerUri);
    // rest omitted for brevity, it breaks here
}

I get

Method not found: 'Void IdentityModel.Client.DiscoveryClient..ctor(System.String, System.Net.Http.HttpMessageHandler)'.

Response from a Web Api 2 action:

<Error><Message>An error has occurred.</Message><ExceptionMessage>Method not found: 'Void IdentityModel.Client.DiscoveryClient..ctor(System.String, System.Net.Http.HttpMessageHandler)'.</ExceptionMessage><ExceptionType>System.MissingMethodException</ExceptionType><StackTrace>   at Admin.OpenIdController.&lt;GetOpenId&gt;d__0.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine&amp; stateMachine)
   at Admin.OpenIdController.GetOpenId(String code, String state)
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;&gt;c__DisplayClass12.&lt;GetExecutor&gt;b__8(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.&lt;InvokeActionAsyncCore&gt;d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.&lt;ExecuteAsync&gt;d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext()</StackTrace></Error>

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (10 by maintainers)

Most upvoted comments

Tbh. I am sick of figuring out which part of the .net tooling is now broken again.

I am facing the same issue.

I have a project IdentityServer4.Management which has references to IdentityServer4.EntityFramework 1.0.1 and IdentityServer4.AccessTokenValidation 1.2.1.

In the Startup.cs I have:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
	Authority = authority,
	RequireHttpsMetadata = false,
	ApiName = "apiResource1"
});

Standalone, that project works fine. However, I have an integration tests project which has a reference to IdentityServer4.Management project. When I try to create a TestServer in the following way:

Environment.SetEnvironmentVariable("AuthorityAddress", _identityServer.BaseAddress.ToString());
_managementServer = new TestServer(new WebHostBuilder()
	.UseStartup<Startup>()
	.UseEnvironment("IntegrationTest"));
_managementClient = _managementServer.CreateClient();

I receive the following error message in the output of my test:

Message: System.MissingMethodException : Method not found: ‘System.ValueTuple`2<System.String,System.String> IdentityModel.Client.DiscoveryClient.ParseUrl(System.String)’.

Not entirely sure what I am doing wrong.

Figured it out. I had to add a <runtime> binding redirect for System.Net.Http for some reason…

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="<public key>" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

After that I could invoke the constructor without fail.

I have got similar issue for the constructor of IdentityModel.Client.TokenClient (in IdentityModel 3.8.0) and adding System.Net.Http to my project as package dependency resolved the issue.

Project target framework: .NET Framework 4.6.2

This also broke for me when updating VS 2017 to 15.4.4. I had to install .NET Framework 4.7 SDK from the VS installer and update my projects to target net47 (previously net461).