Make my WCF service return json

I am trying to return a WCF service method for a JSON object, but it does not work, when I open in a web browser, it shows xml.

How can I get this method to return JSON?

I inserted [WebGet (ResponseFormat = WebMessageFormat.Json)], but that didn't help

[WebGet(ResponseFormat = WebMessageFormat.Json)]  
protected override IEnumerable<KeyValuePair<string, SampleItem>> OnGetItems()  
{  
   // TODO: Change the sample implementation here  
   if (items.Count == 0)  
   {  
      items.Add("A", new SampleItem() { Value = "A" });  
      items.Add("B", new SampleItem() { Value = "B" });  
      items.Add("C", new SampleItem() { Value = "C" });  
   }  
   return this.items;  
}  
+3
source share
2 answers

For this to work, you need to place it with webHttpBindingand WebServiceHostFactoryin the web.config and file *.svc.

web.config , , . JSON WebGet WCF REST. WebGet SOAP, . basicHttpBinding, wsHttpBinding, netTcpBinding ..

WCF REST, WCF REST , WCF REST.

:, *.svc REST, WebGet JSON, factory:

<%@ServiceHost Language="C#" Service="YourService"
               Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

WebServiceHostFactory, , WCF REST, .

+4

WebHttpBehaviour? WebGet . . MSDN

, . , , WebGet, ( ).

0

Source: https://habr.com/ru/post/1764578/


All Articles