castleproject / Windsor

Castle Windsor is a best of breed, mature Inversion of Control container available for .NET
http://www.castleproject.org
Apache License 2.0
1.51k stars 457 forks source link

ASP.NET Core 6: WebApplicationBuilder Example? #607

Open sianabanana opened 2 years ago

sianabanana commented 2 years ago

Are there any examples of how to use Castle Windsor with the new .net 6 Core minimal style

This site has a autofac example https://andrewlock.net/exploring-dotnet-6-part-10-new-dependency-injection-features-in-dotnet-6/

var builder = WebApplication.CreateBuilder(args);

// Call UseServiceProviderFactory on the Host sub property 
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

// Call ConfigureContainer on the Host sub property 
builder.Host.ConfigureContainer<ContainerBuilder>(builder => 
{
    builder.RegisterModule(new MyApplicationModule());
});

var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

Is there a simmilar example for castle? i cant find a castle service provider factory for example.

Previously i was using the Castle.Windsor.MsDependencyInjection WindsorRegistrationHelper.CreateServiceProvider(container, services);

generik0 commented 2 years ago

Yes, MS are irritating, changing this all the time. I will try to fix this up ASAP

generik0 commented 2 years ago

Wait, with the latest version, doesn't this work? builder.Host.UseServiceProviderFactory(new WindsorServiceProviderFactory());

sianmace commented 2 years ago

Yes, sorry for not replying but i eventually found the solution. As you say

builder.Host.UseServiceProviderFactory(new WindsorServiceProviderFactory());

then i do

builder.Host.ConfigureContainer<WindsorContainer>((hostBuilderContext, container) =>
{
    container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));

etc.

Thanks for getting back to me

sianmace commented 2 years ago

It would probs be useful to add an example to the library to save other peoples pain.