简体   繁体   English

在构建我的 Autofac 容器时,如何将以前注册的类型作为参数包含在其他注册类型中?

[英]When building my Autofac container, how do I include previously registered types as parameters in other registered types?

Consider the following:考虑以下:

        public static IContainer Configure()
        {
            var builder = new ContainerBuilder();

            // Register a named HttpClient instance as a singleton.
            builder.Register(_ =>
            {
                var client = new HttpClient
                {
                    MaxResponseContentBufferSize = int.MaxValue,
                    Timeout = TimeSpan.FromMinutes(50)
                };

                client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
                client.DefaultRequestHeaders.Add("Keep-Alive", "3600");
                return client;
            })
            .SingleInstance();

            builder.Register(l => new RandomObject(new HttpClient, "randomString")).As<IRandomObject>();


            return builder.Build();
        }

When registering my RandomObject , how do I replace that new HttpClient instance in my previously registered HttpClient ?注册我的RandomObject时,如何在我之前注册的HttpClient中替换那个new HttpClient实例? Failing that, is there a way new-up the HttpClient parameter so that it has the same settings as the previously registered one?如果做不到这一点,是否有办法更新HttpClient参数,使其具有与先前注册的设置相同的设置?

I think you should use resolve method for that我认为您应该为此使用解决方法

builder.Register(l => new RandomObject(l.Resolve<HttpClient>(), "randomString").As<IRandomObject>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM