我正在使用 web api MVC 4 创建一个简单的 web 服务,我使用 Unity web api 作为我的 IOC,但是在文档中它说将它添加到 application_start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Bootstrapper.Initialise();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
这意味着对于每个请求都会创建它
public static void Initialise()
{
_container = BuildUnityContainer();
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(_container);
}
private static IUnityContainer BuildUnityContainer()
{
_container = new UnityContainer();
// register all your components with the container here
LoggingUtilities.LoadConfigFile("ThirdParty.log4net");
ILog log = LoggingUtilities.DefaultLogger;
_container.RegisterInstance<ILog>(log);
log.InfoFormat("In BuildUnityContainer");
OpClientWrapper wrapper = new OpClientWrapper(log);
_container.RegisterInstance<OpClientWrapper>(wrapper, new ContainerControlledLifetimeManager());
return _container;
}
我希望我的 Opclient 包装器只为所有请求创建一次,这可能吗?