0

您能否就如何将一些引用传递给其他注册提供一些指导?

//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) 

你能帮助实现这一目标吗?

任何帮助,将不胜感激 ?

谢谢,我

4

1 回答 1

1

您在使用密钥服务方面走在了正确的轨道上。此处的文档以获取详细信息和示例。

最简单的方法是使用Autofac.Features.AttributeFilters.KeyFilterAttribute来标记消费类的构造函数。

首先,更新消费者。这是一个例子。

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()零件,则不会发生过滤。

如果您不想在您的类中使用该属性(例如,您想避免将 Autofac 绑定到事物中),那么它会有点困难 - 您需要使用ResolvedParameter此处的文档)。

这看起来更像这样(我有点想把它写在我的头上,所以如果有错别字或其他什么,你已经被警告过,但基本上应该是这样)......

// 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 引用排除在消费者之外(如果重要的话)。如果您经常使用它,您可以编写自己的扩展方法以使其更容易。我将把它作为练习留给读者。

于 2020-05-27T16:41:01.440 回答