0

I am newbie with web services. I am trying to create a WCF Restful web service . I have implemented a simple service that accepts parameter name and when the client requests the service it prints hello with name.

I have created a web client and added the reference of the web service to the client. As per my research, as soon as I add the service reference it should update client's web.config according to the service's web config which is not happening. The issue is that .config file is not being created with end points after adding the service reference and even after running svcutil.exe..Either way its not generating the required .config file..

Relevant code:

[ServiceContract]
    public interface IRestFulTest
    {
        [OperationContract]
    [WebInvoke(Method= "GET",ResponseFormat=WebMessageFormat.Xml, UriTemplate="xml/{Name}")]
        String sayhello(String name);
    }

service web.config

 <system.serviceModel>
    <services>
      <service name="ServiceDemo.RestFulTest">
        <endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding"
          bindingConfiguration="" contract="ServiceDemo.IRestFulTest" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

what probably could be wrong? Any ideas or suggestions would be really appreciated.

G droid
  • 924
  • 4
  • 12
  • 32
  • I think you are missing the point of RESTful web service, the whole idea is not to deal with these service references, but just have your client invoke the REST service with the methods you had exposed. – Y.S Jun 18 '15 at 11:30
  • http://stackoverflow.com/questions/671118/what-exactly-is-restful-programming?rq=1 – Y.S Jun 18 '15 at 11:31
  • @Y.S:How can I achieve that..any suggestions?? – G droid Jun 18 '15 at 11:32
  • There are many tutorials on creating restful services. E.g. http://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services Also I would advise to have a look on .NET web api for restful architecture which is more up do date and is better over wcf to my opinion. – Y.S Jun 18 '15 at 11:35
  • @Y.S: I have created the service..Issue is I am not able to create the client of the same.. – G droid Jun 18 '15 at 11:37
  • I don't get it.. you have a method which you defined as HTTP GET and you defined a routing for it. If all your service configurations are in place, then entering a URL in a web browser should invoke the method. I.e. http://yourService/YourRndPoint.svc/xml/yourName. If this works, and you get a response, then all your client needs to do is just create a HTTP request and send it to the same URL, no references or anything else. Makes sense? – Y.S Jun 18 '15 at 11:42
  • @Y.S :yes I am able to invoke the method in the url but how do i create a HTTP request and send it to the same URL..for that I am creating a console app and then in the references I am adding web service reference(URL) then I am initiating the instance of the proxy class where i am able to find the exposed method and call it.. – G droid Jun 18 '15 at 12:04
  • @@Y.S : This error occurs when I run the console app: – G droid Jun 18 '15 at 12:05
  • Additional information: Could not find default endpoint element that references contract 'ServiceReference1.IRestFulTest' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. – G droid Jun 18 '15 at 12:05
  • Again, you are not supposed to add configurations to your client. You are not adding it to your browser either, but the call gets through right? – Y.S Jun 21 '15 at 03:36
  • all you need is a simple get request, if you are using c#, then something like this: HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://host/endpoint.svc"); HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); – Y.S Jun 21 '15 at 03:38

2 Answers2

0

The reason that no client endpoint configuration is generated in the config file is that generating client code from a RESTful WCF service is not supported.

There is no metadata standard for non-SOAP HTTP services. As a caller of the service you just have to call it directly.

You can consume a RESTful endpoint in .net in many ways, including:

  1. Use the WebChannelFactory
  2. Use the good old HttpClient
  3. Use WebClient
tom redfern
  • 28,053
  • 12
  • 86
  • 116
0

I found the answer myself. In my service I had not declared an endpoint for met data exchange due to which when I was trying to add the reference of the service to the client the required app.config was not being generated.

Adding the end point for metadata exchange solved the problem.

 **<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>**

I hope it would save some one else's lot of time and head banging.

G droid
  • 924
  • 4
  • 12
  • 32