本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.Add方法的具体用法?C# ServiceCollection.Add怎么用?C# ServiceCollection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.DependencyInjection.ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.Add方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add_AddsMultipleDescriptorToServiceDescriptors
public void Add_AddsMultipleDescriptorToServiceDescriptors()
{
// Arrange
var serviceCollection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), new FakeService());
var descriptor2 = new ServiceDescriptor(typeof(IFactoryService), typeof(TransientFactoryService), ServiceLifetime.Transient);
// Act
serviceCollection.Add(descriptor1);
serviceCollection.Add(descriptor2);
// Assert
Assert.Equal(2, serviceCollection.Count);
Assert.Equal(new[] { descriptor1, descriptor2 }, serviceCollection);
}
示例2: MultiRegistrationServiceTypes_AreRegistered_MultipleTimes
public void MultiRegistrationServiceTypes_AreRegistered_MultipleTimes()
{
// Arrange
var services = new ServiceCollection();
// Register a mock implementation of each service, AddMvcServices should add another implemenetation.
foreach (var serviceType in MutliRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType.Key);
services.Add(ServiceDescriptor.Transient(serviceType.Key, mockType));
}
// Act
MvcCoreServiceCollectionExtensions.AddMvcCoreServices(services);
// Assert
foreach (var serviceType in MutliRegistrationServiceTypes)
{
AssertServiceCountEquals(services, serviceType.Key, serviceType.Value.Length + 1);
foreach (var implementationType in serviceType.Value)
{
AssertContainsSingle(services, serviceType.Key, implementationType);
}
}
}
示例3: ServiceDescriptors_AllowsRemovingPreviousRegisteredServices
public void ServiceDescriptors_AllowsRemovingPreviousRegisteredServices()
{
// Arrange
var serviceCollection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), new FakeService());
var descriptor2 = new ServiceDescriptor(typeof(IFactoryService), typeof(TransientFactoryService), ServiceLifetime.Transient);
// Act
serviceCollection.Add(descriptor1);
serviceCollection.Add(descriptor2);
serviceCollection.Remove(descriptor1);
// Assert
var result = Assert.Single(serviceCollection);
Assert.Same(result, descriptor2);
}
示例4: Main
public Task<int> Main(string[] args)
{
//Add command line configuration source to read command line parameters.
var config = new Configuration();
config.AddCommandLine(args);
var serviceCollection = new ServiceCollection();
serviceCollection.Add(HostingServices.GetDefaultServices(config));
var services = serviceCollection.BuildServiceProvider(_hostServiceProvider);
var context = new HostingContext()
{
Services = services,
Configuration = config,
ServerName = "Microsoft.AspNet.Server.WebListener",
ApplicationName = "BugTracker"
};
var engine = services.GetService<IHostingEngine>();
if (engine == null)
{
throw new Exception("TODO: IHostingEngine service not available exception");
}
using (engine.Start(context))
{
Console.WriteLine("Started the server..");
Console.WriteLine("Press any key to stop the server");
Console.ReadLine();
}
return Task.FromResult(0);
}
示例5: WrappingServiceProvider
// Need full wrap for generics like IOptions
public WrappingServiceProvider(IServiceProvider fallback, IServiceCollection replacedServices)
{
var services = new ServiceCollection();
var manifest = fallback.GetRequiredService<IRuntimeServices>();
foreach (var service in manifest.Services) {
services.AddTransient(service, sp => fallback.GetService(service));
}
services.Add(replacedServices);
_services = services.BuildServiceProvider();
}
示例6: Add_AddsDescriptorToServiceDescriptors
public void Add_AddsDescriptorToServiceDescriptors()
{
// Arrange
var serviceCollection = new ServiceCollection();
var descriptor = new ServiceDescriptor(typeof(IFakeService), new FakeService());
// Act
serviceCollection.Add(descriptor);
// Assert
var result = Assert.Single(serviceCollection);
Assert.Same(result, descriptor);
}
示例7: CreateServices
private IServiceProvider CreateServices()
{
var services = new ServiceCollection();
services.Add(new ServiceDescriptor(
typeof(ILogger<ObjectResult>),
new Logger<ObjectResult>(NullLoggerFactory.Instance)));
var optionsAccessor = new MockMvcOptionsAccessor();
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
services.Add(new ServiceDescriptor(typeof(IOptions<MvcOptions>), optionsAccessor));
var bindingContext = new ActionBindingContext
{
OutputFormatters = optionsAccessor.Options.OutputFormatters,
};
var bindingContextAccessor = new ActionBindingContextAccessor
{
ActionBindingContext = bindingContext,
};
services.Add(new ServiceDescriptor(typeof(IActionBindingContextAccessor), bindingContextAccessor));
return services.BuildServiceProvider();
}
示例8: SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes
public void SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes()
{
// Arrange
var services = new ServiceCollection();
// Register a mock implementation of each service, AddMvcServices should not replace it.
foreach (var serviceType in SingleRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType);
services.Add(ServiceDescriptor.Transient(serviceType, mockType));
}
// Act
MvcCoreServiceCollectionExtensions.AddMvcCoreServices(services);
// Assert
foreach (var singleRegistrationType in SingleRegistrationServiceTypes)
{
AssertServiceCountEquals(services, singleRegistrationType, 1);
}
}
示例9: CreateServiceCollection
private static IServiceCollection CreateServiceCollection(IConfiguration config)
{
var collection = new ServiceCollection(config);
collection.Add(GetDefaultServices(config));
return collection;
}
示例10: TryAdd_WithType_DoesNotAddDuplicate
public void TryAdd_WithType_DoesNotAddDuplicate(
Action<IServiceCollection> addAction,
Type expectedServiceType,
Type expectedImplementationType,
ServiceLifetime expectedLifetime)
{
// Arrange
var collection = new ServiceCollection();
collection.Add(ServiceDescriptor.Transient(expectedServiceType, expectedServiceType));
// Act
addAction(collection);
// Assert
var descriptor = Assert.Single(collection);
Assert.Equal(expectedServiceType, descriptor.ServiceType);
Assert.Same(expectedServiceType, descriptor.ImplementationType);
Assert.Equal(ServiceLifetime.Transient, descriptor.Lifetime);
}
示例11: Replace_ReplacesFirstServiceWithMatchingServiceType
public void Replace_ReplacesFirstServiceWithMatchingServiceType()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), ServiceLifetime.Transient);
var descriptor2 = new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), ServiceLifetime.Transient);
collection.Add(descriptor1);
collection.Add(descriptor2);
var descriptor3 = new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), ServiceLifetime.Singleton);
// Act
collection.Replace(descriptor3);
// Assert
Assert.Equal(new[] { descriptor2, descriptor3 }, collection);
}
示例12: Replace_AddsServiceIfServiceTypeIsNotRegistered
public void Replace_AddsServiceIfServiceTypeIsNotRegistered()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), ServiceLifetime.Transient);
var descriptor2 = new ServiceDescriptor(typeof(IFakeOuterService), typeof(FakeOuterService), ServiceLifetime.Transient);
collection.Add(descriptor1);
// Act
collection.Replace(descriptor2);
// Assert
Assert.Equal(new[] { descriptor1, descriptor2 }, collection);
}
示例13: AddSequence_AddsServicesToCollection
public void AddSequence_AddsServicesToCollection()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), ServiceLifetime.Transient);
var descriptor2 = new ServiceDescriptor(typeof(IFakeOuterService), typeof(FakeOuterService), ServiceLifetime.Transient);
var descriptors = new[] { descriptor1, descriptor2 };
// Act
var result = collection.Add(descriptors);
// Assert
Assert.Equal(descriptors, collection);
}
示例14: Configure
public void Configure(IBuilder app)
{
app.UseFileServer();
#if NET45
var configuration = new Configuration()
.AddJsonFile(@"App_Data\config.json")
.AddEnvironmentVariables();
string diSystem;
if (configuration.TryGet("DependencyInjection", out diSystem) &&
diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
{
app.UseMiddleware<MonitoringMiddlware>();
var services = new ServiceCollection();
services.AddMvc();
services.AddSingleton<PassThroughAttribute>();
services.AddSingleton<UserNameService>();
services.AddTransient<ITestService, TestService>();
services.Add(OptionsServices.GetDefaultServices());
// Create the autofac container
ContainerBuilder builder = new ContainerBuilder();
// Create the container and use the default application services as a fallback
AutofacRegistration.Populate(
builder,
services,
fallbackServiceProvider: app.ApplicationServices);
builder.RegisterModule<MonitoringModule>();
IContainer container = builder.Build();
app.UseServices(container.Resolve<IServiceProvider>());
}
else
#endif
{
app.UseServices(services =>
{
services.AddMvc();
services.AddSingleton<PassThroughAttribute>();
services.AddSingleton<UserNameService>();
services.AddTransient<ITestService, TestService>();
});
}
app.UseMvc(routes =>
{
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
routes.MapRoute(
"controllerActionRoute",
"{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute(
"controllerRoute",
"{controller}",
new { controller = "Home" });
});
}