Hello! How to pass a custom object as a parameter to the WCF-Service OperationContract method? Code example:

public class MyClass { public int id; } [ServiceContract(Namespace = "http://C_M_Service")] public interface ICMInterface { [OperationContract(Action = "http://localhost:8123/C_M_Service/GetCars")] [WebGet(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)] [WebInvoke(Method="POST", BodyStyle=WebMessageBodyStyle.Wrapped)] string GetCars(MyClass data); } [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Car_market : ICMInterface { public string GetCars(MyClass data) { return("OK: id="+data.id.ToString()); } } 

Update

A message about the null object reference is displayed. Probably, it is necessary to somehow create an object, but I don’t understand how.

Yes, about creating through new - this is understandable. But where to use it? I am passing data from a JQuery.soap request to the OperationContract WCF service. When I use WCF-service in OperationContract as a parameter, say, int everything is fine. If you use a custom class, this question appears.

Tried to use DataContract:

 [DataContract] public class Cars_Data { [DataMember] public int id; } 

Result: "Object reference does not indicate an instance."

JQuery.soap code:

 $.soap({ url: "http://localhost:8123/C_M_Service/", method: "GetCars", SOAPAction: "http://localhost:8123/C_M_Service/GetCars", soap11:true, data: '<GetCars xmlns="http://C_M_Service"><data xmlns="http://C_M_Service"><id>123</id></data></GetCars>', error: function (soapresponse) { alert("Error: "+soapresponse.toString()); }, success: function (result) { alert("OK: "+result.toString()); } }); 

1 answer 1

The class to be passed must be marked as data contract using the [DataContract] / [DataMember] :

 [DataContract] public class MyClass { [DataMember] public int id; } 

It is also worth checking the names and namespaces of the transmitted elements. as a sample, you can use a request from the WCF Test Client:

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:8123/C_M_Service/GetCars</Action> </s:Header> <s:Body> <GetCars xmlns="http://C_M_Service"> <data xmlns:d4p1="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <d4p1:id>1</d4p1:id> </data> </GetCars> </s:Body> </s:Envelope> 

The namespace for the id must match the targetNamespace of wsdl :

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/WcfService1" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/WcfService1"> <xs:complexType name="MyClass"> <xs:sequence> <xs:element minOccurs="0" name="id" type="xs:int"/> </xs:sequence> </xs:complexType> <xs:element name="MyClass" nillable="true" type="tns:MyClass"/> </xs:schema> 
  • Already tried. I receive "The link to object does not indicate an instance". The class added in the question. - AN90
  • Write down the actual request from the client (fiddler, for example) and compare it with the example shown on the sample page for your service. Most likely you didn’t guess somewhere with the name of the element or with the namespace, and data == null comes to the server - PashaPash
  • When the parameter type in OperationContract was int - the data was transferred. - AN90
  • @ AN90 so maybe you didn't guess with the element name for MyClass? Check request format. - PashaPash
  • I didn't have the parameter names (there was another name in the SOAP message). Changed. Now the "Formatter generated an exception when trying to deserialize the message is issued: Error deserializing the C_M_Service parameter : data . The InnerException message was" Error in line 1, position 264. Expected status "Element" .. Detected "Text" with name "", namespace " . "For details, see. InnerException" - AN90