views:

62

answers:

3

I want to invoke a wcf webservice using a url query string. Like if I have a webserive that expose a ADD 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.

+1  A: 

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

Florian
I am just writing a new wcf WS. I am learning wcf by trying different approaches.
vaibhav
James Manning
I dont know what a rest service is :(
vaibhav
A: 

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:

Endpoint.TV screencasts:

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

marc_s
A: 

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();
lucene user