Add AutoFac to the MVC sample
This commit is contained in:
parent
0123b38840
commit
95aa6ad607
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"DependencyInjection": "AutoFac"
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@
|
|||
<DevelopmentServerPort>42750</DevelopmentServerPort>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="App_Data\Config.json" />
|
||||
<Content Include="Areas\Travel\Views\Flight\Fly.cshtml" />
|
||||
<Content Include="Content\bootstrap.min.css" />
|
||||
<Content Include="project.json" />
|
||||
|
|
|
|||
|
|
@ -1,24 +1,65 @@
|
|||
using Microsoft.AspNet;
|
||||
using System;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using MvcSample.Web.Filters;
|
||||
using MvcSample.Web.Services;
|
||||
|
||||
#if NET45
|
||||
using Autofac;
|
||||
using Microsoft.Framework.DependencyInjection.Autofac;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
#endif
|
||||
|
||||
namespace MvcSample.Web
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public void Configure(IBuilder app)
|
||||
{
|
||||
app.UseServices(services =>
|
||||
#if NET45
|
||||
var configuration = new Configuration()
|
||||
.AddJsonFile(@"App_Data\config.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
string diSystem;
|
||||
|
||||
if (configuration.TryGet("DependencyInjection", out diSystem) &&
|
||||
diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddMvc();
|
||||
services.AddSingleton<PassThroughAttribute>();
|
||||
services.AddSingleton<UserNameService>();
|
||||
services.AddTransient<ITestService, TestService>();
|
||||
});
|
||||
services.AddTransient<ITestService, TestService>();
|
||||
services.Add(OptionsServices.GetDefaultServices());
|
||||
|
||||
// Create the autofac container
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
|
||||
// Create the container and use the default application services as a fallback
|
||||
AutofacRegistration.Populate(
|
||||
builder,
|
||||
services,
|
||||
fallbackServiceProvider: app.ApplicationServices);
|
||||
|
||||
IContainer container = builder.Build();
|
||||
|
||||
app.UseServices(container.Resolve<IServiceProvider>());
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
app.UseServices(services =>
|
||||
{
|
||||
services.AddMvc();
|
||||
services.AddSingleton<PassThroughAttribute>();
|
||||
services.AddSingleton<UserNameService>();
|
||||
services.AddTransient<ITestService, TestService>();
|
||||
});
|
||||
}
|
||||
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,13 +14,17 @@
|
|||
"Microsoft.AspNet.Server.WebListener": "0.1-alpha-*",
|
||||
"Microsoft.DataAnnotations": "0.1-alpha-*",
|
||||
"Microsoft.Framework.ConfigurationModel": "0.1-alpha-*",
|
||||
"Microsoft.Framework.DependencyInjection": "0.1-alpha-*"
|
||||
"Microsoft.Framework.DependencyInjection": "0.1-alpha-*",
|
||||
"Microsoft.Framework.OptionsModel": "0.1-alpha-*"
|
||||
},
|
||||
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001" },
|
||||
"configurations": {
|
||||
"net45": {
|
||||
"dependencies": {
|
||||
"System.ComponentModel.DataAnnotations": ""
|
||||
"System.ComponentModel.DataAnnotations": "",
|
||||
"Microsoft.Framework.DependencyInjection.Autofac": "0.1-alpha-*",
|
||||
"Microsoft.Framework.ConfigurationModel.Json": "0.1-alpha-*",
|
||||
"Autofac": "3.3.0"
|
||||
}
|
||||
},
|
||||
"k10": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue