Autofac.Integration.Owin

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Autofac.Integration.Owin相关的知识,希望对你有一定的参考价值。

        public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            return app
                .RegisterAutofacLifetimeScopeInjector(container)
                .UseAllMiddlewareRegisteredInContainer(container);
        }
        private static IAppBuilder RegisterAutofacLifetimeScopeInjector(this IAppBuilder app, ILifetimeScope container)
        {
            app.Use(async (context, next) =>
                {
                    using (var lifetimeScope = container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag,
                    b => b.RegisterInstance(context).As<IOwinContext>()))
                    {
                        context.Set(Constants.OwinLifetimeScopeKey, lifetimeScope);
                        await next();
                    }
                });

            app.Properties[InjectorRegisteredKey] = true;
            return app;
        }

 

using System;
using Microsoft.Owin;

namespace Autofac.Integration.Owin
{
    /// <summary>
    /// Extension methods for using Autofac within an OWIN context.
    /// </summary>
    public static class OwinContextExtensions
    {
        /// <summary>
        /// Gets the current Autofac lifetime scope from the OWIN context.
        /// </summary>
        /// <param name="context">The OWIN context.</param>
        /// <returns>The current lifetime scope.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="context" /> is <see langword="null" />.
        /// </exception>
        public static ILifetimeScope GetAutofacLifetimeScope(this IOwinContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            return context.Get<ILifetimeScope>(Constants.OwinLifetimeScopeKey);
        }
    }
}

 

以上是关于Autofac.Integration.Owin的主要内容,如果未能解决你的问题,请参考以下文章

Java分代垃圾回收机制:年轻代/年老代/持久代(转)

R留学生作业代码代写代编程代编程代编程

IPEX-1代/3代/4代/5代,PCB天线底座,公头,样式及封装尺寸图

JVM 年轻代 老年代 持久代 gc

深圳本地网店代运营公司

C线程代业代写代调试POSIX Threads代编码