-
Notifications
You must be signed in to change notification settings - Fork 458
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
Castle.Windsor.Extensions.DependencyInjection No Scope Available #595
Comments
I have related issue: I use IHostedService. all services registered with ConfigureContainer extension of IHostBuilder.
scope.ServiceProvider.GetService<> was throw No Scope. if i replace IServiceScopeFactory IWindsorContainer all works perfectly. |
I was able to come up with a work around, but I think it's a bit of a hack and I'm unsure how it will effect releasing scoped resources. The below creates a class that derives from namespace WebApplication1
{
public class Global : System.Web.HttpApplication
{
public static WindsorServiceProviderFactoryCustom ServiceProviderFactory;
protected void Application_Start(object sender, EventArgs e)
{
Console.WriteLine("Hello World!");
var container = new WindsorContainer();
ServiceProviderFactory = new WindsorServiceProviderFactoryCustom(container);
var host = new HostBuilder()
.UseWindsorContainerServiceProvider(ServiceProviderFactory)
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<SomeService>();
})
.Build();
//Note: This works great
var resolved = ServiceProviderFactory.Container.Resolve<SomeService>();
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
public class SomeService
{
}
public class HttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//This fixes the error, but I am too unfamiliar with the internals of this library to understand if this is a reasonable workaround. Will the component be scoped to `scope` below?
Global.ServiceProviderFactory.CreateRootScope();
using (var scope = Global.ServiceProviderFactory.Container.BeginScope())
{
//this no longer errors, but at what cost.
var someService = Global.ServiceProviderFactory.Container.Resolve<SomeService>();
}
}
public bool IsReusable => false;
}
public sealed class WindsorServiceProviderFactoryCustom : WindsorServiceProviderFactoryBase
{
public WindsorServiceProviderFactoryCustom(IWindsorContainer container)
{
CreateRootScope();
SetRootContainer(container);
}
public new void CreateRootScope()
{
base.CreateRootScope();
}
}
} |
I think this has been fixed as I think the cleanest way to do it though is:
|
I have this issue with the latest versions of var builder = WebApplication.CreateBuilder();
var container = new WindsorContainer();
builder.Host.UseWindsorContainerServiceProvider(container); And when running my server (with gRPC services, and a Yarp reverse proxy configured), I get this error:
I have checked the method Any idea? 😄 Addendum: The exact same code used to work with the previous version / package |
I am using this nuget package to configure Castle Windsor as the DI container. https://www.nuget.org/packages/Castle.Windsor.Extensions.DependencyInjection/
This works fine except for when I try to resolve scoped classes with an IHttpHandler. I get the error: InvalidOperationException: No scope available
What is the correct way to create scope for an HttpHandler?
Here's a sample application reproducing the issue:
Web.config:
The text was updated successfully, but these errors were encountered: