c# - PerHttpRequestLifeTimeManager for Unity with Web API 和 OWIN

标签 c# asp.net-web-api owin

我有一个 Web 服务,它是使用带有 OWIN 的 Web API 5.23 构建的。我使用 Unity 作为 IOC 容器。

此 Web 服务由每 2 秒触发一次 http 请求的 Windows 服务使用。

windows服务启动时,web服务抛出如下异常:

System.NotSupportedException occurred HResult=-2146233067 Message=A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

据我所知,两个线程正在尝试使用同一个 DbContext 实例,因此在每个请求上创建一个新的 DbContext 应该可以解决我的问题。

查看 LifeTimeManagers Unity 提供的,看来 TransientLifetimeManager 是自

TransientLifetimeManager. For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve or ResolveAll method. This lifetime manager is used by default for all types registered using the RegisterType, method unless you specify a different lifetime manager.

出于某种原因,这不起作用,我仍然遇到相同的异常。

经过更多搜索,我找到了 PerRequestLifetimeManager .但我不能使用它,因为我不想依赖 MVC。当我检查它的实现方式时,我注意到它依赖于 IHttpModule。我不确定我是否可以使用它,因为我正在使用 OWIN 中间件来托管我的网络服务。

如何使用 OWIN 为 Web API 实现 PerRequestLifetimeManager?

详细信息以防我做错了什么:

 public static class MiddlewareContainer
{
    public static void Register(IUnityContainer container)
    {
        container.RegisterType<DbContext, LicenceDbContext>(new TransientLifetimeManager());
        container.RegisterType<IdentityDbContext<IdentityUser>, LicenceDbContext>(new TransientLifetimeManager());

        container.RegisterType<IOAuthAuthorizationServerProvider, LicenceAuthorizationProvider>(new TransientLifetimeManager(),
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<AuthorizationProviderLoggingInterceptionBehavior>());

        container.RegisterType<IDeploymentAuthRepository, DeploymentAuthRepository>(new TransientLifetimeManager());
        container.RegisterType(typeof (UserManager<>), typeof (UserManager<>) ,new TransientLifetimeManager());
        container.RegisterType(typeof (IUserStore<>), typeof (UserStore<>), new TransientLifetimeManager());

        container.RegisterType(typeof(IRepository<>), typeof(Repository<>));
        container.RegisterType<IHttpActionResultProvider, HttpActionResultProvider>();
        container.RegisterType<IClaimsIdentityFactory, ClaimsIdentityFactory>();

        container.RegisterType<LoggingInterceptionBehavior, AuthorizationProviderLoggingInterceptionBehavior>();
        container.RegisterType<ILogger, Logger>();
        container.RegisterType<IIdentityProvider, IdentityProvider>();
        container.RegisterType<IIdentityProviderFactory, IdentityProviderFactory>();
        container.RegisterType<ILogger, Logger>();

        container.RegisterType(typeof (IRepository<>), typeof (Repository<>));
        container.RegisterType<IRepository<UserActivity>, UserActivityRepository>();
        container.RegisterType<IRepository<Licence>, LicenceRepository>();
        container.RegisterType<IJsonConverter, JsonConverter>();
        container.RegisterType<IEqualityComparer<Message>, MessageComparer>();
        container.RegisterType<System.Web.Http.ExceptionHandling.ExceptionLogger, GenericExceptionLogger>();
    }
}

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        var container = new UnityContainer();
        container.AddNewExtension<Interception>();

        MiddlewareContainer.Register(container);
        config.DependencyResolver = new UnityResolver(container);

        ConfigureFilters(config, container);
        ConfigureOAuth(app,container);

        WebApiConfig.Register(config);
        app.UseWebApi(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    }

最佳答案

发现问题。它与 Unity 无关。在 Startup 类中,我正在调用

ConfigureOAuth(app,container);

从问题中发布的代码来看,无法知道这可能是一个问题。 (为此道歉)。

这里是方法的内容。

 public void ConfigureOAuth(IAppBuilder app, UnityContainer container)
    {
        OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromHours(1),
            Provider = container.Resolve<IOAuthAuthorizationServerProvider>()
        };

        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }

在实例化 OAuthAuthorizationServerOptions 时,我还必须实例化一个 Provider

该提供程序是 OWIN 管道的一部分,并且在服务器启动时仅实例化一次。每个传入请求都会命中该提供程序,从而导致上述错误。 (多个请求正在尝试使用相同的 DbContext)。

使 IOAuthAuthorizationServerProvider 线程安全解决了这个问题,但这不是理想的情况。

理想的解决方案是指定一个 providerFactory,该工厂将为每个请求创建一个新的提供者。

关于c# - PerHttpRequestLifeTimeManager for Unity with Web API 和 OWIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37969415/

相关文章:

c# - 如何在不清理请求的情况下从请求中获取主体值(Stream)?

reflection - 如何在新的 VS2013 Asp.net 模板中禁用 System.Security.Permissions.ReflectionPermission 使用,以便它在共享主机上运行

c# - 如何处理 global.asax 中的 session 结束?

c# - 如何在此 asp.NET web api 方法中检索 Ajax header ?

c# - 如何在隐藏和查看密码之间切换 Xamarin.Android

c# - Json Formatting.Indented 使用 Web Api Controller, ASP.NET MVC

c# - 在字符串中将反斜杠转义字符转换为文字

ASP.NET Web API 不允许使用冗长的 base64 URI

azure - 我该怎么做才能将 Katana/Owin 自托管 Web Api 添加为 Azure Web 角色上的服务

asp.net-web-api - ASP.NET Web API - OWIN - TokenEndPointPath 在 IIS 中不起作用