Zakaria
Zakaria

Reputation: 15070

tests wcf service in the browser

I can't invoke a basic wcf web method in the browser even with <ServiceMetadata httpGetEnabled="True"/> in the config file.

For the source, code, it's very basic:

For the interface:

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetData();

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: ajoutez vos opérations de service ici
    }

And for the implementation:

 public string GetData()
        {
            return ("{'code':'yes'}");
        }

This method works fine in the built-in visual studio wcf service tester and returns {'code':'yes'}.

In the browser, when I call the http://localhost:54421/Service1.svc/GetData, it displays a blank page. How can I resolve this?

Upvotes: 3

Views: 20011

Answers (2)

Stever B
Stever B

Reputation: 4117

Most browsers will not display json results in browser. Generally, you will see a blank page (try viewing the source) or you will get prompted for a download.

If you are using Firefox there are some add-ons to view JSON and the Poster add-on for testing web services.

If you are using Google Chrome you can try Pretty JSON

Upvotes: 1

bartosz.lipinski
bartosz.lipinski

Reputation: 2677

I am doing that by creating additional endpoint behavior for REST calls so I can have different clients. Take a look at this configuration:

  <endpointBehaviors>
    <behavior name="RESTFriendly">
      <webHttp />
    </behavior>
  </endpointBehaviors>

in your service definition add endpoint which is using this behavior

<endpoint address="/easy" behaviorConfiguration="RESTFriendly" ...

now you can call your service both from browser and from wcf client. To call it from browser:

http://localhost:54421/Service1.svc/easy/GetData

ServiceMetadata is for different purpose here is link to documentation. Basically it means your service will expose information about itself so external developers can create proxy clients.

Upvotes: 1

Related Questions