0

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: enter image description here

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.

Community
  • 1
  • 1
CharlieShi
  • 888
  • 3
  • 17
  • 43
  • I you debug the service, does it break in the constructor and is appService resolved from the container? – Jon Lindeheim May 20 '14 at 13:56
  • @Jon_Lindeheim Every time I refreshed the page, the debug point was not triggered but throw me the exception like above. – CharlieShi May 20 '14 at 14:05
  • I have checked the exption via net and found maybe there are some formats error on my request. But I detailed checked it and found nothing, so wired. – CharlieShi May 21 '14 at 00:13
  • @Jon_Lindeheim Now I can debug into. I can c that new instance can be created by ioc container. but why I can return string method, List method will return no data? – CharlieShi May 21 '14 at 01:26
  • @Jon_Lindeheim Hi, I have solved the problem!!!!! I will post a blog to track this in case that I will use it in the future. thx any way. – CharlieShi May 21 '14 at 02:05

1 Answers1

0

I have posted a blog here to track all the steps I encountered during solving this problem:

TinyFrame升级之十:WCF Rest Service注入IOC的心

it's written by chinese, I will translate it into english when I have time, thx.

CharlieShi
  • 888
  • 3
  • 17
  • 43