tests wcf service in the browser

点点圈 提交于 2019-12-20 17:26:42

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4885111/tests-wcf-service-in-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!