gpt4 book ai didi

asp.net-mvc - 使用 Unity 错误的 ASP.NET Web API 中的依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-04 04:57:10 26 4
gpt4 key购买 nike

我正在努力关注 this blog用于使用 Unity 实现依赖注入(inject),但我在 boostrapper.cs 中遇到错误(我使用 Unity.MVC4 库)。有人可以指出我在这里缺少什么吗?

ERROR: IoCContainer does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator

 public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

// this throws "does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.... error"
DependencyResolver.SetResolver(new IoCContainer(container));

return container;
}

....
}

这是 IoCContainer 类

public class ScopeContainer : IDependencyScope
{
protected IUnityContainer container;

public ScopeContainer(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}

public object GetService(Type serviceType)
{
if (container.IsRegistered(serviceType))
{
return container.Resolve(serviceType);
}
else
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
if (container.IsRegistered(serviceType))
{
return container.ResolveAll(serviceType);
}
else
{
return new List<object>();
}
}

public void Dispose()
{
container.Dispose();
}
}

class IoCContainer : ScopeContainer, IDependencyResolver
{
public IoCContainer(IUnityContainer container)
: base(container)
{
}

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new ScopeContainer(child);
}
}

额外的!我在 app_start 中调用 Bootstrap ,我不确定它是否正确

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Bootstrapper.Initialise();
}

我也觉得它可能与路由有关,我实际上在配置中添加了经典的 MVC 路由,因为我读到 webapi 和 mvc 使用不同的“激活器”之类的东西

 public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

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

最佳答案

找到答案,好像博客已经过时了,因为我什至不必创建 IoCContainer,因为我只需要安装 Unity.WebAPI 并设置 DependencyResolvernew Unity.WebApi.UnityDependencyResolver(container);

public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

return container;
}

来源:http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html

关于asp.net-mvc - 使用 Unity 错误的 ASP.NET Web API 中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com