dryioc controller.service 在设置期间解决,而不是在 api 调用期间解决
dryioc controller.service is resolved during setup, not during api call
我一直在网上搜索和调试,重新阅读 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);
}
我不确定这是否重要,但我正在设置容器,在配置完其他所有内容之后。
经过一番折腾后,我添加了标有 //this
的行后它开始工作了
var container = SetupDependencyInjection(config);
appBuilder.UseDryIocOwinMiddleware(container); //this
appBuilder.UseWebApi(config);
appBuilder.UseDryIocWebApi(config); //this
不过,如果有人可以给我一个解释,我想要一个解释。
因为在另一个 post 上,我读到有人因为同时使用 UseWebApi 和 UseDryIocWebApi 而遇到问题。
但是如果没有 OwinMiddleware,它仍然无法工作。那么这两个功能有什么作用呢?
还是我用了"wrong fix"?
我一直在网上搜索和调试,重新阅读 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);
}
我不确定这是否重要,但我正在设置容器,在配置完其他所有内容之后。
经过一番折腾后,我添加了标有 //this
的行后它开始工作了var container = SetupDependencyInjection(config);
appBuilder.UseDryIocOwinMiddleware(container); //this
appBuilder.UseWebApi(config);
appBuilder.UseDryIocWebApi(config); //this
不过,如果有人可以给我一个解释,我想要一个解释。 因为在另一个 post 上,我读到有人因为同时使用 UseWebApi 和 UseDryIocWebApi 而遇到问题。
但是如果没有 OwinMiddleware,它仍然无法工作。那么这两个功能有什么作用呢?
还是我用了"wrong fix"?