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")リクエストごとのインスタンスが必要な場合はを使用します。

HttpSessionStateBasector引数が実際にコンテナから解決できるかどうかはわかりません。代わりにそこに配置し、それを使用して実装IHttpContextAccessor内のセッション状態オブジェクトにアクセスできます。IUserClient

そしてジム・ボラが示唆したように- Func<IUserClient>ファクトリー)はすでに箱から出して利用可能です。

于 2013-02-01T22:48:47.670 に答える
1

これらの登録のいずれかを行う必要はないと思います。Relationship Typesのため、Func<IUserClient>はすでに利用可能になっているはずです。

于 2013-01-31T14:11:57.283 に答える