3

我正在尝试创建一个“A(UserManager) 需要创建 B(UserClient) 的实例”关系 ( http://code.google.com/p/autofac/wiki/RelationshipTypes ),其中 B(UserClient) 需要一个 HttpSessionStateBase。 .

用户客户端

public class UserClient : IUserClient
    {
        public UserClient(HttpSessionStateBase session)
        {
             //...
        }

        //...
    }

用户管理器

public class UserManager : IUserManager
    {
        private readonly Func<IUserClient> userClientPerRequest;
        private IUserClient UserClient
        {
            get
            {
                return userClientPerRequest();
            }
        }

        public UserManager(Func<IUserClient> userClientPerRequest)
        {
            this.userClientPerRequest = userClientPerRequest;
        }

        public void DoStuff()
        {
            UserClient.DoStuff();
        }

这是注册 autofac 东西的地方

public class MyModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterType<UserManager>().As<IUserManager>().SingleInstance();

            builder.RegisterType<UserClient>().As<IUserClient>().InstancePerHttpRequest();

            builder.RegisterModule(new AutofacWebTypesModule());


            //If i try this, i get Error 1 (printing errors after this code-block)
            builder.Register<Func<IUserClient>>(c => c.Resolve<IUserClient>);

            //If i try this, i get Error 2                
            builder.Register<Func<IUserClient>>(c => {
            var ctx = c.Resolve<IComponentContext>();
            return ctx.Resolve<IUserClient>;                
            });

            //If i try this, well i always get null from GetService..
            builder.Register<Func<IUserClient>>(c =>            
            DependencyResolver.Current.GetService<IUserClient>);
        }

查看Autofac: Reference from a SingleInstance'd type to a HttpRequestScoped,他们使用了一些RequestContainer,但我找不到这样的东西。:)

错误 1

This resolve operation has already ended. When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored. Instead, either resolve IComponentContext again from 'c', or resolve a Func<> based factory to create subsequent components from.

错误 2

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

我尝试过切换.InstancePerHttpRequest().InstancePerLifetimeScope()其他不同的东西..有人有什么想法吗?

谢谢

4

2 回答 2

3

在 Orchard 中手动添加 Autofac 注册时InstancePerMatchingLifetimeScope("shell"),如果需要单例,请使用 ,如果InstancePerMatchingLifetimeScope("work")需要按请求实例,请使用 。

我不确定是否HttpSessionStateBase可以从容器中解析 ctor 参数。您可以将其放在IHttpContextAccessor那里并使用它来访问IUserClient实现内部的会话状态对象。

正如 Jim Bolla 建议的那样 - Func<IUserClient>( factory ) 已经开箱即用。

于 2013-02-01T22:48:47.670 回答
1

我认为您不需要进行其中任何一项注册。由于关系类型Func<IUserClient>您应该已经可以使用。

于 2013-01-31T14:11:57.283 回答