Orchard CMS,Autofac Relationship

时间:2013-01-31 09:35:25

标签: c# inversion-of-control orchardcms autofac

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

UserClient

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

        //...
    }

的UserManager

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()以及其他一些不同的东西..任何人都有任何想法?

由于

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

我认为您不需要进行其中任何一项注册。由于Relationship TypesFunc<IUserClient>应该已经可以使用了。