WCF Rest 4依赖项注入
Wcf有很多依赖注入示例,但其中许多都涉及4.0之前的Wcf版本。我有一个用WCF4REST构建的应用程序,没有.svc文件,基本上看起来像这样WCF Rest 4依赖项注入,wcf,dependency-injection,castle-windsor,Wcf,Dependency Injection,Castle Windsor,Wcf有很多依赖注入示例,但其中许多都涉及4.0之前的Wcf版本。我有一个用WCF4REST构建的应用程序,没有.svc文件,基本上看起来像这样 public class RestService : IRestService { IUserRepository _userRepo; public RestService(IUserRepository repo) { _repo = repo; } public UserModel Get
public class RestService : IRestService
{
IUserRepository _userRepo;
public RestService(IUserRepository repo)
{
_repo = repo;
}
public UserModel GetUser(int id)
{
return _repo.GetUserById(id);
}
}
public interface IRestService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/User/{id}",
BodyStyle = WebMessageBodyStyle.Bare)]
UserModel GetUserById(int id);
}
就像我说的,没有svc文件,整个九码。在global.asax文件中仅此项
RouteTable.Routes.Add(new ServiceRoute("RestService", new WebServiceHostFactory(), typeof(RestService)));
因此,我想使用Castle Windsor注入用户存储库
使用Castle Windsor 4.0实现来设置服务的最佳方法是什么?WCF中的依赖项注入不是很容易实现的。基本上,您需要实现一个
IServiceProvider
,当需要创建服务的新实例时,将调用它。然后,您可以使用Windsor或其他一些IoC工具来获取存储库实例
你可以在关于实例提供者的帖子中找到这样一个IoC容器的例子。我认为WCF设施支持它,不是吗?它确实支持,我在许多应用程序中使用过WCF设施,它可以很方便地实现这一点。没错,或者至少,除非你使用的是温莎的WCF设施,它将WCF世界与容器连接起来。