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.

"Unable to resolve type: System.Threading.Tasks.Task" exception in WebApi application

See original GitHub issue

I’m trying to resolve my own OAuthAuthorizationServerProvider for injecting it into OAuthOptions in my application. But I’m getting a strange exception on app start. I’ve tried many cases, and it works if I use RegisterInstance method.

Server Error in '/' Application.
Unable to resolve type: System.Threading.Tasks.Task, service name:
[InvalidOperationException: Unable to resolve type: System.Threading.Tasks.Task, service name: ]
   LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:3999
   LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:3970
   LightInject.ServiceContainer.GetInstance(Type serviceType, Object[] arguments) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:2675
   LightInject.ServiceContainer.GetInstance(T value) in c:\GitHub\LightInject\NuGet\Build\Net45\LightInject\LightInject.cs:2736
Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider.MatchEndpoint(OAuthMatchEndpointContext context) +10
   Microsoft.Owin.Security.OAuth.<InvokeAsync>d__0.MoveNext() +421
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +383

Steps to reproduce (easy):

  1. Create a new standard ASP.Net 4.5.2 Template “Web API” solution
  2. Add package “LightInject.WebApi 1.0.0.4”
  3. Open “ApplicationOAuthProvider.cs” and remove all constructors
  4. Open “Startup.Auth.cs”, change creation of OAuthOptions to the following and start the application:
var container = new ServiceContainer();
container.Register<ApplicationOAuthProvider, ApplicationOAuthProvider>();

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = container.GetInstance<ApplicationOAuthProvider>(), // this is the modified line
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};
<package id="LightInject" version="3.0.2.7" targetFramework="net452" />
<package id="LightInject.WebApi" version="1.0.0.4" targetFramework="net452" />
Project target: .Net Framework 4.5.2 "Any CPU"
Windows 10 x64, VS 2015

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
seesharpercommented, May 9, 2017

You can also disable property injection at the container level

containerOptions.EnablePropertyInjection = false 
1reaction
the-spykecommented, May 9, 2017

@kbiel Thanks for clarification!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection error: Unable to resolve service for ...
I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I ......
Read more >
ASP.Net Core API Error (Unable to resolve service for type ' ...
I built a Web API using ASP.Net Core (from Microsoft Tutorial). When calling the API, I got this error below: The error can...
Read more >
Unable to resolve service for type '' while attempting to ...
What am I missing? What I have tried: I have tried rearranging the order of services in the container but the error still...
Read more >
Unable to resolve service for type while attempting ...
In my ASP.NET Core application, I get the following error: InvalidOperationException: Unable to resolve service for type 'Cities.Models.
Read more >
Unable to resolve service for type while attempting ...
A C# code example that shows how to correct the ASP.NET Core Dependency Injection error: 'Unable to resolve service for type while attempting...
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