Autofac + WCF REST 4.0 [英] Autofac + WCF REST 4.0

查看:321
本文介绍了Autofac + WCF REST 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个WCF 4.0 REST服务,并希望使用Autofac作为DI容器。很显然,我希望能够调用的服务类(服务合同执行),它接受了一堆接口一起工作的一个参数的构造函数。这些接口要在Autofac容器中注册,我想他们解决,创造我服务类的一个实例时(而不是调用没有参数的构造函数,这是默认值)使用。

I am building a WCF 4.0 REST service and want to use Autofac as DI container. Apparently, I want to be able to call a parameterized constructor of the service class (service contract implementation), which accepts a bunch of interfaces to work with. Those interfaces are to be registered within Autofac container and I want them resolved and used when creating an instance of my service class (instead of calling not parameterized constructor, which is default).

有类似的问题,MVC控制器,这是由Autofac MVC集成封装解决。有没有什么现成的使用WCF REST 4.0?如果没有,什么是解决我的问题的最好方法是什么?例如,有什么样的MVC的DependencyResolver,我可以设置为使用Autofac?

There is similar problem with MVC controllers, which is solved by Autofac MVC Integration package. Is there anything ready-to-use for WCF REST 4.0? If no, what is the best way to solve my problem? E.g., is there anything like MVC's DependencyResolver which I can set up to use Autofac?

请注意,既然4.0,他们改变了一些概念WCF休息。例如,现在有没有 .SVC 文件,路由就足以调用所需的方法。我是很新的WCF REST 4.0,所以我想问问社区的建议,在实施一些巨大的自定义机制消磨时间了。唉,在网上快速搜索并没有给我提供一个可以接受的解决方案。

Note, that since 4.0 they changed some concepts in WCF REST. E.g., now there is no .svc file, routing is enough to call required method. I am quite new to WCF REST 4.0, so I wanted to ask community for suggestions before spending days on implementing some huge custom mechanism. Alas, quick search over internet did not provide me with an acceptable solution.

推荐答案

在您的全球应用程序的启动:

In your global application startup:

//Build a container with your service registered.
var builder = new ContainerBuilder();
builder.RegisterType<YourService>();
var container = builder.Build();

//Set AutofacHostFactory.Container with this built container.
AutofacHostFactory.Container = container;

//Use AutofacWebServiceHostFactory instead of WebServiceHostFactory
var factory = new AutofacWebServiceHostFactory();

//Add your routes
RouteTable.Routes.Add(new ServiceRoute("YourServiceUrl", factory, typeof(YourService)));

这就是全部。

That's all.

这篇关于Autofac + WCF REST 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆