Ruhul Amin

Ruhul Amin

  • NA
  • 4
  • 1k

The data contract type “…” cannot be deserialized because th

May 5 2018 2:33 PM
favorite

In my Application, I have a WCF service. I am trying to send a Json Object and get the Json object as Response.

But every time, I am getting error "The data contract type 'TestConsole.localhostrREF4.PrimeDataProperty' cannot be deserialized because the required data members 'isPrimeResultField, isPrimeResultFieldSpecified, isPrimeResultSpecified1Field, isPrimeResultSpecified1FieldSpecified, numberField, numberFieldSpecified, numberSpecifiedInField, numberSpecifiedInFieldSpecified' were not found.

My Web Service name is "ISAJsonWebService"

Here is my code of Web Service:

IJsonWebService.cs

[ServiceContract] public interface IJsonWebService {

 [OperationContract] [WebInvoke(UriTemplate = "/GetData", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] PrimeDataPropertyOut GetData(PrimeDataProperty value);  }  [DataContract] public class PrimeDataProperty { [DataMember] public int number { get; set; } [DataMember] public bool NumberSpecifiedIn { get; set; } [DataMember] public bool IsPrimeResult { get; set; } [DataMember] public bool IsPrimeResultSpecified { get; set; } }  [DataContract] public class PrimeDataPropertyOut { [DataMember] public bool IsPrimeResultOut { get; set; } [DataMember] public bool IsPrimeResultSpecifiedOut { get; set; } }

EndPoint Code behind JsonWebService.svc.cs

public class JsonWebService : IJsonWebService { public PrimeDataPropertyOut GetData(PrimeDataProperty value) { return new PrimeDataPropertyOut { IsPrimeResultOut = true, IsPrimeResultSpecifiedOut = true }; }  }

Here is my Web Config:

<?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.7.1" /> <httpRuntime targetFramework="4.7.1"/> </system.web>  <system.serviceModel> <diagnostics> <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" /> </diagnostics> <services> <service behaviorConfiguration="serviceBehavior" name="ISAJsonWebService.JsonWebService"> <endpoint address="" behaviorConfiguration="jsonEndpointBehavior" binding="webHttpBinding" name="json" contract="ISAJsonWebService.IJsonWebService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" /> <endpoint address="wsHttpBinding" behaviorConfiguration="wsHttpBinding" binding="wsHttpBinding" bindingConfiguration="" name="wsHttpBinding" contract="ISAJsonWebService.IJsonWebService"> <identity> <dns value="localhost/wsHttpBinding" /> </identity> </endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="jsonEndpointBehavior"> <webHttp /> </behavior> <behavior name="wsHttpBinding" /> </endpointBehaviors> <serviceBehaviors> <behavior name="serviceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <directoryBrowse enabled="true"/> </system.webServer>  </configuration>

I am trying to Consume the web service from a Console App.

Here is the code:

 static void Main(string[] args) {         localhostrREF4.JsonWebService jsonWebServiceRef = new          localhostrREF4.JsonWebService(); PrimeDataProperty pdp = new PrimeDataProperty { number = 1, NumberSpecifiedIn = false, IsPrimeResult = false, IsPrimeResultSpecified = false }; WebClient client = new WebClient();         client.Headers["Content-type"] = "application/json"; MemoryStream ms = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PrimeDataProperty));         serializer.WriteObject(ms, pdp); byte[] data =      client.UploadData("http://localhost:65256/JsonWebService.svc/GetData", "POST", ms.ToArray()); Stream stream = new MemoryStream(data); var obj = new DataContractJsonSerializer(typeof(PrimeDataProperty)); **var returnPrimeDataProperty = obj.ReadObject(stream) as PrimeDataProperty;** }

in the last line (Bold Text) is throwing exception.

Please let me know, what I am missing.

Thanks.