本文整理汇总了C#中SimpleInjector.Container.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# Container.GetService方法的具体用法?C# Container.GetService怎么用?C# Container.GetService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleInjector.Container
的用法示例。
在下文中一共展示了Container.GetService方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UseSimpleInjector
public static IServiceProvider UseSimpleInjector(this IServiceCollection services)
{
var container = new Container();
var scopeFactory = new SimpleInjectorServiceScopeFactory(container);
container.Options.AllowOverridingRegistrations = true;
container.Options.ResolveUnregisteredCollections = true;
container.Options.SuppressLifestyleMismatchVerification = true;
container.Options.DefaultScopedLifestyle = scopeFactory;
foreach (var descriptor in services)
{
//if (descriptor.ServiceType.FullName.IndexOf("IConfigureOptions") >= 0)
//{
// System.Diagnostics.Debugger.Break();
//}
var lifetime = ConvertLifetimeToSimpleInjectorLifetime(descriptor.Lifetime);
if (descriptor.ImplementationType != null)
{
var serviceTypeInfo = descriptor.ServiceType.GetTypeInfo();
if (serviceTypeInfo.IsGenericTypeDefinition)
{
if (descriptor.ImplementationType.GetTypeInfo().IsGenericTypeDefinition)
{
// How do you pass the lifetime here?
container.Register(descriptor.ServiceType, descriptor.ImplementationType, lifetime);
}
else
{
container.Register(descriptor.ServiceType, new[] { descriptor.ImplementationType }, lifetime);
}
}
else
{
container.Register(descriptor.ServiceType, descriptor.ImplementationType, lifetime);
// for collection based
container.Register(descriptor.ImplementationType, descriptor.ImplementationType, lifetime);
}
}
else if (descriptor.ImplementationFactory != null)
{
container.Register(
descriptor.ServiceType,
() => descriptor.ImplementationFactory(container.GetService<IServiceProvider>()),
lifetime);
}
else
{
container.Register(
descriptor.ServiceType,
() => descriptor.ImplementationInstance,
lifetime);
}
}
var groupedTypes = services
.GroupBy(s => s.ServiceType);
foreach (var groupedType in groupedTypes)
{
var serviceType = groupedType.Key;
var type = typeof(IEnumerable<>).MakeGenericType(serviceType);
if (serviceType.GetTypeInfo().IsGenericTypeDefinition)
{
container.RegisterCollection(serviceType, groupedType.Select(t => t.ImplementationType));
}
else
{
// TODO: Get longest lifestyle?
var lifetime = ConvertLifetimeToSimpleInjectorLifetime(groupedType.First().Lifetime);
container.Register(
type,
() =>
{
var collectionServices = groupedType
.Where(c => c.ImplementationType != null)
.Select(c => container.GetRequiredService(c.ImplementationType));
return typeof(System.Linq.Enumerable)
.GetMethod("Cast", new[] { typeof(System.Collections.IEnumerable) })
.MakeGenericMethod(groupedType.Key)
.Invoke(null, new object[] { collectionServices });
},
lifetime);
}
}
container.Register<IServiceProvider>(() => container, Lifestyle.Singleton);
container.Register<IServiceScopeFactory>(() => scopeFactory, Lifestyle.Singleton);
// container.Verify();
return container;
}