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()); } });
new? Show the code. - VladDGetCars? - VladD