使用 MVVMLight 时如何设置 Web API DependencyResolver
How to set Web API DependencyResolver when using MVVMLight
我正在使用 OWIN 在使用 galasoft mvvmlight 框架的 wpf 桌面应用程序中自行托管 Web api Web 服务。当我尝试将我的模型数据服务之一依赖注入网络 api 控制器时,我收到有关 "Make sure that the controller has a parameterless public constructor" 的错误。
我知道 Web api 控制器不支持开箱即用的依赖注入。我看过许多示例,详细说明如何在使用 Unity 或编写您自己的时提供自定义 IDependencyResolver
。有没有直接的方法来使用 mvvmlight SimpleIoc.Default
设置 Web api 启动 class HttpConfiguration
对象的 DependencyResolver
属性?
作为解决方法,我目前通过在我的 api 控制器构造函数中调用 SimpleIoc.Default.GetInstance()
来设置我的数据服务。这行得通,并且在功能上可能与依赖注入机制相同,但依赖注入似乎更优雅。
public class webApiStartup {
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "FileServerApi",
routeTemplate: "{controller}/{filename}"
,defaults: new { Controller = "filesController" }
);
//*
//* The Following line of Code does not work. But intellisense suggests
//* that there is a conversion available ?
//*
config.DependencyResolver = (IDependencyResolver)SimpleIoc.Default;
appBuilder.UseWebApi(config);
}
}
//*
//* This constructor for the web api controller works
//* but it is not as elegant as dependency injection
//*
public class filesController : ApiController
{
ILocalDataService _LocalDataSvc = null;
public filesController() {
_LocalDataSvc = SimpleIoc.Default.GetInstance<ILocalDataService>();
}
演员阵容
config.DependencyResolver = (IDependencyResolver)SimpleIoc.Default;
将失败,因为 SimpleIoc
不是从 IDependencyResolver
.
派生的
为派生自 IDependencyResolver
的容器创建包装器
public class SimpleIocResolver : IDependencyResolver {
protected ISimpleIoc container;
public SimpleIocResolver(ISimpleIoc container) {
if (container == null) {
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType) {
try {
return container.GetInstance(serviceType);
} catch(Exception) {
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType) {
try {
return container.GetAllInstances(serviceType);
} catch (Exception) {
return new List<object>();
}
}
public IDependencyScope BeginScope() {
return new SimpleIocResolver(container);
}
public void Dispose() {
//No Op
}
}
并在配置时使用它
config.DependencyResolver = new SimpleIocResolver(SimpleIoc.Default);
然后可以重构 ApiController
public class FilesController : ApiController {
ILocalDataService _LocalDataSvc = null;
public FilesController(ILocalDataService svc) {
_LocalDataSvc = svc;
}
//...
前提是依赖已经注册到容器中
我正在使用 OWIN 在使用 galasoft mvvmlight 框架的 wpf 桌面应用程序中自行托管 Web api Web 服务。当我尝试将我的模型数据服务之一依赖注入网络 api 控制器时,我收到有关 "Make sure that the controller has a parameterless public constructor" 的错误。
我知道 Web api 控制器不支持开箱即用的依赖注入。我看过许多示例,详细说明如何在使用 Unity 或编写您自己的时提供自定义 IDependencyResolver
。有没有直接的方法来使用 mvvmlight SimpleIoc.Default
设置 Web api 启动 class HttpConfiguration
对象的 DependencyResolver
属性?
作为解决方法,我目前通过在我的 api 控制器构造函数中调用 SimpleIoc.Default.GetInstance()
来设置我的数据服务。这行得通,并且在功能上可能与依赖注入机制相同,但依赖注入似乎更优雅。
public class webApiStartup {
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "FileServerApi",
routeTemplate: "{controller}/{filename}"
,defaults: new { Controller = "filesController" }
);
//*
//* The Following line of Code does not work. But intellisense suggests
//* that there is a conversion available ?
//*
config.DependencyResolver = (IDependencyResolver)SimpleIoc.Default;
appBuilder.UseWebApi(config);
}
}
//*
//* This constructor for the web api controller works
//* but it is not as elegant as dependency injection
//*
public class filesController : ApiController
{
ILocalDataService _LocalDataSvc = null;
public filesController() {
_LocalDataSvc = SimpleIoc.Default.GetInstance<ILocalDataService>();
}
演员阵容
config.DependencyResolver = (IDependencyResolver)SimpleIoc.Default;
将失败,因为 SimpleIoc
不是从 IDependencyResolver
.
为派生自 IDependencyResolver
public class SimpleIocResolver : IDependencyResolver {
protected ISimpleIoc container;
public SimpleIocResolver(ISimpleIoc container) {
if (container == null) {
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType) {
try {
return container.GetInstance(serviceType);
} catch(Exception) {
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType) {
try {
return container.GetAllInstances(serviceType);
} catch (Exception) {
return new List<object>();
}
}
public IDependencyScope BeginScope() {
return new SimpleIocResolver(container);
}
public void Dispose() {
//No Op
}
}
并在配置时使用它
config.DependencyResolver = new SimpleIocResolver(SimpleIoc.Default);
然后可以重构 ApiController
public class FilesController : ApiController {
ILocalDataService _LocalDataSvc = null;
public FilesController(ILocalDataService svc) {
_LocalDataSvc = svc;
}
//...
前提是依赖已经注册到容器中