I am building a WCF REST service and want to use Autofac as DI container. I want to be able to call a parameterized constructor of the service class. Below is my code:
[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;
}
Then in the global.asax, I set up the config by following 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)));
}
Below is the method I'm going to call:
[WebInvoke(Method = "GET"
, ResponseFormat = WebMessageFormat.Xml
, BodyStyle = WebMessageBodyStyle.Bare
, UriTemplate = "/Test/{test}")]
public string Test(string test)
{
return "HelloWorld!";
}
After I start up the project and browse to
http://localhost:1685/QDService/Test/1
The browser throw me an exception like :
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.
, I used the firebug to track it and found this:
I didn't know what caused this but after I removed the parameterized constructor, all worked fine for me . Then I had a quick search on the net but got nothing. Need your help, thx.