62,046
社区成员
发帖
与我相关
我的任务
分享
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>().InstancePerLifetimeScope();
IContainer container = builder.Build();
// 第一次HTTP请求
var myClass1 = container.Resolve<MyClass>();
//第二次HTTP请求
var myClass2 = container.Resolve<MyClass>();
var _containerProvider = new ContainerProvider(new ContainerBuilder().Build());
var test = _containerProvider.RequestLifetime.Resolve<Test>();
this.Label1.Text = MyService.GetMsg()+test.GetMsg();
public partial class _Default : Page
{
public Test MyService { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = MyService.GetMsg();
}
}
public class Global : HttpApplication, IContainerProviderAccessor
{
// Provider that holds the application container.
static IContainerProvider _containerProvider;
public IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
protected void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
AuthConfig.RegisterOpenAuth();
// Build up your application container and register your dependencies.
var builder = new ContainerBuilder();
builder.RegisterType<Test>().InstancePerRequest();
// ... continue registering dependencies...
// Once you're done registering things, set the container
// provider up with your registrations.
_containerProvider = new ContainerProvider(builder.Build());
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
}
public class Test
{
public string GetMsg()
{
return "auto per request test";
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AutofacWebForm-20150706121558;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-AutofacWebForm-20150706121558.mdf" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>
<!--
If you are deploying to a cloud environment that has multiple web server instances,
you should change session state mode from "InProc" to "Custom". In addition,
change the connection string named "DefaultConnection" to connect to an instance
of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express.
-->
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
<!--<httpModules>
--><!--This section is used for IIS6--><!--
<add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web" />
<add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" />
<add name="AttributeInjection" type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web" />
</httpModules>-->
</system.web>
<system.webServer>
<!-- This section is used for IIS7 -->
<modules runAllManagedModulesForAllRequests="true">
<add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web" preCondition="managedHandler" />
<add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" />
<add name="AttributedInjection" type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" />
</modules>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
[ThreadStatic]
private static Reptile reptile;
public static Reptile Reptile
{
get
{
if (reptile == null)
{
reptile = new Reptile();//这里替换成IOC,每次请求创建一个实例
}
return reptile;
}
}
builder.RegisterType<MyClass>().SingleInstance();
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>();
IContainer container = builder.Build();
// 第一次HTTP请求
var myClass01 = container.Resolve<MyClass>();
var myClass02 = container.Resolve<MyClass>();
//myClass01和myClass02是同一个(共享)
//第二次HTTP请求
var myClass11 = container.Resolve<MyClass>();
var myClass12 = container.Resolve<MyClass>();
//myClass01和myClass02是同一个(共享)
//但是myClass01 和myClass11 不是同一个(不共享)
var builder = new ContainerBuilder();
builder.RegisterType<MyClass>();
IContainer container = builder.Build();
// 第一次HTTP请求
var myClass1 = container.Resolve<MyClass>();
//第二次HTTP请求
var myClass2 = container.Resolve<MyClass>();
简单的说这样就是每次请求都是一个全新的对象了