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的更多相关文章

  1. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  2. integration asp.net web api with autofac and owin

    There is an example project showing Web API in conjunction with OWIN self hosting https://github.com ...

  3. Autofac.Integration.Web分析

    using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// ...

  4. Autofac.Integration.Mvc分析

    Autofac.Integration.Mvc static ILifetimeScope LifetimeScope { get { return (ILifetimeScope)HttpConte ...

  5. 使用Autofac,提示重写成员“Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(System.Type)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。

    接触Autofac大概有2天左右,第2天,亲自动手搭建demo,搭完,以为大功告成的时候,提示了这个错误,网上找了很多方法,都没有解决. 为以后的朋友,避免踩坑,分享一下我的解决方法. Dmeo我是新 ...

  6. 未能加载文件或程序集“Autofac.Integration.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    是因为web.config中dependentAssembly结点下的版本号和当前引用的程序集的版本号不一致!

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

    http://git.oschina.net/shiningrise/AutofacOwinDemo using Microsoft.Owin; using Owin; using System.We ...

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

    主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Int ...

  9. 要引用这几个才有GetOwinContext与GetAutofacLifetimeScope

    using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; var owin = this.Request ...

随机推荐

  1. 区间DP lightoj 1031

    在此游戏中任意时刻的状态都是原始序列的一段子序列故: 定义d(i, j) : 表示原来序列的第i ~ j个元素组成的子序列,在双方都采取最优策略的情况下,先手得分的最大值. 状态转移时,需要枚举从左边 ...

  2. Havel-Hakimi定理

    s是指所有点的度数:由非负整数组成的非增序列s:d1,d2,d3.....,dn(n>=2,d1>=1)是可图的(即能构成图)当且仅当s1:d2-1,d3-1,...dn;是可图的:例如: ...

  3. 510C

    510C 拓扑排序:将那些受影响的字母拓扑排序,其后的输出 #include<iostream> #include<cstdio> #include<vector> ...

  4. Ext-设置form表单不可编辑

    1. var formEach = win.down('form').items.items; Ext.each(formEach,function(item){ console.log(item); ...

  5. C#的输入输出,类型,运算符,语句的练习

    //请输入您的姓名.年龄.工作单位, //拼接成一句完整的话 //我叫***,今年**岁了,在****工作. Console.Write("请输入你的姓名:"); string n ...

  6. iOS异步下载下载进度条显示

    说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection  NSURLRequest NSMutableURLRequest  委托是 NSURLConnectionDo ...

  7. xml序列化方式

    public static class MySerializeXmlHelper { static MySerializeXmlHelper() { } private static object _ ...

  8. android下载简单工具类

    功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考. 首先是一个得到字节流随后保存到内存卡上的工具类: package com. ...

  9. Uva11538 排列组合水题

    画个图就很容易推出公式: 设mn=min(m,n),mx=max(m,n) 对角线上: 横向:m*C(n,2) 纵向:n*C(m,2) 因为所有的C函数都是只拿了两个,所以可以优化下.不过不优化也过了 ...

  10. [NOIP2014] 提高组 洛谷P2312 解方程

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...