Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design: Failure when UseMiddleware(args) is used #10502

Closed
zugzwangm opened this issue Dec 7, 2017 · 11 comments
Closed

Design: Failure when UseMiddleware(args) is used #10502

zugzwangm opened this issue Dec 7, 2017 · 11 comments

Comments

@zugzwangm
Copy link

dotnet ef migrations list (and possibly other commands) fails when Startup.Configure() adds a Middleware with app.UseMiddleware<MyMiddleware>(args). The app works flawlessly while running normally.

Workaround is to avoid using param object[] args when calling UseMiddleware(), and inject everything through DI.

Application startup exception: System.InvalidOperationException: A suitable constructor for type 'MyProject.Web.MasterServer.WebSocketManager.WebSocketManagerMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.MapExtensions.Map(IApplicationBuilder app, PathString pathMatch, Action`1 configuration)
   at MyProject.Web.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory, IAntiforgery antiforgery) in D:\...\Startup.cs:line 234
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
[2017-12-07 02:12:13] crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
                            Application startup exception
System.InvalidOperationException: A suitable constructor for type 'MyProject.Web.MasterServer.WebSocketManager.WebSocketManagerMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.MapExtensions.Map(IApplicationBuilder app, PathString pathMatch, Action`1 configuration)
   at MyProject.Web.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory, IAntiforgery antiforgery) in D:\...\Startup.cs:line 234
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: A suitable constructor for type 'MyProject.Web.MasterServer.WebSocketManager.WebSocketManagerMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
PM> dotnet ef migrations list

Further technical details

EF Core version: 2.0
Operating system: Win10
IDE: Visual Studio 2017 15.4.5

@bricelam
Copy link
Contributor

bricelam commented Dec 8, 2017

Is there an argument it's expecting to be present? Maybe it's specified in launchSettings.json... (see #8695)

@bricelam
Copy link
Contributor

I wonder if this has been mitigated in 2.1 by aspnet/Hosting#1263

@ajcvickers
Copy link
Member

@divega Note: update forward link in message.

@divega
Copy link
Contributor

divega commented Dec 19, 2017

Updated fwlink 851728 to point to https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext which covers all supported patterns for configuring the DbContext at both run time and design time.

I am planning to update the topic to call out more clearly the specific requirements of design time tools.

@hscanlan
Copy link

hscanlan commented Dec 31, 2017

How do you actually fix this problem? I have the exact same thing when trying to migrate. But when I run my app, it works perfectly fine.

The "fix" is just a duplicate link to this thread, so it doesn't show an answer ..

What am I missing here?

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
@bricelam bricelam modified the milestones: 2.1.0, 2.1.0-preview1 Jan 17, 2018
@bricelam bricelam changed the title dotnet ef migrations list (and possibly other commands) fails when UseMiddleware(args) is used Design: Failure when UseMiddleware(args) is used Jan 19, 2018
@bricelam
Copy link
Contributor

@zugzwangm @hscanlan Do you have a repro project I can use to dig into this?

@bricelam
Copy link
Contributor

I'm not able to repro this with the information provided. Here's what I tried:

app.UseMiddleware<MyMiddleware>(new MyDependency());
class MyMiddleware
{
    public MyMiddleware(RequestDelegate next, MyDependency dependency)
    {
    }

    public Task Invoke(HttpContext httpContext)
        => Task.CompletedTask;
}

class MyDependency
{
}

@bricelam bricelam removed this from the 2.1.0-preview1 milestone Jan 22, 2018
@cobrafast
Copy link

I'm stuck with this too.

The only difference I can see, is that I use serviceProvider.GetService<MyDependency>() to retrieve the instance passed to the middleware, because in my case MyDependency has dependencies of its own that I want to have handled by dependency injection (which works fine during runtime).
The service was previously set during ConfigureServices as a singleton.

Perhaps if you change your attempt to the following (which is quite close to what I'm actually using) you can reproduce it (I did not try it this isolated though).

services.AddSingleton(typeof(MyOtherDependency));
services.AddSingleton(typeof(MyDependency));
app.UseMiddleware<MyMiddleware>(serviceProvider.GetService<MyDependency>());
class MyMiddleware { /* (snip) (use your code) */ }
class MyDependency
{
	public MyDependency(MyOtherDependency dep, IServiceScopeFactory ssf)
	{ }
}
class MyOtherDependency { }

@cobrafast
Copy link

cobrafast commented Jan 24, 2018

Seems like a workaround is to comment out the offending app.UseMiddleware(...) call, do the migration, and uncomment back in.

@bricelam
Copy link
Contributor

Still not able to repro it. Let me know if you can isolate the issue.

@ajcvickers
Copy link
Member

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants