WCF and HTTP GET

2019-06-26 13:22发布

问题:

My WCF service exposes this function

public SerialNumberInfo GetSerialNumberInfo(string serialNumber) { }

Is there a way to enable HTTP GET on my WCF service? Example:

http://localhost:8004/MyService/GetSerialNumberInfo?serialNumber=4

回答1:

yes, you need to use the webHttpBinding on your WCF service.

See the WCF REST Starter Kit for more information on REST Support in WCF.

  • REST in WCF
  • Overview of REST in WCF
  • Series of screencasts on how to use REST in WCF

If you're hosting your service in IIS, you need to create a separate *.svc file for the REST service (let's call it RESTService.svc), which contains:

<%@ ServiceHost Service="YourServiceName" Language="C#" debug="False"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Now you should be able to connect to the URL

http://localhost:8004/MyService/RESTService.svc

and get your data RESTfully.

Marc



回答2:

What you want to do sounds like building a RESTful service with WCF. Check out the following article on MSDN, it has all the details:

A Guide to Designing and Building RESTful Web Services with WCF 3.5



标签: wcf http