daiplusplus / AspNetDependencyInjection

`AspNetDependencyInjection` allows "Classic" ASP.NET Web Forms applications to use `Microsoft.Extensions.DependencyInjection` to compose application services using dependency injection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mvc Resolver is not "attaching" as expected.

djseng opened this issue · comments

I tried the AddMvcDependencyResolver as demonstrated in the GETTING_STARTED.md but it does not appear to work as expected.

I added the Mvc Package (have been using the base fine in this web forms project for a couple of weeks now).

    public static class ApplicationStart
    {
        private static ApplicationDependencyInjection resolver;

        public static void PreStart()
        {
            resolver = new ApplicationDependencyInjectionBuilder()
                .ConfigureServices(ConfigureServices)
                .AddMvcDependencyResolver()
                .Build();

            Debug.WriteLine("CurrentResolver: " + 
                System.Web.Mvc.DependencyResolver.Current.GetType().FullName); // <-- this outputs the type for the default mvc resolver.
        }

        private static void ConfigureServices(IServiceCollection services) { /* unrelated */ }
}

I think the DependencyInjectionMvcDependencyResolver ctor is missing a call to DependencyResolver.SetResolver( this );

commented

Hmm, good catch!

I do remember my unit and integration tests for ASP.NET MVC passing though - I need to find out why those tests were passing when something like this should surely mean they shouldn't pass, hmm.

I'll report back soon.

It seems the same is true for WebApi. Nothing is setting up the DependencyResolver for that client, and I can't quite get it out of the injected space.

I've added the fix for WebAPI support in #25.

We are running into the same issue for WebAPI, any updates on this?

commented

Do any of you have a project that reproduces the issue that you can put in an emailed zip or github gist or repo?

commented

@StandBackBurrito ASP.NET Web API uses a different mini-resolver class than ASP.NET MVC. The code in this thread is specific to ASP.NET MVC. Can you explain how your ASP.NET Web API project is using ASP.NET MVC's resolver?

  • ASP.NET MVC uses System.Web.Mvc.IDependencyResolver.
  • ASP.NET Web API uses System.Web.Http.Dependencies.IDependencyResolver.

I was responding to @watfordgnf comment and the changes he made in #25.
Let me see if I can create a minimal repro.

@Jehoel I have created a project setup the same way ours is. It has a VB webforms app with some webapi controllers.
The DI setup is done in a C# in another project.

https://github.com/StandBackBurrito/WebApiDIRepro

Let me know if you have any questions
thanks

Hey all,

Was this ever resolved? I have added and configured this package in an ASP.NET Web Forms project and all is good, but I just added MVC and now get this error when executing a controller.

"No parameterless constructor defined for this object"

UPDATE

I just installed 5.0.0-beta02 and my MVC controller injection is working. Good work and thank you!

I've been testing 5.0.0-beta02 in a .net 4.7.2 project and MVC controller injection is working.
@daiplusplus Any plans to publish a release version without the beta tag?