Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
[ServiceContract]
    public interface IManageService
    {

        [OperationContract]
        [WebInvoke(UriTemplate = "Get", BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        GetManageResponse Get(GetManageRequest GetManageRequest);
    }
}


C#
GetManageRequest class have passing parameters
GetManageResponse class have the Return List

GetManageResponse:
C#
using System;
using System.Runtime.Serialization;

namespace DomainModel
{
    [DataContract]
    public class GetManageResponse
    {
        [DataMember]
        public List<Manage> ManageList { get; set; }
    }
}


Web.config:
C#
<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>


Request :
http://localhost:2502/DriverService.svc/json/Get?APIKEY=cbe55a68-0987-4cd4-81e9-73ae18888b9c
{"City":"Vijayawada","GetAction":"Driversavailabledays"}
Response :
Should be in Json
Posted

Turn off automatic format selection.

XML
<system.serviceModel>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
       <standardEndpoints>
           <webHttpEndpoint>
               <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
           </webHttpEndpoint>
       </standardEndpoints>
</system.serviceModel>
 
Share this answer
 
Comments
.net333 9-Feb-16 6:24am    
Hi John. I changed web.config Service model settings same as above. but getting results in xml only
John C Rayan 9-Feb-16 6:45am    
Can you post your XML response here. Are you getting XML response instead of Json?
Finally i got Json Response. The problem is with date time. Now it was solved
Issue:
C#
DateTime.MinValue not be serialized in timezones ahead of UTC

Solution :
C#
[DataMember(IsRequired = false, EmitDefaultValue = false)]

Added this on any DateTime Member.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900