本文整理汇总了C#中Autofac.ContainerBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder类的具体用法?C# ContainerBuilder怎么用?C# ContainerBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ContainerBuilder类属于Autofac命名空间,在下文中一共展示了ContainerBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public void Init() {
_settingsA = new ShellSettings { Name = "Alpha" };
_settingsB = new ShellSettings { Name = "Beta", };
_routes = new RouteCollection();
var rootBuilder = new ContainerBuilder();
rootBuilder.Register(ctx => _routes);
rootBuilder.RegisterType<ShellRoute>().InstancePerDependency();
rootBuilder.RegisterType<RunningShellTable>().As<IRunningShellTable>().SingleInstance();
rootBuilder.RegisterModule(new WorkContextModule());
rootBuilder.RegisterType<WorkContextAccessor>().As<IWorkContextAccessor>().InstancePerMatchingLifetimeScope("shell");
rootBuilder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>();
rootBuilder.RegisterType<ExtensionManager>().As<IExtensionManager>();
rootBuilder.RegisterType<StubCacheManager>().As<ICacheManager>();
rootBuilder.RegisterType<StubAsyncTokenProvider>().As<IAsyncTokenProvider>();
rootBuilder.RegisterType<StubParallelCacheContext>().As<IParallelCacheContext>();
_rootContainer = rootBuilder.Build();
_containerA = _rootContainer.BeginLifetimeScope(
"shell",
builder => {
builder.Register(ctx => _settingsA);
builder.RegisterType<RoutePublisher>().As<IRoutePublisher>().InstancePerMatchingLifetimeScope("shell");
});
_containerB = _rootContainer.BeginLifetimeScope(
"shell",
builder => {
builder.Register(ctx => _settingsB);
builder.RegisterType<RoutePublisher>().As<IRoutePublisher>().InstancePerMatchingLifetimeScope("shell");
});
}
示例2: Init
public void Init() {
_contentDefinitionManager = new Mock<IContentDefinitionManager>();
var builder = new ContainerBuilder();
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterInstance(_contentDefinitionManager.Object);
builder.RegisterInstance(new Mock<IContentDisplay>().Object);
builder.RegisterType<AlphaPartHandler>().As<IContentHandler>();
builder.RegisterType<BetaPartHandler>().As<IContentHandler>();
builder.RegisterType<GammaPartHandler>().As<IContentHandler>();
builder.RegisterType<DeltaPartHandler>().As<IContentHandler>();
builder.RegisterType<EpsilonPartHandler>().As<IContentHandler>();
builder.RegisterType<FlavoredPartHandler>().As<IContentHandler>();
builder.RegisterType<StyledHandler>().As<IContentHandler>();
builder.RegisterType<DefaultShapeTableManager>().As<IShapeTableManager>();
builder.RegisterType<ShapeTableLocator>().As<IShapeTableLocator>();
builder.RegisterType<DefaultShapeFactory>().As<IShapeFactory>();
builder.RegisterType<DefaultContentDisplay>().As<IContentDisplay>();
builder.RegisterType<StubExtensionManager>().As<IExtensionManager>();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
_session = _sessionFactory.OpenSession();
builder.RegisterInstance(new TestSessionLocator(_session)).As<ISessionLocator>();
_container = builder.Build();
_manager = _container.Resolve<IContentManager>();
}
示例3: Register
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
builder.RegisterType<ShippingByWeightService>().As<IShippingByWeightService>().InstancePerHttpRequest();
//data layer
var dataSettingsManager = new DataSettingsManager();
var dataProviderSettings = dataSettingsManager.LoadSettings();
if (dataProviderSettings != null && dataProviderSettings.IsValid())
{
//register named context
builder.Register<IDbContext>(c => new ShippingByWeightObjectContext(dataProviderSettings.DataConnectionString))
.Named<IDbContext>("nop_object_context_shipping_weight_zip")
.InstancePerHttpRequest();
builder.Register<ShippingByWeightObjectContext>(c => new ShippingByWeightObjectContext(dataProviderSettings.DataConnectionString))
.InstancePerHttpRequest();
}
else
{
//register named context
builder.Register<IDbContext>(c => new ShippingByWeightObjectContext(c.Resolve<DataSettings>().DataConnectionString))
.Named<IDbContext>("nop_object_context_shipping_weight_zip")
.InstancePerHttpRequest();
builder.Register<ShippingByWeightObjectContext>(c => new ShippingByWeightObjectContext(c.Resolve<DataSettings>().DataConnectionString))
.InstancePerHttpRequest();
}
//override required repository with our custom context
builder.RegisterType<EfRepository<ShippingByWeightRecord>>()
.As<IRepository<ShippingByWeightRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_shipping_weight_zip"))
.InstancePerHttpRequest();
}
示例4: BuildContainer
public static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
builder.RegisterType<StatisticsViewModel>();
builder.RegisterType<DetailsViewModel>();
builder.RegisterType<MainView>();
builder.RegisterType<MainViewModel>();
builder.RegisterType<CameraView>();
builder.RegisterType<CameraViewModel>();
builder.RegisterType<MvxMessengerHub>().As<IMvxMessenger>().SingleInstance();
builder.RegisterType<ApplicationDataRepository>().As<IApplicationDataRepository>();
builder.RegisterAssemblyTypes(typeof(MyShuttleClient).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(MyShuttleClient).Assembly)
.Where(t => t.Name.EndsWith("ServiceSingleton"))
.AsImplementedInterfaces().SingleInstance();
builder.RegisterType<MyShuttleClient>().As<IMyShuttleClient>();
return builder.Build();
}
示例5: RegisterContainer
private void RegisterContainer() {
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
_pluginServicee.Startup(builder);
_container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
}
示例6: Application_Start
protected void Application_Start()
{
Database.SetInitializer(new TrainingDbInitializer());
var builder = new ContainerBuilder();
builder.RegisterType<EditableUserRepository>().As<IEditableRepository<User>>();
builder.RegisterType<EditableSysOpRepository>().As<IEditableRepository<SysOp>>();
builder.RegisterType<CompanyRepository>().As<IEditableRepository<Company>>();
builder.RegisterType<CommentRepository>().As<IEditableRepository<Comment>>();
builder.RegisterType<CourseRepository>().As<IEditableRepository<Course>>();
builder.RegisterType<EmployeeCourseRepository>().As<IEditableRepository<EmployeeCourse>>();
builder.RegisterType<DivisionRepository>().As<IEditableRepository<Division>>();
builder.RegisterType<EmployeeRepository>().As<IEditableRepository<Employee>>();
builder.RegisterType<EmployeeCourseStatusRepository>().As<IEditableRepository<EmployeeCourseStatus>>();
builder.RegisterType<InstructorRepository>().As<IEditableRepository<Instructor>>();
builder.RegisterType<RaceRepository>().As<IEditableRepository<Race>>();
builder.RegisterType<RegionRepository>().As<IEditableRepository<Region>>();
builder.RegisterType<ResultRepository>().As<IEditableRepository<Result>>();
builder.RegisterType<EmployeeDocumentRepository>().As<IEditableRepository<EmployeeDocument>>();
builder.RegisterType<TrainingScheduleRepository>().As<IEditableRepository<TrainingSchedule>>();
builder.RegisterType<EmployeeTrainingScheduleRepository>().As<IEditableRepository<EmployeeTrainingSchedule>>();
builder.RegisterType<TrainingManualRepository>().As<IEditableRepository<TrainingManual>>();
builder.RegisterType<CompanyBudgetRepository>().As<IEditableRepository<CompanyBudget>>();
builder.RegisterType<DivisionBudgetRepository>().As<IEditableRepository<DivisionBudget>>();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.MessageHandlers.Add(new BasicTrainingAuthMessageHandler());
}
示例7: Init
public static void Init()
{
var serviceBusConnectionString = ConfigurationManager.ConnectionStrings["ServiceBus"].ConnectionString;
var builder = new ContainerBuilder();
var typeProvider = new AssemblyScanningTypeProvider(
Assembly.GetAssembly(typeof(ExampleEventHandler)),
Assembly.GetAssembly(typeof(ExampleCommand))
);
var applicationName = Assembly.GetExecutingAssembly().ToNimbusName();
var instanceName = Environment.MachineName;
builder.RegisterNimbus(typeProvider);
builder.RegisterType<NimbusLogger>().As<ILogger>();
builder.Register(componetContext => new BusBuilder()
.Configure()
.WithConnectionString(serviceBusConnectionString)
.WithNames(applicationName, instanceName)
.WithTypesFrom(typeProvider)
.WithDefaultTimeout(TimeSpan.FromSeconds(30))
.WithAutofacDefaults(componetContext)
.Build())
.As<IBus>()
.AutoActivate()
.OnActivated(o => o.Instance.Start())
.SingleInstance();
_container = builder.Build();
}
示例8: Configure
public static void Configure()
{
var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray();
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblies)
.Where(t => typeof(IStartable).IsAssignableFrom(t))
.As<IStartable>()
.SingleInstance();
builder.RegisterAssemblyModules(assemblies);
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
// Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;
// Set the dependency resolver for MVC.
var mvcResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);
}
示例9: GetMinimalBuilder
public static ContainerBuilder GetMinimalBuilder()
{
var builder = new ContainerBuilder();
builder.Register(c => ReadAppConfigConfig()).SingleInstance();
return builder;
}
示例10: RegisterTypes
public static void RegisterTypes()
{
var builder = new ContainerBuilder();
builder.RegisterType<VideoRepository>().As<IVideoRepository>().InstancePerLifetimeScope();
builder.RegisterType<OrderRepository>().As<IOrderRepository>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<IUserRepository>();
builder.RegisterType<MockPaymentProcessor>().As<IPaymentProcessor>().InstancePerLifetimeScope();
builder.RegisterType<StoreController>()
.UsingConstructor(typeof(IVideoRepository), typeof(IUserRepository));
builder.RegisterType<CartController>()
.UsingConstructor(typeof(IVideoRepository));
builder.RegisterType<WishlistController>()
.UsingConstructor(typeof(IUserRepository), typeof(IVideoRepository));
builder.RegisterType<OrderController>()
.UsingConstructor(typeof(IVideoRepository), typeof(IOrderRepository), typeof(IUserRepository), typeof(IPaymentProcessor));
builder.RegisterType<HomeController>()
.UsingConstructor(typeof(IVideoRepository), typeof(IOrderRepository), typeof(IUserRepository));
builder.RegisterType<VideosController>()
.UsingConstructor(typeof(IVideoRepository));
builder.RegisterType<OrdersController>()
.UsingConstructor(typeof(IOrderRepository));
builder.RegisterType<UsersController>()
.UsingConstructor(typeof(IUserRepository));
builder.RegisterType<AccountController>()
.UsingConstructor(typeof(IUserRepository));
var container = builder.Build();
var resolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(resolver);
}
示例11: RegisterTypes
private static void RegisterTypes(ContainerBuilder builder)
{
string baseDirectory = HttpContext.Current.Server.MapPath("~/App_Data") + ConfigurationManager.AppSettings["dataFolderName"];
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var repositoryAssemblies = Assembly.Load("FileRepository");
builder.RegisterAssemblyTypes(repositoryAssemblies).AsImplementedInterfaces().WithParameter(new NamedParameter("path", baseDirectory));
var serviceAssemblies = Assembly.Load("Services");
builder.RegisterAssemblyTypes(serviceAssemblies).AsImplementedInterfaces();
var configurationManagerWrapperAssembly = Assembly.Load("ConfigurationManagerWrapper");
builder.RegisterAssemblyTypes(configurationManagerWrapperAssembly).AsImplementedInterfaces();
var fileAssembly = Assembly.Load("SystemFileAdapter");
builder.RegisterAssemblyTypes(fileAssembly).AsImplementedInterfaces();
//builder.RegisterType<NLogLogger>().As<ILogger>();
builder.Register(c => new NLogLogger()).As<ILogger>().InstancePerHttpRequest();
builder.RegisterType<UserHub>();
builder.RegisterType<TodoHub>();
// builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.RegisterFilterProvider();
}
示例12: Init
public void Init(IEnumerable<Type> dataMigrations) {
var builder = new ContainerBuilder();
_folders = new StubFolders();
var contentDefinitionManager = new Mock<IContentDefinitionManager>().Object;
builder.RegisterInstance(new ShellSettings { DataTablePrefix = "TEST_"});
builder.RegisterType<SqlServerDataServicesProvider>().As<IDataServicesProvider>();
builder.RegisterType<DataServicesProviderFactory>().As<IDataServicesProviderFactory>();
builder.RegisterType<NullInterpreter>().As<IDataMigrationInterpreter>();
builder.RegisterInstance(_folders).As<IExtensionFolders>();
builder.RegisterInstance(contentDefinitionManager).As<IContentDefinitionManager>();
builder.RegisterType<ExtensionManager>().As<IExtensionManager>();
builder.RegisterType<DataMigrationManager>().As<IDataMigrationManager>();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
builder.RegisterType<StubParallelCacheContext>().As<IParallelCacheContext>();
builder.RegisterType<StubAsyncTokenProvider>().As<IAsyncTokenProvider>();
_session = _sessionFactory.OpenSession();
builder.RegisterInstance(new DefaultContentManagerTests.TestSessionLocator(_session)).As<ISessionLocator>().As<ITransactionManager>();
foreach(var type in dataMigrations) {
builder.RegisterType(type).As<IDataMigration>();
}
_container = builder.Build();
_container.Resolve<IExtensionManager>();
_dataMigrationManager = _container.Resolve<IDataMigrationManager>();
_repository = _container.Resolve<IRepository<DataMigrationRecord>>();
_transactionManager = _container.Resolve<ITransactionManager>();
InitDb();
}
示例13: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterModule(new RepositoryModule());
builder.RegisterType(typeof(SampleArchContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();
}
示例14: DictionaryDocument_Operations
public void DictionaryDocument_Operations()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new SolrNetModule("http://localhost:8983/solr"));
var container = builder.Build();
var m = container.Resolve<ISolrOperations<Dictionary<string, object>>>();
}
示例15: TheyShouldBeResolvable
public void TheyShouldBeResolvable()
{
var interceptorTypes = new[] {typeof (DummyInterceptor)};
var builder = new ContainerBuilder();
var typeProvider = Substitute.For<ITypeProvider>();
typeProvider.InterceptorTypes.Returns(interceptorTypes);
builder.RegisterNimbus(typeProvider);
using (var container = builder.Build())
using (var dependencyResolver = container.Resolve<IDependencyResolver>())
using (var scope = dependencyResolver.CreateChildScope())
{
var interceptorSetting = new GlobalOutboundInterceptorTypesSetting
{
Value = interceptorTypes
};
var outboundInterceptorFactory = new OutboundInterceptorFactory(interceptorSetting,
new PropertyInjector(Substitute.For<IClock>(),
Substitute.For<IDispatchContextManager>(),
Substitute.For<ILargeMessageBodyStore>()));
var dummyNimbusMessage = new NimbusMessage("nullQueue");
var interceptors = outboundInterceptorFactory.CreateInterceptors(scope, dummyNimbusMessage);
interceptors.Count().ShouldBe(1);
}
}