Skip to content

ConfigureContainer(Registry registry) not called #22

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

Closed
CMircea opened this issue Feb 20, 2017 · 8 comments
Closed

ConfigureContainer(Registry registry) not called #22

CMircea opened this issue Feb 20, 2017 · 8 comments

Comments

@CMircea
Copy link
Contributor

CMircea commented Feb 20, 2017

The README says that after calling app.UseStructureMap() the runtime will look for a ConfigureContainer method, however, there isn't any code that actually does this, so the StructureMap.AspNetCore package is kinda useless on its own.

@khellang
Copy link
Member

khellang commented Feb 20, 2017

What are you talking about?

The package adds the UseStructureMap extension method to IWebHostBuilder.

There's no mentioning of app anywhere. The code that makes the hosting layer call Startup.ConfigureContainer is here.

The StructureMap.AspNetCore package isn't useless on its own, it's used together with StructureMap.Microsoft.DependencyInjection to integrate StructureMap with ASP.NET Core. Why would anyone create a useless package?

@CMircea
Copy link
Contributor Author

CMircea commented Feb 20, 2017

Hmm... my bad. I don't get ConfigureContainer called on ASP.NET Core 1.0.1. Digging further into this, support for it was added in ASP.NET Core 1.1.

Here is StartupLoader for 1.0.1: https://github.com/aspnet/Hosting/blob/rel/1.0.1/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs

And here is StartupLoader for 1.1: https://github.com/aspnet/Hosting/blob/rel/1.1.0/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs

For 1.1 we can see on line 18 that it does look for a ConfigureContainer method, while 1.0.1 doesn't have that functionality. The StructureMap.AspNetCore package is useful only for 1.1, but this isn't mentioned anywhere. I checked and even the version isn't suggestive - it's 1.0.0, it should be the same as ASP.NET Core's version IMO (1.1). I have now seen the dependency on Microsoft.AspNetCore.Hosting.Abstractions version 1.1, but that is quite easy to miss.

I suggest documenting that StructureMap.AspNetCore only supports ASP.NET Core 1.1. It works with 1.0 only if you use the overload that takes a Registry, which isn't as useful :)

Sorry for jumping to the conclusion that the package is useful, that was my mistake and I was too harsh.

@khellang
Copy link
Member

Sure, I'll clarify this in the documentation.

It works with 1.0 only if you use the overload that takes a Registry, which isn't as useful

What does that mean?

@CMircea
Copy link
Contributor Author

CMircea commented Feb 21, 2017

It means that in 1.0 you can create the container in any way you fancy and call this overload of AddStructureMap: https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection/blob/master/src/StructureMap.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs#L12

There's no automatic support for ConfigureContainer, so you have to do the equivalent yourself, and there's two ways to integrate StructureMap:

  1. Use the IServiceProvider ConfigureServices(IServiceCollection services) method, and manually return new StructureMapServiceProvider(Container).
  2. Call AddStructureMap(Registry) in Configure(IApplicationBuilder app).

The first option doesn't need the StructureMap.AspNetCore package, but the second does.

I prefer the first option, as I can manually call Populate(services) and then AssertConfigurationIsValid.

@khellang
Copy link
Member

khellang commented Feb 21, 2017

I'm not sure what you're saying. In 1.1, which the library requires, you can, as you say, call either IWebHostBuilder.UseStructureMap, or IServiceCollection.AddStructureMap. These are equivalent. The former requires StructureMap.AspNetCore, the latter doesn't. They will both make the hosting layer call ConfigureContainer(Registry). There should be no way to use this in a 1.0 app.

The second option you have, is to manually, as you've mentioned, return an instance of IServiceProvider from the ConfigureServices(IServiceCollection) method. This works in 1.0 and doesn't require StructureMap.AspNetCore.

I'm not sure what you mean by "call AddStructureMap(Registry) in Configure(IApplicationBuilder). By the time Configure(IApplicationBuilder) is called, it's too late to call anything on the service collection. Where do you even get a service collection in that method?

@CMircea
Copy link
Contributor Author

CMircea commented Feb 21, 2017

My bad, I mixed up IApplicationBuilder with IWebHostBuilder, I meant calling IWebHostBuilder.UseStructureMap.

There should be no way to use this in a 1.0 app.

Sure there is. Add it as a dependency, you will get Microsoft.Extensions.DependencyInjection.Abstractions version 1.1 but all other packages will be 1.0. DependencyInjection 1.1 is backwards compatible to ASP.NET Core 1.0 from what I can tell. IIRC the ASP.NET team tested StructureMap against 1.0 and only the constructor selection was a problem.

@khellang
Copy link
Member

khellang commented Feb 21, 2017

Oh, I see what you're saying now. You get 1.1 of the DI-specific packages, but the hosting layer remains at 1.0 and won't know what to do with the IServiceProviderFactory<T> service. That's exactly right. It's a good point that this should be mentioned in the README.

@CMircea
Copy link
Contributor Author

CMircea commented Feb 21, 2017

Yeah, that's exactly right.

Compatibility with 1.0 should be documented, but mentioned that the StructureMap.AspNetCore package requires ASP.NET Core 1.1 :).

@CMircea CMircea closed this as completed Feb 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants