我一直在搜索,在线和调试,重新阅读dryioc的文档并查看样本。我不认为我的问题在于干旱,或者我可能忽略了一个愚蠢的错误。
项目设置=> service => owin + webapi2 使用attributerouting,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);
}
我不确定这是否重要,但在配置完所有其他内容后,我将设置容器。
答案 0 :(得分:1)
经过一番乱搞后,我在添加标有// this
的行后才开始工作var container = SetupDependencyInjection(config);
appBuilder.UseDryIocOwinMiddleware(container); //this
appBuilder.UseWebApi(config);
appBuilder.UseDryIocWebApi(config); //this
如果有人能给我一个,我想要一个表达。 因为在另一篇文章中,我读到一个人因使用UseWebApi和UseDryIocWebApi而遇到问题。
但是如果没有OwinMiddleware,它仍然无法正常工作。那么这两个功能都能实现什么呢?
或者我使用了“错误修复”?