我正在使用Owin,Web API,实体框架,ASP.NET身份创建API.我使用Simple Injector作为我选择的DI框架.
在Owin启动过程中,我想用一些示例数据为我的数据库设定种子.这由一个实现类处理IDatabaseInitializer,看起来像这样:
public class MyDbInitializer : DropCreateDatabaseAlways<MyDataContext>
{
private readonly IUserManager _userManager;
public MyDbInitializer(IUserManager userManager)
{
_userManager = userManager;
}
protected override void Seed(MyDataContext context)
{
SeedIdentities();
}
private void SeedIdentities()
{
var user = new User
{
UserName = "someUsername",
Email = "some@email.com"
};
_userManager.CreateAsync(user, "Password");
}
Run Code Online (Sandbox Code Playgroud)
IUserManager是ASP.NET Identiy UserManager类的代理,它间接依赖于IUnitOfWork.如果你想知道,IUserManager注册如下:
container.Register(typeof(IUserManager),
() => container.GetInstance<IUserManagerFactory>().Create());
Run Code Online (Sandbox Code Playgroud)
因为我想根据Web API请求使用单个工作单元,所以我已经注册了IUnitOfWork以下内容:
container.RegisterWebApiRequest<IUnitOfWork, MyUnitOfWork>();
Run Code Online (Sandbox Code Playgroud)
除了解析类中的IUserManager依赖项之外,这对于所有事情都很好MyDbInitializer.在应用程序启动期间,SimpleInjector失败并出现以下ActivationException:
SimpleInjector.ActivationException was …Run Code Online (Sandbox Code Playgroud) c# dependency-injection simple-injector asp.net-web-api owin
我正在转换现有的ASP .Net Web API 2项目以使用OWIN.该项目使用Castle Windsor作为依赖注入框架,其中一个依赖项设置为使用PerWebRequest生活方式.
当我向服务器发出请求时,我得到一个Castle.MicroKernel.ComponentResolutionException例外.该异常建议将以下内容添加到配置文件中的system.web/httpModules和system.WebServer/modules部分:
<add name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
Run Code Online (Sandbox Code Playgroud)
这不能解决错误.
从SimpleInjector的OWIN集成提供的示例中获取灵感,我尝试使用以下方法在OWIN启动类中设置范围(以及更新依赖关系的生活方式):
appBuilder.User(async (context, next) =>
{
using (config.DependencyResolver.BeginScope()){
{
await next();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这也没有用.
我如何使用Castle Windsor的PerWebRequest生活方式或在OWIN中模拟它?
大约一年前,在Visual Studio中创建时自动生成的MVC项目不包含任何关于OWIN的内容.作为再次申请的人,试图了解变化,我想知道OWIN是否可以取代我的DI.
根据我的理解,Startup.Auth.cs中的以下内容集中了用户管理器的创建(处理身份),以及为应用程序创建数据库连接.
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Other things...
}
}
Run Code Online (Sandbox Code Playgroud)
从一个非常有用的来源:http://blogs.msdn.com/b/webdev/archive/2014/02/12/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity. aspx,看起来好像我们可以随时使用以下代码访问用户管理器或dbcontext
public class AccountController : Controller
{
private ApplicationUserManager _userManager;
public AccountController() { }
public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
public ApplicationUserManager UserManager {
get
{
// HttpContext.GetOwinContext().Get<ApplicationDbContext>(); …Run Code Online (Sandbox Code Playgroud) 我目前正在使用WebApiRequestLifestyle具有默认的范围生活方式.我想在OWIN中间件和其中一个API控制器中注入一个服务,服务的范围应该仍然是WebAPI,即对于整个请求,应该只有一个服务实例.
public class TestMiddleware : OwinMiddleware
{
private readonly ITestService _testService;
public TestMiddleware(OwinMiddleware next, ITestService testService) : base(next)
{
_testService = testService;
}
public override async Task Invoke(IOwinContext context)
{
var test = _testService.DoSomething();
await Next.Invoke(context);
}
}
public class ValuesController : ApiController
{
private readonly ITestService _testService;
public ValuesController(ITestService testService)
{
_testService = testService;
}
}
Run Code Online (Sandbox Code Playgroud)
整个请求的ITestService实例应该相同.我该如何注册中间件?
这就是我现在这样做的方式:
using (container.BeginExecutionContextScope())
{
var testService = container.GetInstance<ITestService>();
app.Use<TestMiddleware>(testService);
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是 - 在注册期间为中间件创建一个ITestService实例并永久保留(如单例),并且对于每个webapi请求,都会在控制器之间创建和共享新实例(webapi范围)
请不要指出这些问题 - WebApi + Simple Injector + OWIN
c# dependency-injection simple-injector asp.net-web-api owin