标签: autofac

如何使用Autofac确保每个请求有一个NHibernate ISession?

我在Application_Start方法中使用的Autofac模块中有以下代码:

builder.Register(c => new Configuration().Configure().BuildSessionFactory())
    .SingletonScoped();
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession())
    .HttpRequestScoped();

builder.Register<NHibernateSomethingRepository>().As<ISomethingRepository>();
Run Code Online (Sandbox Code Playgroud)

存储库的构造函数将ISession作为参数.但是我最终为整个应用程序提供了一个会话,即使我明确要求它是HttpRequestScoped.

我已经配置了ContainerDisposal HTTP模块.

根据文档,您必须创建一个嵌套容器,但我让Autofac自动装配依赖项.

我该怎么办?

谢谢!

nhibernate asp.net-mvc dependency-injection autofac

3
推荐指数
1
解决办法
1397
查看次数

在Autofac中调用所有ISomething实例

我有一个接口ISomething与方法Start.我想得到这个接口的所有实现(在多个程序集中,主要的和所有引用的),并在应用程序启动时调用Start方法.如何使用Autofac 2.4.4.705执行此操作?

autofac

3
推荐指数
1
解决办法
1275
查看次数

AutoFac创建类型,无论存在

我想使用AutoFac将引用注入到对象的构造函数中.但是,实际对象本身未注册.我正在为ASP.NET MVC控制器工厂执行此操作,其中控制器将不会注册,但构造函数params将是.我设法团结一致,但我遇到了AutoFac的问题.

那可能吗?

谢谢.

.net c# dependency-injection autofac

3
推荐指数
2
解决办法
797
查看次数

Autofac注册支架

在最后几天,我把手表带到了orchad源,在使用Autofac注册组件的过程中,我看到了相同的代码,我无法解释!我将举一个例子:

builder.RegisterType<A>().As<IA>();
{
  builder.RegisterType<B>().As<IB>();
  {
     builder.RegisterType<C>().As<IC>();
  }
}
Run Code Online (Sandbox Code Playgroud)

我不能不支持括号做什么?它是否像子注册?

希望有人能帮助我!

谢谢

c# ioc-container autofac orchardcms

3
推荐指数
1
解决办法
200
查看次数

Autofac:解析对命名实例的特定依赖关系

使用Autofac,我想注册一个组件并指定要解析为命名实例的特定依赖项.

我使用构造函数注入找到了类似下面的示例,这几乎是我想要的.

builder.Register(c => new ObjectContainer(ConnectionStrings.CustomerDB))
    .As<IObjectContainer>()
    .Named("CustomerObjectContainer");

builder.Register(c => new ObjectContainer(ConnectionStrings.FooDB))
    .As<IObjectContainer>()
    .Named("FooObjectContainer");

builder.Register(c => new CustomerRepository(
    c.Resolve<IObjectContainer>("CustomerObjectContainer"));

builder.Register(c => new FooRepository(
    c.Resolve<IObjectContainer>("FooObjectContainer"));
Run Code Online (Sandbox Code Playgroud)

但是,我需要使用属性注入,我不想指定所有依赖项.

就像是:

builder.Register<CustomerRepository>().With<IObjectContainer>("CustomerObjectContainer");    
builder.Register<FooRepository>().With<IObjectContainer>("FooObjectContainer");
Run Code Online (Sandbox Code Playgroud)

所有未指定的依赖项的构建应该在未命名的实例中发生.

谢谢,亚历克斯

[丹尼尔克回答]

对于该类型的任何属性,按类型解析的重载.

    public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> WithDependency<TLimit, TReflectionActivatorData, TStyle, TProperty>(
        this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration,
        Func<IComponentContext, TProperty> valueProvider)
        where TReflectionActivatorData : ReflectionActivatorData
    {
        return registration.WithProperty(new ResolvedParameter((p, c) =>
            {
                PropertyInfo prop;
                return p.TryGetDeclaringProperty(out prop) &&
                    prop.PropertyType == typeof(TProperty);
            },
            (p, c) => valueProvider(c)));
    }
Run Code Online (Sandbox Code Playgroud)

ioc-container autofac

3
推荐指数
1
解决办法
6823
查看次数

Autofac的混合生活方式

我在ASP.NET MVC中有一个应用程序,它也有一个WCF服务包含在同一个项目中.

我使用Autofac来管理依赖注入.问题是当通过Web访问应用程序时,我需要根据Http请求实例化依赖项.当通过WCF访问应用程序时,我需要依赖项实例化依赖项.

在Castle.Windsor中,有一个管理混合生活方式的项目(在此链接中).

我需要类似的东西,比如:

builder.Register<UnitOfMeasureService>(x => new UnitOfMeasureService())
            .As<IUnitOfMeasureService>().HybridLifetimeInstance();
Run Code Online (Sandbox Code Playgroud)

是否有解决方法来管理实例生命周期,具体取决于应用程序何时具有HttpContext

c# autofac asp.net-mvc-3

3
推荐指数
1
解决办法
814
查看次数

Autofac OWIN TestServer和HttpContext

我正在尝试使用我的IIS托管WebAPI 2.2应用程序设置集成测试.我使用Autofac进行DI,我使用的是使用OWIN的新ASP.net Identity堆栈.我遇到了Autofac的问题,其中HttpContext类总是为null.以下是我如何设置我的基本集成测试类 -

 [TestClass]
public class TestBase
{
    private SimpleLifetimeScopeProvider _scopeProvider;
    private IDependencyResolver _originalResolver;
    private HttpConfiguration _configuration;
    public TestServer Server { get; private set; }

    [TestInitialize]
    public void Setup()
    {
        Server = TestServer.Create(app =>
        {
            //config webpai
            _configuration = new HttpConfiguration();
            WebApiConfig.Register(_configuration);

            // Build the container.
            var container = App_Start.IocConfig.RegisterDependencies(_configuration);
            _scopeProvider = new SimpleLifetimeScopeProvider(container);

            //set the mvc dep resolver
            var mvcResolver = new AutofacDependencyResolver(container, _scopeProvider);
            _originalResolver = DependencyResolver.Current;
            DependencyResolver.SetResolver(mvcResolver);

            //set the webapi dep resolvers
            _configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            app.UseAutofacMiddleware(container); …
Run Code Online (Sandbox Code Playgroud)

asp.net integration-testing autofac asp.net-mvc-5 asp.net-web-api2

3
推荐指数
1
解决办法
3211
查看次数

将log4net与Autofac一起使用

我正在尝试将log4net与Autofac一起使用.我已粘贴此代码http://autofac.readthedocs.org/en/latest/examples/log4net.html,并从Program.cs/Main()我正在做

var iocBuilder = new ContainerBuilder();
iocBuilder.RegisterModule(new LoggingModule());
var iocContainer = iocBuilder.Build();
Run Code Online (Sandbox Code Playgroud)

现在我想立即尝试这个(在下一行),写一个简单的行到日志文件.我该怎么办?

我在想这样的事情

var ls = iocContainer.Resolve<LoggingModule>();
ls.Info("the logging is working");
Run Code Online (Sandbox Code Playgroud)

非常感谢

c# inversion-of-control autofac

3
推荐指数
1
解决办法
6367
查看次数

在Owin Startup上解析InstancePerLifetimeScope中的Autofac服务

我无法找到通过Autofac解析服务的正确方法,该方法在构造 Owin上下文时使用,并且也在请求端处理.

由于此时OwinContext仍在构建中,因此无法通过调用找到LifetimeScope HttpContext.Current.GetOwinContext().GetAutofacLifetimeScope().OwinContext还没有.

在我的代码中,IAdfsAuthorizationProvider服务直接在该服务中解析Container,但在请求之后不会被处理并且寿命更长.

我可以通过调用创建一个新的LifeTimeScope container.BeginLifetimeScope(),但现在LifeTime被分离了请求,并且可能会解析同一请求中的不同实例.并且无法在正确的时间自行处理lifeTimeScope.

    public void Configure(IAppBuilder app)
    {
         var builder = new ContainerBuilder();    
         builder.RegisterType<AdfsAuthorizationProvider>().As<IAdfsAuthorizationProvider>().InstancePerLifetimeScope();

         var container = builder.Build();

         app.UseAutofacMiddleware(container);

         // **Service that's not binding to the request lifetime.**
         var service = container.Resolve<IAdfsAuthorizationProvider>();

         app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
             {
                Provider = new AuthorizationServerProvider(service),
             });         
          }
Run Code Online (Sandbox Code Playgroud)

有没有人有建议?

c# startup inversion-of-control autofac owin

3
推荐指数
1
解决办法
1744
查看次数

如何使用.net核心依赖项注入指定参数?

使用Autofac考虑以下代码:

 Builder.RegisterType<SecurityCache>().As<ISecurityCache>()
    .WithParameter("id", AppId).SingleInstance()
Run Code Online (Sandbox Code Playgroud)

SecurityCache有3个参数,其中两个由DI容器处理,然后使用来指定“ id”参数WithParameter。如何使用.NET Core DI而不是使用Autofac做到这一点?

我想要做

services.AddSingleton<ISecurityCache, SecurityCache>(); 
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何指定id参数。

c# dependency-injection autofac .net-core

3
推荐指数
1
解决办法
1711
查看次数