简体   繁体   中英

Using DryIOC in an OWIN-based ASP.NET WebAPI doesn't work

I have a simple app which uses an OWIN-Startup class to configure itself. BUT, I'm not able to get it to work! Here is my configuration:

public void Configuration(IAppBuilder appBuilder) {

    var httpConfiguration = new HttpConfiguration();

    httpConfiguration.Routes.MapHttpRoute(
          name: "default_api_versioned_by_areas",
          routeTemplate: "api/{area}/{controller}/{action}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

    var container = new Container();

    // registering my services. They're all good....

    container.WithWebApi(
        httpConfiguration, new[] {GetType().Assembly});
    container.RegisterWebApiControllers(
        httpConfiguration, new[] { GetType().Assembly });

    appBuilder.UseDryIocWebApi(httpConfiguration)
        .UseDryIocOwinMiddleware(container);

    appBuilder.UseWebApi(httpConfiguration);

    // I can resolve other services:
    var s = container.Resolve<MyOtherService>();

    // but I cannot resolve controllers:
    var c = container.Resolve<ContentController>();
}

As you can see, MyOtherService is resolvable; But not the ContentController . Also, when I request an API, I get this error:

An error occurred when trying to create a controller of type 'ContentController'. Make sure that the controller has a parameterless public constructor.

Googling doesn't help. Can you help me to figure out how to run the app?

UPDATE:

It seems controllers are not-registered at-all. When I try to resolve the ContentController - which has a default ctor with no-dependency - I get this error:

// code:
var c = container.Resolve<ContentController>();

// exception:

An exception of type 'DryIoc.ContainerException' occurred in DryIoc.dll but was not handled in user code

Additional information: Unable to resolve MyProject.Areas.Alpha.Controllers.ContentController

Where CurrentScope: 

  and ResolutionScope: 

  and Found registrations:

  without matching scope DefaultKey.Of(0),{ID=33, ImplType=MyProject.Areas.Alpha.Controllers.ContentController, Reuse=CurrentScopeReuse {Name="WebRequestScopeName", Lifespan=100}}}

  without matching scope DefaultKey.Of(1),{ID=37, ImplType=MyProject.Areas.Alpha.Controllers.ContentController, Reuse=CurrentScopeReuse {Name="WebRequestScopeName", Lifespan=100}}}

That is a standard error response in Web API when the framework is unable to resolve controller. One of the dependencies of the controller cannot be resolved.

For example, if ContentController was defined as

public class ContentController : ApiController {

    public ContentController(IOtherService service) { ... }

}

and the DependencyResovler does not know how to resolve IOtherService then the above stated error will be displayed.

Make sure that all the dependencies of the controller are registered and can be resolved by the IoC container.

To find exact error why controller is not resolved you may pass throwIfUnresolved parameter to WithWebApi method. By default, resolution of controllers fallbacks to WebApi infrastructure and original exception is swallowed.

container = container.WithWeApi(config, assemblies, throwIfUnresolved: type => type.Name.EndsWith("Controller"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM