本文整理汇总了C#中ServiceCollection.AddServiceAgents方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddServiceAgents方法的具体用法?C# ServiceCollection.AddServiceAgents怎么用?C# ServiceCollection.AddServiceAgents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.AddServiceAgents方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HttpClientFactoryClientActionIsPassed
private void HttpClientFactoryClientActionIsPassed()
{
var serviceAgentSettings = new ServiceAgentSettings();
HttpClient passedClient = null;
IServiceProvider passedServiceProvider = null;
var services = new ServiceCollection();
services.AddServiceAgents(s =>
{
s.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_1.json");
s.Section = "TestAgent";
}, (serviceProvider, client) =>
{
passedClient = client;
passedServiceProvider = serviceProvider;
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly
);
///get the registrated HttpFactory
var registration = services.Single(sd => sd.ServiceType == typeof(IHttpClientFactory));
//Manually call the CreateClient on the factory (this normally happens when the service agent gets resolved
var factory = registration.ImplementationFactory.Invoke(null) as HttpClientFactory;
factory.CreateClient(serviceAgentSettings, new ServiceSettings { Host = "test.be" });
Assert.NotNull(passedClient);
}
示例2: ActionNullRaisesArgumentException
private void ActionNullRaisesArgumentException()
{
Action<ServiceSettingsJsonFile> nullAction = null;
var services = new ServiceCollection();
var ex = Assert.Throws<ArgumentNullException>(() => services.AddServiceAgents(nullAction));
Assert.Equal("jsonSetupAction", ex.ParamName);
}
示例3: HttpClientFactoryIsRegistratedAsSingleton
private void HttpClientFactoryIsRegistratedAsSingleton()
{
var services = new ServiceCollection();
services.AddServiceAgents(settings =>
{
settings.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_1.json");
settings.Section = "TestAgent";
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(IHttpClientFactory))
.ToArray();
Assert.Equal(1, registrations.Count());
Assert.Equal(ServiceLifetime.Singleton, registrations[0].Lifetime);
}
示例4: ServiceAgentSettingsActionIsPassed
private void ServiceAgentSettingsActionIsPassed()
{
var serviceAgentSettings = new ServiceAgentSettings();
var services = new ServiceCollection();
services.AddServiceAgents(json =>
{
json.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_1.json");
}, settings =>
{
settings.Services["TestAgent"].Port = "15000";
}, null,
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(IConfigureOptions<ServiceAgentSettings>))
.ToArray();
Assert.Equal(1, registrations.Count());
Assert.Equal(ServiceLifetime.Singleton, registrations[0].Lifetime);
var configOptions = registrations[0].ImplementationInstance as IConfigureOptions<ServiceAgentSettings>;
Assert.NotNull(configOptions);
serviceAgentSettings = new ServiceAgentSettings();
configOptions.Configure(serviceAgentSettings);
var serviceSettings = serviceAgentSettings.Services["TestAgent"];
Assert.NotNull(serviceSettings);
Assert.Equal("15000", serviceSettings.Port);
}
示例5: TokenHelperIsRegistratedAsScoped
private void TokenHelperIsRegistratedAsScoped()
{
var services = new ServiceCollection();
services.AddServiceAgents(settings =>
{
settings.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_3.json");
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(ITokenHelper) &&
sd.ImplementationType == typeof(TokenHelper))
.ToArray();
Assert.Equal(1, registrations.Count());
Assert.Equal(ServiceLifetime.Scoped, registrations[0].Lifetime);
}
示例6: GenericAgentIsRegistratedAsScoped
private void GenericAgentIsRegistratedAsScoped()
{
var services = new ServiceCollection();
services.AddServiceAgents(settings =>
{
settings.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_4.json");
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(GenericAgent<>))
.ToArray();
Assert.Equal(1, registrations.Count());
Assert.Equal(ServiceLifetime.Scoped, registrations[0].Lifetime);
registrations = services.Where(sd => sd.ServiceType == typeof(IConfigureOptions<ServiceAgentSettings>))
.ToArray();
var configOptions = registrations[0].ImplementationInstance as IConfigureOptions<ServiceAgentSettings>;
Assert.NotNull(configOptions);
var serviceAgentSettings = new ServiceAgentSettings();
configOptions.Configure(serviceAgentSettings);
Assert.Equal(1, serviceAgentSettings.Services.Count);
var serviceSettings = serviceAgentSettings.Services["GenericAgent"];
Assert.NotNull(serviceSettings);
}
示例7: MultipleServiceAgentsAreRegistratedAsScoped
private void MultipleServiceAgentsAreRegistratedAsScoped()
{
var services = new ServiceCollection();
services.AddServiceAgents(settings =>
{
settings.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_2.json");
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(TestAgent) ||
sd.ServiceType == typeof(OtherTestAgent))
.ToArray();
Assert.Equal(2, registrations.Count());
Assert.Equal(ServiceLifetime.Scoped, registrations[0].Lifetime);
Assert.Equal(nameof(OtherTestAgent), registrations[0].ServiceType.Name);
Assert.Equal(ServiceLifetime.Scoped, registrations[1].Lifetime);
Assert.Equal(nameof(TestAgent), registrations[1].ServiceType.Name);
}
示例8: MultipleServiceAgents
private void MultipleServiceAgents()
{
var services = new ServiceCollection();
services.AddServiceAgents(settings =>
{
settings.FileName = Path.Combine(Directory.GetCurrentDirectory(), "_TestData/serviceagentconfig_2.json");
},
assembly: typeof(AddServiceAgentsTests).GetTypeInfo().Assembly);
var registrations = services.Where(sd => sd.ServiceType == typeof(IConfigureOptions<ServiceAgentSettings>))
.ToArray();
var configOptions = registrations[0].ImplementationInstance as IConfigureOptions<ServiceAgentSettings>;
Assert.NotNull(configOptions);
var serviceAgentSettings = new ServiceAgentSettings();
configOptions.Configure(serviceAgentSettings);
Assert.Equal(2, serviceAgentSettings.Services.Count);
var serviceSettings = serviceAgentSettings.Services["TestAgent"];
Assert.NotNull(serviceSettings);
Assert.Equal("None", serviceSettings.AuthScheme);
Assert.Equal("test.be", serviceSettings.Host);
Assert.Equal("api", serviceSettings.Path);
Assert.Equal("5001", serviceSettings.Port);
Assert.Equal(HttpSchema.Http, serviceSettings.Scheme);
serviceSettings = serviceAgentSettings.Services["OtherTestAgent"];
Assert.NotNull(serviceSettings);
Assert.Equal(AuthScheme.Bearer, serviceSettings.AuthScheme);
Assert.Equal("other.be", serviceSettings.Host);
Assert.Equal("path", serviceSettings.Path);
Assert.Equal("5002", serviceSettings.Port);
Assert.Equal(HttpSchema.Https, serviceSettings.Scheme);
}