REST service works fine in Microsoft WCF Test Client but fails in browser

Had a  WCF Rest service.  On svc file, when you hit F5, Microsoft WCF Test Client opens up and you can invoke all the methods and everything works fine. However, when I open the same service in browser, there is no response. And the problem was default web.config does not have the following lines under system.serviceModel

<services>
  <service name="CRService.Amazon";  behaviorConfiguration="ServiceBehavior";>
IAmazon" behaviorConfiguration="web" />
  <service>
<services>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
<webHttp/>
   </behavior>
  </endpointBehaviors>
</behaviors>

just so you can relate the above entries, here is my interface definition and source code.

namespace CRService
 {
[ServiceContract]
public interface IAmazon
     {
         [OperationContract]
         [WebInvoke (Method= "GET",
         ResponseFormat= WebMessageFormat.Json,
         UriTemplate="json/{id}")]
         IEnumerable GetData(string id);
     }

public class Amazon : IAmazon
     {
      public IEnumerable  GetData(string key)
         {
             AmazonConnector.Amazon a = new AmazonConnector.Amazon();
             return  a.Search(key);
         }
     }
}

and now my http://localhost:61527/Amazon.svc/json/123 URI returns the nice JSON.

I must add, if you are writing a new web service, you should be using WebAPI.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s