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);
        }
    }
}
时间: 2024-12-23 23:13:49

Autofac.Integration.Owin的相关文章

Autofac.Integration.Mvc.Owin分析

using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Security; using System.Web; using Autofac; using Autofac.Integration.Owin; namespace Owin { /// <summary> /// Extension methods for configuring the OWIN p

Autofac.Integration.Mvc分析

Autofac.Integration.Mvc static ILifetimeScope LifetimeScope { get { return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)]; } set { HttpContext.Current.Items[typeof(ILifetimeScope)] = value; } } namespace Autofac.Integration.Mvc { /

Autofac.Integration.Web分析

using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// <summary> /// Provides application-wide and per-request containers. /// </summary> public class ContainerProvider : IContainerProvider { reado

autofac + owin + webform + mvc + webapi集成demo

http://git.oschina.net/shiningrise/AutofacOwinDemo using Microsoft.Owin; using Owin; using System.Web.Mvc; using Autofac; using Autofac.Integration.Owin; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi; using System.Web.Http; using Sy

Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析

主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Integration.Web Autofac.Integration.WebApi.Owin Autofac.Integration.Mvc.Owin 地址:https://github.com/autofac 以上几个项目分别是 Mvc Webapi Owin 和 webform 与autofac的整

要引用这几个才有GetOwinContext与GetAutofacLifetimeScope

using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; var owin = this.Request.GetOwinContext(); var scop = owin.GetAutofacLifetimeScope(); scop.ResolveOptional()

OWIN support for the Web API 2 and MVC 5 integrations in Autofac

Currently, in the both the Web API and MVC frameworks, dependency injection support does not come into play until after the OWIN pipeline has started executing. This is simply a result of the OWIN support being added to both frameworks after their in

【翻译Autofac的帮助文档】1.入门指南

[写在前面]尝试做完一件工作之外自我觉得有意义的一件事,那就从翻译Autofac的帮助文档吧. 入门指南 将Autofac集成你的应用程序的步骤通常很简单,一般是: 时刻以IOC(控制反转)的思想来规划你的应用程序 在你的Porject中添加Autofac引用 按照如下步骤设计应用程序的启动环节 创建一个ContainerBuilder 向ContainerBuilder注册组件 通过ContainerBuilder的Build()方法获得Container(后续需用到) 在应用程序运行环节时,

Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现

一.前言 由于项目业务复杂,创建了多个插件并把他们放在了不同的项目中,项目使用AutoFac做的IOC:但是主项目可以注入,插件注入失败, 没有为该对象定义无参数的构造函数.下面就一步一步注入插件项目. 二.新建带有插件的项目 参考: ASP.NET MVC5 插件化机制简单实现 项目结构如下图: 三.建立DomainServices类库 新建一个ITestService接口,代码如下: namespace DomainServices { public interface ITestServi