1

我一直在在线搜索和调试,重新阅读dryioc的文档并查看示例。我认为我的问题不在于dryioc,或者我忽略了一个愚蠢的错误。

项目设置 => 服务 => owin + webapi2 使用属性路由、swagger 和 apiversioning

这就是我设置容器的方式

private IContainer SetupDependencyInjection(HttpConfiguration config)
{
        var container = new Container();
        container.WithWebApi(config, throwIfUnresolved: type => typeIsController());
        container.RegisterWebApiControllers(config);
        container.Register<IObjectFactory, ObjectFactory>(Reuse.Singleton);

        RegisterStoreServices(container);
        RegisterMetadata(container);

        var validate = container.Validate();
        var verify = container.VerifyResolutions();
        if (validate.Any() || verify.Any())
        {
            throw new Exception("Error occured while setting up service dependencies.");
        }

        using (var c = container.OpenScope(Reuse.WebRequestScopeName))
        {
            var songController = c.Resolve<Musify.WebApi.Controllers.v1._0.SongsController>();
            var GenresController = c.Resolve<Musify.WebApi.Controllers.v1._0.GenresController>();
        }
        return container;
}

RegisterStoreServices 和 RegisterMetadata 都只包含单例或临时注册。

当我尝试在我的 API 控制器上调用控制器方法时,我收到以下错误

    {
  "message": "An error has occurred.",
  "exceptionMessage": "An error occurred when trying to create a controller of type 'SongsController'. Make sure that the controller has a parameterless public constructor.",
  "exceptionType": "System.InvalidOperationException",
  "stackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
  "innerException": {
    "message": "An error has occurred.",
    "exceptionMessage": "Unable to resolve Musify.Api.Service.Data.ISongRepository as parameter \"songRepository\"\r\n  in scoped Musify.WebApi.Controllers.v1._0.SongsController.\r\nWhere no service registrations found\r\n  and number of Rules.FallbackContainers: 0\r\n  and number of Rules.UnknownServiceResolvers: 0",
    "exceptionType": "DryIoc.ContainerException",
    "stackTrace": "   at DryIoc.Throw.It(Int32 error, Object arg0, Object arg1, Object arg2, Object arg3)\r\n   at DryIoc.Container.ThrowUnableToResolve(Request request)\r\n   at DryIoc.Container.DryIoc.IContainer.ResolveFactory(Request request)\r\n   at DryIoc.ReflectionFactory.CreateExpressionOrDefault(Request request)\r\n   at DryIoc.Factory.GetExpressionOrDefault(Request request)\r\n   at DryIoc.Factory.GetDelegateOrDefault(Request request)\r\n   at DryIoc.Container.ResolveAndCacheDefaultDelegate(Type serviceType, Boolean ifUnresolvedReturnDefault, IScope scope)\r\n   at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, Boolean ifUnresolvedReturnDefault)\r\n   at DryIoc.WebApi.DryIocDependencyScope.GetService(Type serviceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
  }
    }

我知道在很多情况下,这是因为服务没有注册,但我注册了。

container.Register<ISongRepository, SongRepository>(Reuse.Singleton);

正如您在第一个代码片段中看到的那样,我验证、验证,甚至尝试了 container.openscope,以查看容器是否已注册。在容器上,我可以看到歌曲存储库也被填充。

所以我猜,不知何故,webapi 不使用注入容器的控制器。

我在依赖项设置中做错了吗?

public void Configuration(IAppBuilder appBuilder)
    {
        SetupLogging();

        var config = new HttpConfiguration();
        SetupRouting(config);
        SetupVersioning(config);
        SetupJson(config);
        SetupDocumentation(config);
        SetupDependencyInjection(config);
        appBuilder.UseWebApi(config);
        appBuilder.UseErrorPage(ErrorPageOptions.ShowAll);
    }

我不确定这是否重要,但是在配置完其他所有内容之后,我正在设置容器。

4

1 回答 1

1

经过一番折腾,我在添加标有 //this 的行后让它工作了

var container = SetupDependencyInjection(config);
        appBuilder.UseDryIocOwinMiddleware(container); //this
        appBuilder.UseWebApi(config);
        appBuilder.UseDryIocWebApi(config); //this

不过,如果有人能给我一个,我想要一个解释。因为在另一篇文章中,我读到有人因为同时使用 UseWebApi 和 UseDryIocWebApi 而遇到问题。

但如果没有 OwinMiddleware,它仍然无法工作。那么这两个功能是做什么的呢?

还是我使用了“错误的修复”?

于 2017-11-21T23:27:49.407 回答