WCF Break Options Including Complex Types

Setting up a WCF service using webHttpBinding ... I can return complex types from a method in xml format ok. How to take a complex type as a parameter?

[ServiceContract(Name = "TestService", Namespace = "http://www.test.com/2009/11")] public interface ITestService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/Person/{customerAccountNumber}, {userName}, {password}, {PersonCriteria}")] Person SubmitPersonCriteria(string customerAccountNumber, string userName, string password, PersonCriteria details); } 

Since UriTemplate only allows strings, what's the best practice? The idea is that the client application will publish a request to the service, for example, search criteria for a person. The service will respond with an appropriate object containing data as XML.

+7
rest xml wcf
source share
3 answers

You can send complex types through leisure.

 [ServiceContract] public interface ICustomerSpecialOrderService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "deletecso/")] bool DeleteCustomerOrder(CustomerSpecialOrder orderToDelete); } 

The implementation is as follows:

 public bool DeleteCustomerOrder(CustomerSpecialOrder orderToDelete) { // Do something to delete the order here. } 

You can call the method from the WPF client:

 public void DeleteMyOrder(CustomerSpecialOrder toDelete) { Uri address = new Uri(your_uri_here); var factory = new WebChannelFactory<ICustomerSpecialOrderService>(address); var webHttpBinding = factory.Endpoint.Binding as WebHttpBinding; ICustomerSpecialOrderService service = factory.CreateChannel(); service.DeleteCustomerOrder(toDelete); } 

Or you can also call it using HttpWebRequest by writing a complex type to a byte array that we do from a mobile client.

 private HttpWebRequest DoInvokeRequest<T>(string uri, string method, T requestBody) { string destinationUrl = _baseUrl + uri; var invokeRequest = WebRequest.Create(destinationUrl) as HttpWebRequest; if (invokeRequest == null) return null; // method = "POST" for complex types invokeRequest.Method = method; invokeRequest.ContentType = "text/xml"; byte[] requestBodyBytes = ToByteArray(requestBody); invokeRequest.ContentLength = requestBodyBytes.Length; using (Stream postStream = invokeRequest.GetRequestStream()) postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length); invokeRequest.Timeout = 60000; return invokeRequest; } 
+8
source share

Your options:

  • use POST and accept an XML document of arbitrary complexity, or
  • use GET and specify how to map the URL path to your query criteria.

I would recommend the first, it feels more RESTful and less hacks. POST should have sent the request, and in return you will get queryId, something related to what you sent.

According to REST ideas, you can get this identifier to get the results of the request.

0
source share

You can pass JSON or XML data as an input body to a REST service call and specify the same in the definition of a service contract. Then it will allow you to pass the object as an input to the REST service call.

0
source share

All Articles