本文整理汇总了C#中IServiceCollection.Scan方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.Scan方法的具体用法?C# IServiceCollection.Scan怎么用?C# IServiceCollection.Scan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.Scan方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
//Add Cqrs services
services.AddSingleton<InProcessBus>(new InProcessBus());
services.AddSingleton<ICommandSender>(y => y.GetService<InProcessBus>());
services.AddSingleton<IEventPublisher>(y => y.GetService<InProcessBus>());
services.AddSingleton<IHandlerRegistrar>(y => y.GetService<InProcessBus>());
services.AddScoped<ISession, Session>();
services.AddSingleton<IEventStore, InMemoryEventStore>();
services.AddScoped<ICache, CQRSlite.Cache.MemoryCache>();
services.AddScoped<IRepository>(y => new CacheRepository(new Repository(y.GetService<IEventStore>()), y.GetService<IEventStore>(), y.GetService<ICache>()));
services.AddTransient<IReadModelFacade, ReadModelFacade>();
//Scan for commandhandlers and eventhandlers
services.Scan(scan => scan
.FromAssemblies(typeof(InventoryCommandHandlers).GetTypeInfo().Assembly)
.AddClasses(classes => classes.Where(x => {
var allInterfaces = x.GetInterfaces();
return
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICommandHandler<>)) ||
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IEventHandler<>));
}))
.AsSelf()
.WithTransientLifetime()
);
//Register bus
var serviceProvider = services.BuildServiceProvider();
var registrar = new BusRegistrar(new DependencyResolver(serviceProvider));
registrar.Register(typeof(InventoryCommandHandlers));
// Add framework services.
services.AddMvc();
}
示例2: RegisterServices
public static void RegisterServices(IServiceCollection services)
{
services.Scan(scan => scan
.FromAssemblyOf<IRepositoryBase>()
.AddClasses(classes => classes.AssignableTo<IRepositoryBase>())
.AsImplementedInterfaces()
.WithScopedLifetime());
}
示例3: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
this.loggerFactory = new LoggerFactory();
loggerFactory.AddNLog();
services.AddSingleton<ILoggerFactory>(loggerFactory);
// Add framework services.
services.AddMvc()
.AddMvcOptions(o => { o.Filters.Add(new GlobalExceptionFilter(loggerFactory)); })
.AddViewLocalization()
.AddDataAnnotationsLocalization()
.AddFluentValidation(cfg => { cfg.RegisterValidatorsFromAssemblyContaining<ICommand>(); });
// Must be included .AddMvc
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new FeatureLocationExpander());
});
services.AddLocalization(options => options.ResourcesPath = "Resources");
var mapper = new MapperConfiguration(cfg =>
cfg.AddProfiles(new[]
{
"SandboxCore.Queries",
"SandboxCore.Commands"
})).CreateMapper();
services.AddSingleton<IMapper>(mapper);
var connection = @"Server=(localdb)\mssqllocaldb;Database=SandboxCore;Trusted_Connection=True;";
services.AddDbContext<CommandDbContext>(options => options.UseSqlServer(connection));
services.AddDbContext<QueryDbContext>(options => options.UseSqlServer(connection));
//
// Mediator pattern
//
services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));
services.AddScoped<MultiInstanceFactory>(p => t => p.GetRequiredServices(t));
// Use Scrutor to scan and register all classes as their implemented interfaces.
services.Scan(scan => scan
.FromAssembliesOf(typeof(IMediator), typeof(Handler))
.AddClasses()
.AsImplementedInterfaces());
//
// CommandDispatcher and QueryDispatcher
//
services.Scan(scan => scan
.FromAssembliesOf(typeof(IQueryHandler<,>), typeof(ICommandHandler<>))
.AddClasses()
.AsImplementedInterfaces());
}
示例4: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Register the IConfiguration instance which AppConfig binds against.
services.Configure<AppConfig>(Configuration);
// Add application services
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IRepository<TransactionItemListDto>, MongoRepository<TransactionItemListDto>>();
services.AddSingleton<IRepository<Account>, MongoRepository<Account>>();
services.AddSingleton<IRepository<Security>, MongoRepository<Security>>();
services.AddSingleton<IRepository<Price>, MongoRepository<Price>>();
services.AddSingleton<IRepository<Period>, MongoRepository<Period>>();
#region CQRS
services.AddMemoryCache();
//Add Cqrs services
services.AddSingleton<InProcessBus>(new InProcessBus());
services.AddSingleton<ICommandSender>(y => y.GetService<InProcessBus>());
services.AddSingleton<IEventPublisher>(y => y.GetService<InProcessBus>());
services.AddSingleton<IHandlerRegistrar>(y => y.GetService<InProcessBus>());
services.AddScoped<ISession, Session>();
services.AddSingleton<IEventStore, MongoEventStore>();
services.AddScoped<ICache, CQRSlite.Cache.MemoryCache>();
services.AddScoped<IRepository>(y => new CQRSlite.Cache.CacheRepository(new Repository(y.GetService<IEventStore>()), y.GetService<IEventStore>(), y.GetService<ICache>()));
services.AddTransient<IReadModelFacade, ReadModelFacade>();
//Scan for commandhandlers and eventhandlers
services.Scan(scan => scan
.FromAssemblies(typeof(AccountTransactionCommandHandlers).GetTypeInfo().Assembly)
.AddClasses(classes => classes.Where(x => {
var allInterfaces = x.GetInterfaces();
return
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICommandHandler<>)) ||
allInterfaces.Any(y => y.GetTypeInfo().IsGenericType && y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IEventHandler<>));
}))
.AsSelf()
.WithTransientLifetime()
);
//Register bus
var serviceProvider = services.BuildServiceProvider();
var registrar = new BusRegistrar(new DependencyResolver(serviceProvider));
registrar.Register(typeof(AccountTransactionCommandHandlers));
//Register Mongo
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
BsonClassMap.RegisterClassMap<TransactionCreated>(cm =>
{
cm.AutoMap();
cm.SetIdMember(cm.GetMemberMap(c => c.ObjectId));
});
#endregion
// Add framework services.
services.AddMvc();
}