WCF Rest涉及复杂类型的参数

Exc*_*ior 7 xml rest wcf

设置使用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);
}
Run Code Online (Sandbox Code Playgroud)

由于UriTemplate只允许字符串,最佳做法是什么?这个想法是客户端应用程序将向服务发布请求,例如一个人的搜索条件.该服务将使用包含XML数据的相应对象进行响应.

Bre*_*Bim 8

您可以使用休息发布复杂类型.

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

实现如下:

public bool DeleteCustomerOrder(CustomerSpecialOrder orderToDelete)
{
    // Do something to delete the order here.
}
Run Code Online (Sandbox Code Playgroud)

您可以从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);
}
Run Code Online (Sandbox Code Playgroud)

或者您也可以使用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;
}
Run Code Online (Sandbox Code Playgroud)