WebAPIContrib.Core
WebAPIContrib.Core copied to clipboard
UseBranchWithServices extension method is not working with branched mvc apps.
filip's original sample of the UseBranchWithServices feature doesn't work any longer when using the extension method from WebApiContrib.Core Following sample clearly illustrates/reproduces the problem. https://github.com/paulvanbladel/aspnetcore-parallel-pipelines
This is a bug in controller discovery.
You could workaround it by explicitly adding your assembly to MVC controller discovery (AddApplicationPart(typeof(MyApiController).Assembly);
).
app.UseBranchWithServices("/api",
s =>
{
s.AddTransient<IHiService, ResourceService>();
s.AddMvc().ConfigureApplicationPartManager(manager =>
{
manager.FeatureProviders.Clear();
manager.FeatureProviders.Add(new TypedControllerFeatureProvider<MyApiController>());
}).AddApplicationPart(typeof(MyApiController).Assembly);
},
a =>
{
a.UseMvc();
});
An even more elegant version would be:
public static class MvcCoreBuilderExtensions
{
public static IMvcBuilder AddSpecificControllersOnly<TController>(this IMvcBuilder builder) where TController : ControllerBase
{
return builder.ConfigureApplicationPartManager(manager =>
{
manager.FeatureProviders.Clear();
manager.FeatureProviders.Add(new TypedControllerFeatureProvider<TController>());
}).AddApplicationPart(typeof(TController).Assembly);
}
public static IMvcCoreBuilder AddSpecificControllersOnly<TController>(this IMvcCoreBuilder builder) where TController : ControllerBase
{
return builder.ConfigureApplicationPartManager(manager =>
{
manager.FeatureProviders.Clear();
manager.FeatureProviders.Add(new TypedControllerFeatureProvider<TController>());
}).AddApplicationPart(typeof(TController).Assembly);
}
}
And then you can just do:
app.UseBranchWithServices("/api",
s =>
{
s.AddTransient<IHiService, ResourceService>();
s.AddMvc().AddSpecificControllersOnly<MyApiController>();
},
a =>
{
a.UseMvc();
});
Hey Filip, That's a great workaround. Thanks a lot. While it solves the issue in the project itself, the integration test project (making use of TestHost) cannot resolve services correctly when using UseBranchWithServices(). There is a second git branch called unittest-problem in https://github.com/paulvanbladel/aspnetcore-parallel-pipelines, which demonstrates the problem. Cheers paul.
The other scenario you have:
[Fact]
public void WeHaveAProblemWhenResolvingServices()
{
var result = _server.Host.Services.GetService(typeof(IHiService));
Assert.NotNull(result);
}
doesn't work by design. Each branch has its own dedicated service provider which is different from the global service provider (that the test host surfaces).
Sure, I see. Thanks a lot.
Should this solution work if the controllers are being loaded from external assemblies?
yes
I should have left an example.
I created a sample app with the following structure
The contents of the files can be seen at: https://gist.github.com/amcorch/0173b2752c97269b7366bb5c920766b7
Every request I make to http://localhost:5000/resource or http://localhost:5000/admindata results in a 404 error. I'm surely missing something obvious which is causing a problem.
Any help is appreciated.
Hi @filipw , Can I customize to have view of controller in branch?