开发者

How to invoke a WCF webservice using HTTP Get

I want to invoke a wcf webservice using a url query string. Like if I have a webserive that expose a AD开发者_如何学CD method of adding two integers. I want to invoke this service using http://mywebserviceAddress/Add?x=4&y=5

Is it possible to do this. I am new to webservices, this may be very easy for most of you.


See if below example helps you:

Service Contract

[ServiceContract] 
public interface IService1 
{ 
   [OperationContract] 
   [WebGet(UriTemplate="Add/{x}/{y}",ResponseFormat=WebMessageFormat.Xml)] 
   int Add(string x, string y); 
 }

Service implementation:

public class Service1 : IService1
    {
        public int Add(string x, string y)
        {
            return Convert.ToInt32(x) + Convert.ToInt32(y);
        }
    }

Web config:

<system.serviceModel>
        <services>
            <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
                <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="WcfService1.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfService1.Service1Behavior">
                    <serviceMetadata httpGetEnabled="false"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
        </behaviors>
    </system.serviceModel>

Client code:

WebRequest request = WebRequest.Create("http://localhost:2156/Service1.svc/Add/2/3");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine("Request to add numbers: ");
            Console.WriteLine("Request status: " + response.StatusDescription);
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine("Response: \n" + responseFromServer);
            Console.ReadLine();


If you want to call your WCF service with straight HTTP verbs, you need to create a WCF REST service, using the webHttpBinding.

Check out some resources for that:

  • An Introduction To RESTful Services With WCF

Endpoint.TV screencasts:

  • Building RESTful services with WCF (Part 1)
  • Building RESTful services with WCF (Part 2)
  • Calling RESTful services in WCF

Endpoint.TV in general has really good coverage for WCF and WCF REST stuff.


Is it a WS you want to build or a already existing one that you want to consume ?

If you want to build one, see REST web service.

Check the MSDN page for REST, you will find articles, videos, training, code samples, etc. http://msdn.microsoft.com/en-us/netframework/cc950529.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜