如何在wcf服务中获取json响应 [英] How to get json response in wcf service
问题描述
我需要在wcf服务中获得Json响应。 Xml请求和响应正常工作。但我需要更改相同的xml请求和对Json的响应。
请帮助我得到Json的回复
谢谢
我尝试过:
Hi,
I need to get Json response in wcf service. Xml Request and Response working properly. but i need to change same xml request and response to Json.
Please help me to get the Json response
Thanks
What I have tried:
[ServiceContract]
public interface IManageService
{
[OperationContract]
[WebInvoke(UriTemplate = "Get", BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
GetManageResponse Get(GetManageRequest GetManageRequest);
}
}
GetManageRequest class have passing parameters
GetManageResponse class have the Return List
GetManageResponse :
GetManageResponse:
using System;
using System.Runtime.Serialization;
namespace DomainModel
{
[DataContract]
public class GetManageResponse
{
[DataMember]
public List<Manage> ManageList { get; set; }
}
}
Web.config:
Web.config:
<services>
<service behaviorConfiguration="debug" name="CloudServices.ManageService">
<endpoint address="json" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="CloudServices.IManageService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:2502/CloudServices/ManageService" />
</baseAddresses>
</host>
</service>
</services>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
请求:
http:// localhost:2502 / DriverService.svc / json /获取?APIKEY = cbe55a68-0987-4cd4-81e9-73ae18888b9c
{城市:Vijayawada,GetAction:Driversavailabledays}
回复:
应该在Json
Request :
http://localhost:2502/DriverService.svc/json/Get?APIKEY=cbe55a68-0987-4cd4-81e9-73ae18888b9c
{"City":"Vijayawada","GetAction":"Driversavailabledays"}
Response :
Should be in Json
推荐答案
关闭自动格式选择。
Turn off automatic format selection.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
最后我得到了Json的回应。问题在于日期时间。现在它已经解决了
问题:
Finally i got Json Response. The problem is with date time. Now it was solved
Issue:
DateTime.MinValue not be serialized in timezones ahead of UTC
解决方案:
Solution :
[DataMember(IsRequired = false, EmitDefaultValue = false)]
在任何DateTime会员上添加此内容。
Added this on any DateTime Member.
这篇关于如何在wcf服务中获取json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!