WebAPIContrib.Core icon indicating copy to clipboard operation
WebAPIContrib.Core copied to clipboard

UseBranchWithServices extension method is not working with branched mvc apps.

Open paulvanbladel opened this issue 6 years ago • 9 comments

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

paulvanbladel avatar Mar 07 '18 10:03 paulvanbladel

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();
                });

filipw avatar Mar 07 '18 17:03 filipw

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();
                });

filipw avatar Mar 07 '18 17:03 filipw

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.

paulvanbladel avatar Mar 08 '18 07:03 paulvanbladel

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).

filipw avatar Mar 08 '18 07:03 filipw

Sure, I see. Thanks a lot.

paulvanbladel avatar Mar 09 '18 07:03 paulvanbladel

Should this solution work if the controllers are being loaded from external assemblies?

amcorch avatar Aug 29 '18 19:08 amcorch

yes

filipw avatar Aug 29 '18 20:08 filipw

I should have left an example.

I created a sample app with the following structure image

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.

amcorch avatar Aug 29 '18 20:08 amcorch

Hi @filipw , Can I customize to have view of controller in branch?

huanbd avatar Oct 30 '18 04:10 huanbd