您如何提供 Autofac 中的特定注册作为其他注册的参数?
How do you provide specific registrations in Autofac as parameters to other registrations?
能否就如何将一些引用传递给其他注册提供一些指导?
//registration of 1st http client
builder.RegisterType<HttpClient>()
//.Keyed<HttpMessageInvoker>("authHttpClient")
.WithProperties(new[] { new NamedPropertyParameter("PooledConnectionLifetime", 2 })
.WithProperties(new[] { new NamedPropertyParameter("BaseAddress", "whatever"))})
.SingleInstance();
//registration of 2nd httpclient
builder.RegisterType<HttpClient>()
.Keyed<HttpMessageInvoker>("dynamicsHttpClient")
.WithProperties(new[] { new NamedPropertyParameter("PooledConnectionLifetime", 20) })
.WithProperties(new[] { new NamedPropertyParameter("BaseAddress", "other something" })
.SingleInstance();
// **I need to do the registration of type and pass the 1st httpClient registration**
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
// **I need to do the registration of type pass 2nd instance of httpClient registration**
builder.RegisterType<DynamicsApiGateway>()
.As<IDynamicsApiGateway>()
.SingleInstance();
//Method ctor's
//DynamicsAuthApiGateway(**HttpClient client**, DynamicsAuthApiGatewaySettings apiGatewaySettings)
//DynamicsApiGateway(**HttpClient client**, Func<HttpResponseMessage, Task> errorHandler = null)
你能帮忙说说如何实现吗?
任何帮助将不胜感激?
谢谢,
我
您使用密钥服务的方向正确。 Docs here for details and examples.
最简单的方法是使用 Autofac.Features.AttributeFilters.KeyFilterAttribute
来标记消费 class 上的构造函数。
首先,更新消费者。这是一个例子。
public class DynamicsAuthApiGateway : IDynamicsAuthApiGateway
{
// KeyFilterAttribute on the parameter
public DynamicsAuthApiGateway([KeyFilter("authHttpClient")] HttpClient client)
{
// ...
}
}
然后注册键控客户端并在消费者上启用属性过滤。
// Don't forget
// using Autofac.Features.AttributeFilters;
// at the top... then:
// Register the HttpClient instances with keys
// and be sure the key is on the same type/interface
// as you see in the constructor of the consumer.
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("authHttpClient")
.SingleInstance();
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("dynamicsHttpClient")
.SingleInstance();
// Register the consumers and enable attribute filters
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
.WithAttributeFiltering();
builder.RegisterType<DynamicsApiGateway>()
.As<IDynamicsApiGateway>()
.SingleInstance()
.WithAttributeFiltering();
您必须选择加入属性过滤,因为如果您不需要它会产生额外的性能开销。如果您不输入 WithAttributeFiltering()
部分,则不会进行过滤。
如果你不想在你的 class 中使用该属性(例如,你想避免将 Autofac 绑定到事物中)那么它有点难 - 你需要使用 ResolvedParameter
( docs here).
看起来更像这样(我有点想把它写下来,所以如果有错别字之类的,你已经被警告过,但基本上应该是)...
// Register the HttpClient instances with keys
// and be sure the key is on the same type/interface
// as you see in the constructor of the consumer.
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("authHttpClient")
.SingleInstance();
// Register the consumers using resolved parameters.
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(HttpClient),
(pi, ctx) => ctx.ResolveKeyed<HttpClient>("authHttpClient")));
它不是很干净,但它使 Autofac 引用远离消费者(如果重要的话)。如果你经常使用它,你可以编写自己的扩展方法以使其更容易。我将把它留作 reader.
的练习
能否就如何将一些引用传递给其他注册提供一些指导?
//registration of 1st http client
builder.RegisterType<HttpClient>()
//.Keyed<HttpMessageInvoker>("authHttpClient")
.WithProperties(new[] { new NamedPropertyParameter("PooledConnectionLifetime", 2 })
.WithProperties(new[] { new NamedPropertyParameter("BaseAddress", "whatever"))})
.SingleInstance();
//registration of 2nd httpclient
builder.RegisterType<HttpClient>()
.Keyed<HttpMessageInvoker>("dynamicsHttpClient")
.WithProperties(new[] { new NamedPropertyParameter("PooledConnectionLifetime", 20) })
.WithProperties(new[] { new NamedPropertyParameter("BaseAddress", "other something" })
.SingleInstance();
// **I need to do the registration of type and pass the 1st httpClient registration**
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
// **I need to do the registration of type pass 2nd instance of httpClient registration**
builder.RegisterType<DynamicsApiGateway>()
.As<IDynamicsApiGateway>()
.SingleInstance();
//Method ctor's
//DynamicsAuthApiGateway(**HttpClient client**, DynamicsAuthApiGatewaySettings apiGatewaySettings)
//DynamicsApiGateway(**HttpClient client**, Func<HttpResponseMessage, Task> errorHandler = null)
你能帮忙说说如何实现吗?
任何帮助将不胜感激?
谢谢, 我
您使用密钥服务的方向正确。 Docs here for details and examples.
最简单的方法是使用 Autofac.Features.AttributeFilters.KeyFilterAttribute
来标记消费 class 上的构造函数。
首先,更新消费者。这是一个例子。
public class DynamicsAuthApiGateway : IDynamicsAuthApiGateway
{
// KeyFilterAttribute on the parameter
public DynamicsAuthApiGateway([KeyFilter("authHttpClient")] HttpClient client)
{
// ...
}
}
然后注册键控客户端并在消费者上启用属性过滤。
// Don't forget
// using Autofac.Features.AttributeFilters;
// at the top... then:
// Register the HttpClient instances with keys
// and be sure the key is on the same type/interface
// as you see in the constructor of the consumer.
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("authHttpClient")
.SingleInstance();
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("dynamicsHttpClient")
.SingleInstance();
// Register the consumers and enable attribute filters
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
.WithAttributeFiltering();
builder.RegisterType<DynamicsApiGateway>()
.As<IDynamicsApiGateway>()
.SingleInstance()
.WithAttributeFiltering();
您必须选择加入属性过滤,因为如果您不需要它会产生额外的性能开销。如果您不输入 WithAttributeFiltering()
部分,则不会进行过滤。
如果你不想在你的 class 中使用该属性(例如,你想避免将 Autofac 绑定到事物中)那么它有点难 - 你需要使用 ResolvedParameter
( docs here).
看起来更像这样(我有点想把它写下来,所以如果有错别字之类的,你已经被警告过,但基本上应该是)...
// Register the HttpClient instances with keys
// and be sure the key is on the same type/interface
// as you see in the constructor of the consumer.
builder.RegisterType<HttpClient>()
.Keyed<HttpClient>("authHttpClient")
.SingleInstance();
// Register the consumers using resolved parameters.
builder.RegisterType<DynamicsAuthApiGateway>()
.As<IDynamicsAuthApiGateway>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(HttpClient),
(pi, ctx) => ctx.ResolveKeyed<HttpClient>("authHttpClient")));
它不是很干净,但它使 Autofac 引用远离消费者(如果重要的话)。如果你经常使用它,你可以编写自己的扩展方法以使其更容易。我将把它留作 reader.
的练习