本文整理汇总了C#中Autofac.ContainerBuilder.Register方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.Register方法的具体用法?C# ContainerBuilder.Register怎么用?C# ContainerBuilder.Register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Autofac.ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.Register方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureWebApiContainer
public static IContainer ConfigureWebApiContainer()
{
Configure();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<EmployeeContext>().As<IDataContextAsync>().AsImplementedInterfaces().InstancePerRequest();
containerBuilder.RegisterType<ProtivitiContextProvider<EmployeeContext>>().As<IBreezeUnitofWorkAsync>().AsImplementedInterfaces().InstancePerRequest();
//containerBuilder.Register(c=> new EFContextProvider<ApplicationDbContext>()).As<EFContextProvider<ApplicationDbContext>>().InstancePerRequest();
//containerBuilder.RegisterType<BaseController>().PropertiesAutowired();
containerBuilder.Register(c => new RepositoryProvider(new RepositoryFactories())).As<IRepositoryProvider>().AsImplementedInterfaces().InstancePerRequest();
containerBuilder.RegisterType<Repository<Employee>>().As<IRepositoryAsync<Employee>>().AsImplementedInterfaces().InstancePerRequest();
// containerBuilder.RegisterType<EmployeeService>().As<IEmployeeService>().AsImplementedInterfaces().InstancePerRequest();// InstancePerApiRequest();
containerBuilder.Register(c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())
{
/*Avoids UserStore invoking SaveChanges on every actions.*/
//AutoSaveChanges = false
})).As<UserManager<ApplicationUser>>().InstancePerRequest();
containerBuilder.RegisterApiControllers(System.Reflection.Assembly.GetExecutingAssembly()).PropertiesAutowired();
IContainer container = containerBuilder.Build();
return container;
}
示例2: Configuration
public void Configuration(IAppBuilder app)
{
var configuration = new ConfigurationService();
var builder = new ContainerBuilder();
builder.Register(c => configuration).As<ConfigurationService>().SingleInstance();
builder.Register(c => configuration.CurrentConfiguration).As<IAppConfiguration>().SingleInstance();
builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
var container = AutofacBootstrapper.Initialize(builder);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Must register Autofac middleware FIRST!
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
ConfigureAuth(app, configuration.CurrentConfiguration);
AntiForgeryConfig.UniqueClaimTypeIdentifier = JwtClaimTypes.Subject;
AntiForgeryConfig.RequireSsl = true;
AntiForgeryConfig.CookieName = Prsd.Core.Web.Constants.CookiePrefix + Constants.AntiForgeryCookieName;
MvcHandler.DisableMvcResponseHeader = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelBinders.Binders.DefaultBinder = new TrimModelBinder();
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(WeeeRequiredAttributeAdapter));
ApplicationVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
示例3: Application_Start
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Store = new DocumentStore() { ConnectionStringName = "Onboarding" };
Store.Initialize();
var builder = new ContainerBuilder();
Store.Conventions.RegisterIdConvention<User>((dbname, commands, user) => "users/" + user.UserName);
builder.Register(c => {
var store = new DocumentStore {
ConnectionStringName = "Onboarding",
DefaultDatabase = "Onboarding"
}.Initialize();
return store;
}).As<IDocumentStore>().SingleInstance();
builder.Register(c => c.Resolve<IDocumentStore>().OpenAsyncSession()).As<IAsyncDocumentSession>().InstancePerLifetimeScope();
}
示例4: ApplicationContainer
public ApplicationContainer()
{
var builder = new ContainerBuilder();
//builder.RegisterModule<WhiteboxProfilingModule>();
//default to InstancePerDependency, i.e., they it will make a new
//one each time someone asks for one
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.GetInterfaces().Contains(typeof(ICommand))).InstancePerLifetimeScope();
builder.Register(c => LocalizationManager).SingleInstance();
if (Settings.Default.MruProjects==null)
{
Settings.Default.MruProjects = new MostRecentPathsList();
}
builder.RegisterInstance(Settings.Default.MruProjects).SingleInstance();
//this is to prevent some problems we were getting while waiting for a browser to navigate and being forced to call Application.DoEvents().
//HtmlThumbnailer & ConfigurationDialog, at least, use this.
builder.Register(c => new NavigationIsolator()).InstancePerLifetimeScope();
builder.Register<HtmlThumbNailer>(c => new HtmlThumbNailer(c.Resolve<NavigationIsolator>())).SingleInstance();
builder.Register<BookThumbNailer>(c => new BookThumbNailer(c.Resolve<HtmlThumbNailer>())).SingleInstance();
_container = builder.Build();
Application.ApplicationExit += OnApplicationExit;
}
示例5: RegisterConfigurationSections
private static void RegisterConfigurationSections(ContainerBuilder builder)
{
builder.Register(_ => ImportSettings.Current).SingleInstance();
builder.Register(c => c.Resolve<ImportSettings>().General).SingleInstance();
builder.Register(c => c.Resolve<ImportSettings>().Doaj).SingleInstance();
builder.Register(c => c.Resolve<ImportSettings>().Ulrichs).SingleInstance();
}
示例6: Fetch
public void Fetch()
{
var person = EntityCreator.Create<E.Person>();
var people = new InMemoryDbSet<E.Person> { person };
var context = new Mock<IEntityContext>(MockBehavior.Strict);
context.Setup(_ => _.People).Returns(people);
context.Setup(_ => _.Dispose());
var personViewFactory = new Mock<IObjectFactory<IPersonView>>(MockBehavior.Strict);
personViewFactory
.Setup(_ => _.FetchChild(It.Is<int>(__ => __ == person.Id)))
.Returns(Mock.Of<IPersonView>());
var container = new ContainerBuilder();
container.Register<IEntityContext>(_ => context.Object);
container.Register<IObjectFactory<IPersonView>>(_ => personViewFactory.Object);
using (container.Build().Bind(() => IoC.Container))
{
var actual = DataPortal.Fetch<PersonCollectionView>();
Assert.IsNotNull(actual);
Assert.AreEqual(1, actual.Count, "Count");
}
context.VerifyAll();
personViewFactory.VerifyAll();
}
示例7: Load
/// <summary>
/// Override to add registrations to the container.
/// </summary>
/// <param name="builder">The builder through which components can be registered.</param>
/// <remarks>
/// Note that the ContainerBuilder parameter is not the same one
/// that the module is being registered by (i.e. it can have its own defaults).
/// </remarks>
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
// Register the global application objects
{
// Utilities
builder.Register(c => new ApplicationConstants())
.As<ApplicationConstants>();
builder.Register(c => new FileConstants(c.Resolve<ApplicationConstants>()))
.As<FileConstants>();
builder.Register(c => new XmlConfiguration(
ExecutorConfigurationKeys.ToCollection()
.Append(CommunicationConfigurationKeys.ToCollection())
.Append(DiagnosticsConfigurationKeys.ToCollection())
.ToList(),
ExecutorConstants.ConfigurationSectionApplicationSettings))
.As<IConfiguration>()
.SingleInstance();
RegisterLoggers(builder);
RegisterProfiler(builder);
RegisterDiagnostics(builder);
}
}
示例8: RegisterRepositories
public void RegisterRepositories(ContainerBuilder builder)
{
builder.Register(context =>
{
var documentStore = RavenHelper.CreateDocumentStore();
return documentStore;
})
.As<IDocumentStore>()
.SingleInstance();
builder.Register(context => context.Resolve<IDocumentStore>().OpenSession()).As<IDocumentSession>();
builder.RegisterType<RavenBuildRepository>().As<IBuildRepository>();
builder.RegisterType<RavenProjectRepository>().As<IProjectRepository>();
builder.RegisterType<RavenFileRepository>().As<IFileRepository>();
//Bind<IFileStorage>().To<RavenFileStorage_Collection>();
builder.RegisterType<RavenFileStorage_Attachment>().As<IFileStorage>();
builder.RegisterType<RavenAttachmentManager>().As<IRavenAttachmentManager>();
builder.RegisterType<RavenDeployRepository>().As<IDeployRepository>();
builder.RegisterType<RavenDeployStateRepository>().As<IDeployStateRepository>();
builder.RegisterType<RavenSystemLogRepository>().As<ISystemLogRepository>();
builder.RegisterType<RavenMembershipRepository>().As<IMembershipRepository>();
builder.RegisterType<RavenEmailQueueRepository>().As<IEmailQueueRepository>();
builder.RegisterType<RavenCredentialsRepository>().As<ICredentialsRepository>();
builder.RegisterType<RavenSystemSettingsRepository>().As<ISystemSettingsRepository>();
builder.RegisterType<RavenRazorTemplateRepository>().As<IRazorTemplateRepository>();
builder.RegisterType<RavenCleanupRepository>().As<ICleanupRepository>();
builder.RegisterType<RavenOfflineDeploymentRepository>().As<IOfflineDeploymentRepository>();
builder.RegisterType<RavenSystemRoleRepository>().As<ISystemRoleRepository>();
builder.RegisterType<RavenBuildPurgeRuleRepository>().As<IBuildPurgeRuleRepository>();
builder.RegisterType<RavenConnectionSettingRepository>().As<IConnectionSettingRepository>();
builder.RegisterType<RavenPermissionRepository>().As<IPermissionRepository>();
}
示例9: RegisterNewOrUpdateDetailsAsyncEditExsitingAndShouldReturnOne
public async Task RegisterNewOrUpdateDetailsAsyncEditExsitingAndShouldReturnOne()
{
ContainerBuilder autoFacBuilder = new ContainerBuilder();
autoFacBuilder.Register(c =>
{
var iHaslContext = Substitute.For<IHaslContext>();
return iHaslContext;
}).InstancePerDependency();
autoFacBuilder.Register(c =>
{
var iEntityFacade = Substitute.For<IDoctorFacade>();
iEntityFacade.GetDetails(Arg.Any<IHaslContext>(), Arg.Any<string>()).Returns(new Doctor { RegisteredUserId = 5 });
iEntityFacade.UpdateAsync(Arg.Any<IHaslContext>(), Arg.Any<Doctor>()).Returns(1);
return iEntityFacade;
}).InstancePerDependency();
autoFacBuilder.RegisterType<DoctorManager>().As<IDoctorManager>().InstancePerDependency();
IContainer iContainer = autoFacBuilder.Build();
var iCurdLogicManager = iContainer.Resolve<IDoctorManager>();
var modifiedCount = await iCurdLogicManager.RegisterNewOrUpdateDetailsAsync(new Doctor()
{
HospitalId = 456789,
RegisteredUserId = 5,
});
Assert.Equal<int>(1, modifiedCount);
}
示例10: RegisterNewOrUpdateDetailsAsyncAndShouldReturnOne
public async Task RegisterNewOrUpdateDetailsAsyncAndShouldReturnOne()
{
ContainerBuilder autoFacBuilder = new ContainerBuilder();
autoFacBuilder.Register(c =>
{
var iHaslContext = Substitute.For<IHaslContext>();
return iHaslContext;
}).InstancePerDependency();
autoFacBuilder.Register(c =>
{
var iEntityFacade = Substitute.For<ICareGiverFacade>();
iEntityFacade.GetDetails(Arg.Any<IHaslContext>(), Arg.Any<string>()).Returns((CareGiver)null);
iEntityFacade.AddNew(Arg.Any<IHaslContext>(), Arg.Any<CareGiver>()).Returns(1);
return iEntityFacade;
}).InstancePerDependency();
autoFacBuilder.RegisterType<CareGiverFacade>().As<ICareGiverManager>().InstancePerDependency();
IContainer iContainer = autoFacBuilder.Build();
var iCurdLogicManager = iContainer.Resolve<ICareGiverManager>();
var modifiedCount = await iCurdLogicManager.RegisterNewOrUpdateDetailsAsync(new Entities.CareGiver()
{
RegisteredUserId = 1,
Description = "Test Caregiver",
});
Assert.Equal<int>(1, modifiedCount);
}
示例11: RegisterApplicationComponents
static void RegisterApplicationComponents(ContainerBuilder builder, WebConfigSettings settings)
{
builder.RegisterModule<NLogModule>();
builder.RegisterType<ViewInfo>().InstancePerHttpRequest();
builder.RegisterType<SingleWebServerApplicationState>().As<IApplicationState>().InstancePerHttpRequest();
builder.RegisterType<DefaultApplicationBus>().As<IApplicationBus>().InstancePerHttpRequest();
builder.RegisterInstance(settings).As<ISettings>().SingleInstance();
//NOTE: registering a single RavenDB document store instance for the entire application, and a single instance per HttpRequest for the IDocumentSession
builder.Register(x => RavenDBConfig.CreateDocumentStore()).As<IDocumentStore>().SingleInstance();
builder.Register(x => x.Resolve<IDocumentStore>().OpenSession()).As<IDocumentSession>().InstancePerHttpRequest();
builder.RegisterType<AuthenticationCallbackProvider>().As<IAuthenticationCallbackProvider>();
builder.RegisterType<CookieCache>().As<ICache>();
builder.RegisterType<SimpleAuthenticationController>().As<SimpleAuthenticationController>().InstancePerHttpRequest();
builder.RegisterType<DefaultSecurityEncoder>().As<ISecurityEncoder>().SingleInstance();
builder.RegisterType<DefaultSlugConverter>().As<ISlugConverter>().SingleInstance();
if(settings.RunningInProduction()) {
builder.RegisterType<CloudinaryImageStorage>().As<IImageStorage>().SingleInstance();
} else {
builder.RegisterType<LocalImageStorage>().As<IImageStorage>().SingleInstance();
}
}
示例12: RegisterServices
private static void RegisterServices(ContainerBuilder builder)
{
builder.Register(x => new EntertainmentSystemDbContext())
.As<DbContext>()
.InstancePerRequest();
builder.Register(x => new HttpCacheService())
.As<ICacheService>()
.InstancePerRequest();
builder.Register(x => new IdentifierProvider())
.As<IIdentifierProvider>()
.InstancePerRequest();
var usersServicesAssembly = Assembly.GetAssembly(typeof(UsersAdminService));
builder.RegisterAssemblyTypes(usersServicesAssembly).AsImplementedInterfaces();
var mediaServicesAssembly = Assembly.GetAssembly(typeof(MediaCategoryService));
builder.RegisterAssemblyTypes(mediaServicesAssembly).AsImplementedInterfaces();
var forumServicesAssembly = Assembly.GetAssembly(typeof(ForumPostService));
builder.RegisterAssemblyTypes(forumServicesAssembly).AsImplementedInterfaces();
var dropboxCloudStorage = Assembly.GetAssembly(typeof(DropboxCloudStorage));
builder.RegisterAssemblyTypes(dropboxCloudStorage).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DbRepository<>))
.As(typeof(IDbRepository<>))
.InstancePerRequest();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AssignableTo<BaseController>().PropertiesAutowired();
}
示例13: RegisterServices
private static void RegisterServices(ContainerBuilder builder)
{
builder.Register(x => new AppDbContext())
.As<DbContext>()
.InstancePerRequest();
builder.Register(x => new HttpCacheService())
.As<ICacheService>()
.InstancePerRequest();
builder.Register(x => new IdentifierProvider())
.As<IIdentifierProvider>()
.InstancePerRequest();
builder.Register(x => new DropdownListService())
.As<IDropdownListService>()
.InstancePerRequest();
var servicesAssembly = Assembly.GetAssembly(typeof(IPostsService));
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DbRepository<>))
.As(typeof(IDbRepository<>))
.InstancePerRequest();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AssignableTo<BaseController>().PropertiesAutowired();
}
示例14: Load
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder
.RegisterType<WcfAppDomainInitializer>()
.AsImplementedInterfaces()
.SingleInstance();
builder
.RegisterType<WcfServer>()
.AsImplementedInterfaces()
.SingleInstance();
builder
.RegisterCmdLineFlag("nowcf", "Do not run the embedded WCF Server; running it is the default when no action parameter is specified", NoWcfKey);
builder
.Register(c => new AutofacServiceHostFactory())
.As<AutofacServiceHostFactory>()
.SingleInstance();
builder
.Register(c => new AutofacWebServiceHostFactory())
.As<AutofacWebServiceHostFactory>()
.SingleInstance();
}
示例15: Load
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
var nhConfiguration = new NHibernateConfiguration(ConnectionString);
builder.Register(c => nhConfiguration.GetSessionFactory()).As<ISessionFactory>().SingleInstance();
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerHttpRequest();
}