Web Api Unity 核心项目
Web Api Core Project with Unity
我正在 .net 核心中创建 Web api。我正在使用 Mac Sierra 10.12.3 和 visual studio 作为 Mac 版本 7.3.3(build 12)。作为参考,我在下面使用 Github 项目。
https://github.com/unitycontainer/examples
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider() // Add Unity as default Service Provider
.UseStartup<Startup>()
.Build();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterSingleton<IUserRepository, UserRepository>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add MVC as usual
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
此 link 代码在 Windows 中有效,但在 Mac 中无效。
我正在尝试 运行 这个项目它给我以下错误
Application startup exception: System.ArgumentNullException: Value
cannot be null. Parameter name: source at
System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Loaded
'/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.0.5/System.Diagnostics.Debug.dll'.
Skipped loading symbols. Module is optimized and the debugger option
'Just My Code' is enabled. [41m[1m[37mcrit[39m[22m[49m:
Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
Microsoft.AspNetCore.Hosting.Internal.WebHost:Critical: Application
startup exception
System.ArgumentNullException: Value cannot be null. Parameter name:
source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1
source) at
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Application startup exception System.ArgumentNullException: Value cannot be null. Parameter name: source at
System.Linq.Enumerable.Reverse[TSource](IEnumerable'1 source) at
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Exception thrown: 'System.ArgumentNullException' in
Microsoft.AspNetCore.Hosting.dll
编辑:
program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Start.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddSingleton(typeof(IUserRepository<User, int>), typeof(UserRepository));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
在使用上层代码并且没有统一比它工作正常后。
我们将不胜感激。
经过大量研究终于找到了解决方案
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider() // Add Unity as default Service Provider
.UseStartup<Startup>()
.Build();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// Configure Unity container
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterSingleton<IUserRepository, UserRepository>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Container could be configured via services as well.
// Just be careful not to override registrations
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Add MVC as usual
services.AddMvcCore().AddJsonFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
阅读这篇博文后,我发现了差异。
https://offering.solutions/blog/articles/2017/02/07/difference-between-addmvc-addmvcore/
Nuget Unity.Microsoft.DependencyInjection
感谢@Nkosi 的帮助和支持。
我正在 .net 核心中创建 Web api。我正在使用 Mac Sierra 10.12.3 和 visual studio 作为 Mac 版本 7.3.3(build 12)。作为参考,我在下面使用 Github 项目。
https://github.com/unitycontainer/examples
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider() // Add Unity as default Service Provider
.UseStartup<Startup>()
.Build();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterSingleton<IUserRepository, UserRepository>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add MVC as usual
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
此 link 代码在 Windows 中有效,但在 Mac 中无效。
我正在尝试 运行 这个项目它给我以下错误
Application startup exception: System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.0.5/System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. [41m[1m[37mcrit[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Microsoft.AspNetCore.Hosting.Internal.WebHost:Critical: Application startup exception
System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Application startup exception System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable'1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Exception thrown: 'System.ArgumentNullException' in Microsoft.AspNetCore.Hosting.dll
编辑:
program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Start.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddSingleton(typeof(IUserRepository<User, int>), typeof(UserRepository));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
在使用上层代码并且没有统一比它工作正常后。
我们将不胜感激。
经过大量研究终于找到了解决方案
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider() // Add Unity as default Service Provider
.UseStartup<Startup>()
.Build();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// Configure Unity container
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterSingleton<IUserRepository, UserRepository>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Container could be configured via services as well.
// Just be careful not to override registrations
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Add MVC as usual
services.AddMvcCore().AddJsonFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
阅读这篇博文后,我发现了差异。
https://offering.solutions/blog/articles/2017/02/07/difference-between-addmvc-addmvcore/
感谢@Nkosi 的帮助和支持。