设置使用webHttpBinding的WCF服务...我可以从XML方法返回复杂类型。如何将复杂类型作为参数?
[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);
}
由于UriTemplate只允许字符串,最佳做法是什么?这个想法是客户端应用程序将向服务发布请求,例如一个人的搜索条件。该服务将使用包含数据的相应对象作为XML进行响应。
答案 0 :(得分:8)
您可以使用休息发布复杂类型。
[ServiceContract]
public interface ICustomerSpecialOrderService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "deletecso/")]
bool DeleteCustomerOrder(CustomerSpecialOrder orderToDelete);
}
实现如下:
public bool DeleteCustomerOrder(CustomerSpecialOrder orderToDelete)
{
// Do something to delete the order here.
}
您可以从WPF客户端调用方法:
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);
}
或者您也可以使用HttpWebRequest调用它,将复杂类型写入我们从移动客户端执行的字节数组。
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;
}
答案 1 :(得分:0)
您的选择:
我推荐前者,感觉更加RESTful而且不那么hacky。 POST将提交一个查询,作为回应,您将获得一个queryId,与您提交的内容有关。
根据REST的想法,您可以获取该ID以获取查询结果。
答案 2 :(得分:0)
您可以在REST服务调用中传递JSON字符串或XML格式数据作为输入主体,并在服务协定定义中提及相同内容。然后它将允许您在REST服务调用中传递对象作为输入。