WebApi 2 - 无法用结构图确定 DataProtection.IDataProtector 依赖注入
WebApi 2 -cant determined DataProtection.IDataProtector Dependency Injection with structuremap
我正在使用 this beriliant 项目作为我的 MVC 项目的基础。
但是当我将 WebAPI 用于项目时,IDataProtector
注入出现问题。
我重新设计了 base 和 upload here,并添加了一个用于测试 WebAPI 授权的控制台项目。
这是结构图初始化:
private static readonly Lazy<Container> _containerBuilder =
new Lazy<Container>(initStructureMap, LazyThreadSafetyMode.ExecutionAndPublication);
public static IContainer Container
{
get { return _containerBuilder.Value; }
}
return new Container(ioc =>
{
ioc.For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new DbContext());
ioc.For<IDataSerializer<AuthenticationTicket>>().Use<TicketSerializer>();
ioc.For<ISecureDataFormat<AuthenticationTicket>>().Use<SecureDataFormat<AuthenticationTicket>>();
});
而在WebApiConfig中classDI是这样的:
var container = StructuremapMvc.Container;
GlobalConfiguration.Configuration.Services.Replace(
typeof(IHttpControllerActivator), new StructureMapHttpControllerActivator(container));
在我的初创公司中,我使用 IAppBuilder 创建数据保护器:
public void ConfigureAuth(IAppBuilder app)
{
StructuremapMvc.Container.Configure(config =>
{
config.For<IDataProtectionProvider>()
.HybridHttpOrThreadLocalScoped()
.Use(() => app.GetDataProtectionProvider());
});
}
它在WebApiConfig 和IDataProtection 在WebApi 中不起作用后启动。我的 ServiceLayer 在单独的项目中,DataProtection 需要在那里注入。
您需要在容器中显式注册 IDataProtector
的实现。
示例配置可能如下所示:
ioc.For<IDataProtector>().Use(() => new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
ioc.For<ITextEncoder>().Use<Base64UrlTextEncoder>();
请记住,此特定配置可能不适合您的确切需求。
希望对您有所帮助!
您需要在项目中添加两个 class :
1-StructureMapDependencyScopeClass
using StructureMap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
namespace Project.Helpers
{
public class StructureMapDependencyScope : IDependencyScope
{
protected readonly IContainer Container;
public StructureMapDependencyScope(IContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.Container = container;
}
public void Dispose()
{
this.Container.Dispose();
}
public object GetService(Type serviceType)
{
if (serviceType == null)
{
return null;
}
try
{
return serviceType.IsAbstract || serviceType.IsInterface
? this.Container.TryGetInstance(serviceType)
: this.Container.GetInstance(serviceType);
}
catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.Container.GetAllInstances(serviceType).Cast<object>();
}
}
}
2-StructureMapDependencyResolver Class
using StructureMap;
using System.Web.Http.Dependencies;
namespace Project.Helpers
{
public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container)
: base(container)
{
}
public IDependencyScope BeginScope()
{
IContainer child = this.Container.GetNestedContainer();
return new StructureMapDependencyResolver(child);
}
}
}
最后在 WebApiConfig Class:
中将此代码替换为您的代码
// IoC Config
var container = SmObjectFactory.Container;
// Web API configuration and services
config.DependencyResolver = new StructureMapDependencyResolver(container);
通过 StructureMap here.
阅读更多关于 ASP.NET MVC 4 和 Web API 中的依赖注入
我正在使用 this beriliant 项目作为我的 MVC 项目的基础。
但是当我将 WebAPI 用于项目时,IDataProtector
注入出现问题。
我重新设计了 base 和 upload here,并添加了一个用于测试 WebAPI 授权的控制台项目。
这是结构图初始化:
private static readonly Lazy<Container> _containerBuilder =
new Lazy<Container>(initStructureMap, LazyThreadSafetyMode.ExecutionAndPublication);
public static IContainer Container
{
get { return _containerBuilder.Value; }
}
return new Container(ioc =>
{
ioc.For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new DbContext());
ioc.For<IDataSerializer<AuthenticationTicket>>().Use<TicketSerializer>();
ioc.For<ISecureDataFormat<AuthenticationTicket>>().Use<SecureDataFormat<AuthenticationTicket>>();
});
而在WebApiConfig中classDI是这样的:
var container = StructuremapMvc.Container;
GlobalConfiguration.Configuration.Services.Replace(
typeof(IHttpControllerActivator), new StructureMapHttpControllerActivator(container));
在我的初创公司中,我使用 IAppBuilder 创建数据保护器:
public void ConfigureAuth(IAppBuilder app)
{
StructuremapMvc.Container.Configure(config =>
{
config.For<IDataProtectionProvider>()
.HybridHttpOrThreadLocalScoped()
.Use(() => app.GetDataProtectionProvider());
});
}
它在WebApiConfig 和IDataProtection 在WebApi 中不起作用后启动。我的 ServiceLayer 在单独的项目中,DataProtection 需要在那里注入。
您需要在容器中显式注册 IDataProtector
的实现。
示例配置可能如下所示:
ioc.For<IDataProtector>().Use(() => new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
ioc.For<ITextEncoder>().Use<Base64UrlTextEncoder>();
请记住,此特定配置可能不适合您的确切需求。
希望对您有所帮助!
您需要在项目中添加两个 class :
1-StructureMapDependencyScopeClass
using StructureMap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
namespace Project.Helpers
{
public class StructureMapDependencyScope : IDependencyScope
{
protected readonly IContainer Container;
public StructureMapDependencyScope(IContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.Container = container;
}
public void Dispose()
{
this.Container.Dispose();
}
public object GetService(Type serviceType)
{
if (serviceType == null)
{
return null;
}
try
{
return serviceType.IsAbstract || serviceType.IsInterface
? this.Container.TryGetInstance(serviceType)
: this.Container.GetInstance(serviceType);
}
catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.Container.GetAllInstances(serviceType).Cast<object>();
}
}
}
2-StructureMapDependencyResolver Class
using StructureMap;
using System.Web.Http.Dependencies;
namespace Project.Helpers
{
public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container)
: base(container)
{
}
public IDependencyScope BeginScope()
{
IContainer child = this.Container.GetNestedContainer();
return new StructureMapDependencyResolver(child);
}
}
}
最后在 WebApiConfig Class:
中将此代码替换为您的代码// IoC Config
var container = SmObjectFactory.Container;
// Web API configuration and services
config.DependencyResolver = new StructureMapDependencyResolver(container);
通过 StructureMap here.
阅读更多关于 ASP.NET MVC 4 和 Web API 中的依赖注入