gpt4 book ai didi

xml - 涉及复杂类型的 WCF Rest 参数

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:37 24 4
gpt4 key购买 nike

正在设置一个使用 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 数据的适当对象进行响应。

最佳答案

您可以使用 rest 发布复杂类型。

[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;
}

关于xml - 涉及复杂类型的 WCF Rest 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1709781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com