2013-05-03 53 views
0

我在WCF服務庫有問題,因爲我已經設置webMessegeFormatJSON格式, 而是它返回XML格式返回。我該如何解決這個問題?我錯過了什麼嗎?不以JSON格式,而是以XML格式

非常感謝你提前爲那些誰幫助:)

這裏是我的代碼:

public class Service : iService 
{ 
    [WebInvoke(Method="GET", 
     RequestFormat = WebMessageFormat.Json, 
     UriTemplate="{id}/{name}/{age}/{sex}/{address}")] 
    public Response Transaction(string id, string name, string age, string sex, string address) 
    { 
     return new Response() 
     { 
      ID = id, 
      Name = name, 
      Age = age, 
      Sex = sex, 
      Address = address 
     }; 
    } 
} 

public class Response 
{ 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public string Age { get; set; } 
    public string Sex { get; set; } 
    public string Address { get; set; } 
} 

這裏是我的應用程序配置

<?xml version="1.0"?> 
<configuration> 

    <system.serviceModel> 
    <services> 
     <service name="WcfEServiceLibrary.Service"> 
     <endpoint address="http://phws13:8732/WcfServiceLibrary/" 
        binding="webHttpBinding" 
        contract="WcfServiceLibrary.iService"> 
     </endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

回答

1

你需要設置ResponseFormatWebMessageFormat.Json

[WebInvoke(Method = "GET", 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "{id}/{name}/{age}/{sex}/{address}")] 
+0

我明白了,謝謝!它的作品,bodystyle是必要的? – SHINHAN 2013-05-03 01:23:37

+1

如果您希望使用某種工具更好地改善您的結果,但是這是可選的 – Turbot 2013-05-03 01:24:42