AspNetDependencyInjection icon indicating copy to clipboard operation
AspNetDependencyInjection copied to clipboard

Mvc Resolver is not "attaching" as expected.

Open djseng opened this issue 5 years ago • 12 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 */ }
}

djseng avatar Sep 04 '20 15:09 djseng

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

djseng avatar Sep 04 '20 16:09 djseng

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.

daiplusplus avatar Sep 18 '20 12:09 daiplusplus

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.

watfordgnf avatar Apr 14 '21 19:04 watfordgnf

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

watfordgnf avatar Apr 14 '21 20:04 watfordgnf

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

StandBackBurrito avatar May 25 '21 00:05 StandBackBurrito

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

daiplusplus avatar May 25 '21 00:05 daiplusplus

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

daiplusplus avatar May 25 '21 00:05 daiplusplus

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

StandBackBurrito avatar May 25 '21 00:05 StandBackBurrito

@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

StandBackBurrito avatar May 25 '21 17:05 StandBackBurrito

@djseng

@StandBackBurrito

I've just published version 5.0.0-beta01 which I think fixes this issue - can you verify that at your end?

https://www.nuget.org/packages/Jehoel.AspNetDependencyInjection/5.0.0-beta01

https://www.nuget.org/packages/Jehoel.AspNetDependencyInjection.Mvc/5.0.0-beta01

daiplusplus avatar Jul 19 '21 04:07 daiplusplus

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!

mohaaron avatar Nov 12 '21 19:11 mohaaron

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?

pl-jcarter avatar Jan 23 '23 16:01 pl-jcarter