本文整理汇总了C#中IAppBuilder.UseAutofacWebApi方法的典型用法代码示例。如果您正苦于以下问题:C# IAppBuilder.UseAutofacWebApi方法的具体用法?C# IAppBuilder.UseAutofacWebApi怎么用?C# IAppBuilder.UseAutofacWebApi使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAppBuilder
的用法示例。
在下文中一共展示了IAppBuilder.UseAutofacWebApi方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Configure
public static void Configure(IAppBuilder app)
{
//Autofac configuration
var builder = new ContainerBuilder();
// Get your HttpConfiguration.
var config = new HttpConfiguration();
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<AccountController>().InstancePerRequest();
builder.RegisterType<PhoneBookContext>().InstancePerRequest();
//dependenses resolving
builder.RegisterType<MainUserManager>().As<IMainUserManager>().InstancePerRequest();
builder.Register(c => new UserStore<User>(c.Resolve<PhoneBookContext>())).AsImplementedInterfaces().InstancePerRequest();
builder.RegisterType<ApplicationUserManager>().AsSelf();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacWebApi(config);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
示例2: Configuration
public void Configuration(IAppBuilder app)
{
// Setup db context for use in DI
app.Use(async (context, next) =>
{
TestDbContext dbContext = _dbContextFactory(context);
context.Set(DbContextKey, dbContext);
await next();
dbContext.Dispose();
});
var appContainerBuilder = new ContainerBuilder();
appContainerBuilder.Register(ctx => HttpContext.Current.GetOwinContext()).As<IOwinContext>();
appContainerBuilder.Register(c => c.Resolve<IOwinContext>().Get<TestDbContext>(DbContextKey)).As<TestDbContext>();
appContainerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var appContainer = appContainerBuilder.Build();
app.UseAutofacMiddleware(appContainer);
var httpConfig = GetWebApiConfiguration();
httpConfig.DependencyResolver = new AutofacWebApiDependencyResolver(appContainer);
app.UseWebApi(httpConfig);
app.UseAutofacWebApi(httpConfig);
}
示例3: ConfigureMobileApp
/// <summary>
/// Configures the webApi dependency injection to start the service.
/// </summary>
/// <param name="app">The Azure app service mobile app to configure.</param>
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.Services.Add(typeof(IExceptionLogger), new ApplicationInsightsExceptionLogger());
IContainer container = ConfigureWebApiDependencies();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
config.MapHttpAttributeRoutes();
new MobileAppConfiguration()
.AddMobileAppHomeController()
.MapApiControllers()
.ApplyTo(config);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
// Configure Application Insights Telemetry.
TelemetryConfiguration.Active.InstrumentationKey = SystemContext.Current.Environment.InstrumentationKey;
var rootPath = HttpContext.Current.Server.MapPath("~");
var serverPath = Path.Combine(rootPath, "bin");
using (var scope = container.BeginLifetimeScope())
{
var repository = scope.Resolve<IRepository>();
// Do database initialization synchronously
repository.InitializeDatabaseIfNotExisting(serverPath).ConfigureAwait(false).GetAwaiter().GetResult();
}
}
示例4: Configuration
public void Configuration(IAppBuilder app)
{
var autoFacBuilder = new ContainerBuilder();
HttpConfiguration config = new HttpConfiguration();
ConfigureAutoFac(autoFacBuilder);
autoFacBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
autoFacBuilder.RegisterWebApiFilterProvider(config);
autoFacBuilder.RegisterWebApiModelBinders(Assembly.GetExecutingAssembly());
var container = autoFacBuilder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
ConfigureAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
config.EnsureInitialized();
}
示例5: Configuration
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
ConfigureFilters(app, config);
ConfigureWebApi(app, config);
var container = ConfigureDI(app, config);
app.UseAutofacWebApi(config);
app.UseAutofacMiddleware(container);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
Provider = container.Resolve<IOAuthBearerAuthenticationProvider>()
});
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/auth/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(7),
Provider = container.Resolve<IOAuthAuthorizationServerProvider>()
});
app.UseWebApi(config);
}
示例6: Configure
public static void Configure(IAppBuilder app)
{
if (app == null)
throw new ArgumentNullException("app");
IContainer container = IocConfiguration.BuildContainer(actorSystemName: "Wakka");
app.UseAutofacMiddleware(container);
HttpConfiguration webApiConfiguration = new HttpConfiguration
{
// Only used when mapping attribute routes.
DependencyResolver = new AutofacWebApiDependencyResolver(container)
};
app.UseAutofacWebApi(webApiConfiguration); // Share OWIN lifetime scope.
webApiConfiguration.MapHttpAttributeRoutes();
webApiConfiguration.EnsureInitialized();
app.UseWebApi(webApiConfiguration);
// Explicitly start the actor system when the OWIN app is started.
ActorSystem actorSystem = container.ResolveNamed<ActorSystem>("Wakka");
// Try to gracefully shut down the actor system when the host is shutting down.
AppProperties appProperties = new AppProperties(app.Properties);
appProperties.OnAppDisposing.Register(() =>
{
actorSystem.Shutdown();
actorSystem.AwaitTermination(
timeout: TimeSpan.FromSeconds(5)
);
});
}
示例7: Configuration
public void Configuration(IAppBuilder app)
{
// Loading a new configuration.
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// Loading formatters.
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
// Resolving Dependencies
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterModule(new DataLayerModule());
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// Configuring Mappings
DataLayerMapping.AutoMapperStart();
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
示例8: ConfigureDependencies
private void ConfigureDependencies(IAppBuilder app,
HttpConfiguration config)
{
// configure container
ContainerBuilder builder = new ContainerBuilder();
// bind all webapi controllers
builder.RegisterApiControllers(Assembly.GetExecutingAssembly())
.InstancePerRequest();
// register context
builder.RegisterType<TemplateAppContext>()
.AsSelf()
.InstancePerRequest();
// register services
builder.RegisterType<ConfigurationService>()
.As<IConfigurationService>()
.InstancePerLifetimeScope();
builder.RegisterType<UserService>()
.As<IUserService>()
.InstancePerRequest();
// create container
IContainer container = builder.Build();
// bind app to autofac
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
}
示例9: Register
/// <summary>
/// Registers the configuration
/// </summary>
public static IContainer Register(IAppBuilder app, HttpConfiguration configuration, IServiceFacade serviceFacade = null)
{
Condition.Requires(configuration, "configuration").IsNotNull();
var builder = new ContainerBuilder();
builder.RegisterModule(new ServiceModule(serviceFacade));
// controllers
builder.RegisterApiControllers(typeof (ControllerBase).Assembly);
// request
builder.RegisterHttpRequestMessage(configuration);
// authorization
builder.RegisterType<AuthorizationServerProvider>().As<IOAuthAuthorizationServerProvider>().SingleInstance();
// set resolver
var container = builder.Build();
// set the dependency resolver for Web API
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.DependencyResolver = resolver;
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(configuration);
ConfigureOAuth(app, container);
app.UseWebApi(configuration);
return container;
}
示例10: Register
public static void Register(IAppBuilder app, HttpConfiguration config)
{
var aspNetIdentityAssembly = typeof(IMarkerAspNetIdentityInterface).Assembly;
var cornhouseCoreAssembly = typeof (IMarkerCornhouseCoreInterface).Assembly;
var executingAssembly = Assembly.GetExecutingAssembly();
var builder = new ContainerBuilder();
builder.Register(c => InitializeRavenDb.Initialize(aspNetIdentityAssembly)).As<IDocumentStore>().InstancePerLifetimeScope();
builder.Register(c => c.Resolve<IDocumentStore>().OpenAsyncSession()).As<IAsyncDocumentSession>().InstancePerRequest();
builder.Register(c => HttpContext.Current.User).InstancePerRequest();
builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
builder.RegisterType<ApplicationRoleManager>().AsSelf().InstancePerRequest();
builder.RegisterType<RavenUserStore>().AsSelf().InstancePerRequest();
builder.RegisterType<RavenRoleStore>().AsSelf().InstancePerRequest();
builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest();
builder.RegisterApiControllers(executingAssembly);
builder.RegisterApiControllers(aspNetIdentityAssembly);
builder.RegisterAssemblyTypes(executingAssembly).AsImplementedInterfaces().InstancePerRequest();
builder.RegisterAssemblyTypes(aspNetIdentityAssembly).AsImplementedInterfaces().InstancePerRequest();
builder.RegisterAssemblyTypes(cornhouseCoreAssembly).AsImplementedInterfaces().InstancePerRequest();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
}
示例11: Configuration
public void Configuration(IAppBuilder app)
{
TI.trace("StartupServerAutofac", "Begin in memory server setup");
Stopwatch sw = new Stopwatch();
sw.Start();
var configuration = new HttpConfiguration();
//configuration.EnableSystemDiagnosticsTracing();
//configuration.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
//add some temp route hacks
configuration.Routes.MapHttpRoute(
name: "Login1",
routeTemplate: "Login/Login",
defaults: new { controller = "Login", action = "LoginGet" });
WebApiConfig.RegisterRoutes(configuration);
WebApiConfig.RegisterIntegrationRoutes(configuration);
DB_TestingHelper dbHelper = DefaultDbTestingHelper.GetDefaultDbTestingHelper();
ContainerBuilder b = IOCHelper.InitializeServerWithAutofacContainer(dbHelper.Server_DistributrExmxConnection, dbHelper.MongoConnectionString, "win", dbHelper.MongoAuditingConnectionString);
Assembly a = Assembly.GetAssembly(typeof(Distributr.WebApi.ApiControllers.CommandController));
b.RegisterApiControllers(a);
Autofac.IContainer c = b.Build();
System.Web.Http.Dependencies.IDependencyResolver r = new AutofacWebApiDependencyResolver(c) ;
configuration.DependencyResolver = r;
app.UseAutofacMiddleware(c);
app.UseAutofacWebApi(configuration);
app.UseWebApi(configuration);
sw.Stop();
TI.trace("[StartupServerAutofac]",
string.Format("Time taken to setup in memory server {0}", sw.ElapsedMilliseconds));
}
示例12: Start
protected virtual void Start(IAppBuilder app, IContainer container)
{
var config = CreateHttpConfiguration();
ConfigureWebApi(config);
if (Settings.ShowExceptionDetails)
{
app.UseErrorPage(new ErrorPageOptions
{
ShowExceptionDetails = true,
ShowSourceCode = true
});
}
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
RegisterSignalR(container, app);
app.UseStageMarker(PipelineStage.MapHandler);
RegisterServices(container, app, config);
RegisterShutdown(app, container);
StartIndexingIfConfigured(container);
}
示例13: ConfigureAutofac
public void ConfigureAutofac(IAppBuilder app)
{
var builder = new ContainerBuilder();
// register the class that sets up bindings between interfaces and implementation
builder.RegisterModule(new WebAutofacModule());
// register configuration provider
builder.RegisterType<ConfigurationProvider>().As<IConfigurationProvider>();
// register Autofac w/ the MVC application
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// Register the WebAPI controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
// Setup Autofac dependency resolver for MVC
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Setup Autofac dependency resolver for WebAPI
Startup.HttpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// 1. Register the Autofac middleware
// 2. Register Autofac Web API middleware,
// 3. Register the standard Web API middleware (this call is made in the Startup.WebApi.cs)
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(Startup.HttpConfiguration);
}
示例14: Configuration
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
var container = ConfigureAutoFac.ConfigureMvc();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
//Dit op Configure laten staan, dit wordt namelijk niet in web requests gebruikt
// Remove the old formatter, add the new one.
var formatter = new JsonMediaTypeFormatter();
formatter.SerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
config.Formatters.Remove(config.Formatters.JsonFormatter);
config.Formatters.Add(formatter);
config.Formatters.Remove(config.Formatters.XmlFormatter);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
app.UseAutofacWebApi(config);
app.UseWebApi(config);
ConfigureAuth(app);
}
示例15: Configuration
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
// Remove XML formatter
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
config.Formatters.Remove(config.Formatters.XmlFormatter);
// NetIn and assign a dependency resolver for Web API to use.
var dependencyResolver = new DependencyResolver();
config.DependencyResolver = dependencyResolver;
// Filter for loggering
var logActionFilter = new Containerizer.Filters.LogActionFilter(DependencyResolver.logger);
config.Filters.Add(logActionFilter);
var exceptionLoggingFilter = new Containerizer.Filters.ExceptionLoggingFilter(DependencyResolver.logger);
config.Filters.Add(exceptionLoggingFilter);
// Make sure the Autofac lifetime scope is passed to Web API.
app.UseAutofacWebApi(config);
app.MapWebSocketPattern<ContainerProcessHandler>("/api/containers/(?<handle>.*)/run", dependencyResolver);
app.UseWebApi(config);
}