Asp.net 5 web api 依赖解析器
Asp.net 5 web api dependency resolver
我已经使用 asp.net 5 web api 模板创建了项目。 startup.cs
文件内容是这样的
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseMvc();
}
}
我想使用像 Autofac、StructureMap 和 Ninject 这样的 IoC 框架。但是如何使用框架更改默认的 web api 依赖解析器?
例如 StructureMap 容器设置如下:
var container = new StructureMap.Container(m=>m.Scan(x =>
{
x.TheCallingAssembly();
x.WithDefaultConventions();
}));
但是不能在网页上设置api生活
此处的文档可能会有所帮助:
查看 StructureMap.Dnx 项目:
https://github.com/structuremap/structuremap.dnx
用法
该包包含一个 public 扩展方法 Populate
。
它用于使用一组 ServiceDescriptors
或 IServiceCollection
.
填充 StructureMap 容器
例子
using System;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddWhatever();
var container = new Container();
// You can populate the container instance in one of two ways:
// 1. Use StructureMap's `Configure` method and call
// `Populate` on the `ConfigurationExpression`.
container.Configure(config =>
{
// Register stuff in container, using the StructureMap APIs...
config.Populate(services);
});
// 2. Call `Populate` directly on the container instance.
// This will internally do a call to `Configure`.
// Register stuff in container, using the StructureMap APIs...
// Here we populate the container using the service collection.
// This will register all services from the collection
// into the container with the appropriate lifetime.
container.Populate(services);
// Finally, make sure we return an IServiceProvider. This makes
// DNX use the StructureMap container to resolve its services.
return container.GetInstance<IServiceProvider>();
}
}
我已经使用 asp.net 5 web api 模板创建了项目。 startup.cs
文件内容是这样的
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseMvc();
}
}
我想使用像 Autofac、StructureMap 和 Ninject 这样的 IoC 框架。但是如何使用框架更改默认的 web api 依赖解析器?
例如 StructureMap 容器设置如下:
var container = new StructureMap.Container(m=>m.Scan(x =>
{
x.TheCallingAssembly();
x.WithDefaultConventions();
}));
但是不能在网页上设置api生活
此处的文档可能会有所帮助:
查看 StructureMap.Dnx 项目:
https://github.com/structuremap/structuremap.dnx
用法
该包包含一个 public 扩展方法 Populate
。
它用于使用一组 ServiceDescriptors
或 IServiceCollection
.
例子
using System;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddWhatever();
var container = new Container();
// You can populate the container instance in one of two ways:
// 1. Use StructureMap's `Configure` method and call
// `Populate` on the `ConfigurationExpression`.
container.Configure(config =>
{
// Register stuff in container, using the StructureMap APIs...
config.Populate(services);
});
// 2. Call `Populate` directly on the container instance.
// This will internally do a call to `Configure`.
// Register stuff in container, using the StructureMap APIs...
// Here we populate the container using the service collection.
// This will register all services from the collection
// into the container with the appropriate lifetime.
container.Populate(services);
// Finally, make sure we return an IServiceProvider. This makes
// DNX use the StructureMap container to resolve its services.
return container.GetInstance<IServiceProvider>();
}
}