0
我正在构建一个WCF REST服务并希望使用Autofac作为DI容器。我想能够调用服务类的参数化构造函数。下面是我的代码:如何在WCF Rest中触发Autofac构造函数注入?
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public partial class QDService:IQDService
{
public QDService(IApplicationService appService)
{
this.appService = appService;
}
private readonly IApplicationService appService;
}
在Global.asax
然后,我设置的配置按照this chapter:
private void RegisterRoutes()
{
var builder = new ContainerBuilder();
builder.RegisterType<QDService>();
builder.RegisterType<ApplicationService>().As<IApplicationService>();
var container = builder.Build();
AutofacHostFactory.Container = container;
var factory = new AutofacWebServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("QDService", factory, typeof(QDService)));
}
下面是方法,我要打电话:
[WebInvoke(Method = "GET"
, ResponseFormat = WebMessageFormat.Xml
, BodyStyle = WebMessageBodyStyle.Bare
, UriTemplate = "/Test/{test}")]
public string Test(string test)
{
return "HelloWorld!";
}
我启动项目后浏览到
http://localhost:1685/QDService/Test/1
浏览器把我像一个例外:
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.
,我用萤火虫来跟踪它,发现这个:
我不知道是什么原因造成这一点,但之后我删除了参数的构造函数,对我来说都工作得很好。然后,我在网上进行了一次快速搜索,但什么也没得到。需要你的帮助,thx。
我调试服务,它在构造函数中是否中断,是从容器中解析的appService? –
@Jon_Lindeheim每次刷新页面时,调试点都没有触发,但抛出了像上面这样的异常。 – CharlieShi
我已经通过网络检查了exption,发现我的请求中可能有一些格式错误。但我详细检查了它,没有发现任何东西,所以有线。 – CharlieShi