在这个主题上阅读了几个不同的SO Posts和Docs后,我仍然感到困惑,无法通过我的服务端点上的RequestBindingException。例如,我想使用POSTMAN将以下xml发布到服务。
<LeadApplications>
<LeadApplication>
<Email>daffy.duck@example.com</Email>
<FirstName>Joey</FirstName>
<MiddleName>Disney</MiddleName>
<LastName>Duck</LastName>
<Street1>1 Disneyland Street</Street1>
<Street2>2 Disneyland Street</Street2>
<City>PAUMA VALLEY</City>
<State>CA</State>
<Zip>92503</Zip>
</LeadApplication>
</LeadInformations>
[Restrict(RequestAttributes.Xml)]
public class LeadData : IRequiresRequestStream
{
public Stream RequestStream { get; set; }
}
public object Post(ServiceModel.Types.DirectApi.Legacy.LeadData request)
{
return null;
}
Routes.Add<ServiceModel.Types.DirectApi.Legacy.LeadData>("/Leads/LeadData/", "POST", "LMS - DirectApi")
我希望这是克服.NET反序列化属性排序问题的最佳方法。
原始请求/响应
POST http://localhost/LO.Leads.Receiver/api/Leads/LeadData/ HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 381
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2289.0 Safari/537.36
Content-Type: application/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: ss-pid=fhjNz7BGBBqgps6zsnUo; ss-opt=perm; X-UAId=f94092eb-fae8-4b39-b5ff-ae3b404645d8; m=34e2:|41b8:f|4a01:t|ca3:t|b12:f|47ba:t|77cb:t
<LeadApplications>
<LeadApplication>
<Email>daffy.duck@example.com</Email>
<FirstName>Joey</FirstName>
<MiddleName>Disney</MiddleName>
<LastName>Duck</LastName>
<Street1>1 Disneyland Street</Street1>
<Street2>2 Disneyland Street</Street2>
<City>PAUMA VALLEY</City>
<State>CA</State>
<Zip>92503</Zip>
</LeadApplication>
</LeadApplications>
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/xml
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-MiniProfiler-Ids: ["6f4255ee84fa45d1a2e05de8a268fc37","a52523648aed4dfaae96e607d07b5163","f9cf295e8e7b4aa1b0579929e61d59a5","7b4daa97fc8d427cb952b17414c4da31","707e7624fa4546c3911a9cb3ce5e6a36"]
X-Powered-By: ASP.NET
Date: Wed, 28 Jan 2015 02:19:30 GMT
Content-Length: 538
<?xml version="1.0" encoding="utf-8"?><ErrorResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.servicestack.net/types"><ResponseStatus><ErrorCode>RequestBindingException</ErrorCode><Message>Unable to bind request</Message><StackTrace> at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath)
at ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName)</StackTrace><Errors i:nil="true" /></ResponseStatus></ErrorResponse>
谢谢你, 斯蒂芬
答案 0 :(得分:5)
由于Request Content-Type与现有的已注册的Serializer(即XML)匹配,因此它仍然会尝试使用该Serializer反序列化正文。现在修复此问题,忽略IRequiresRequestStream
次请求from this commit的所有序列化程序。
现在,您可以使用以下命令创建对流的访问权限并将其读取为普通XML:
[Restrict(RequestAttributes.Xml)]
[Route("/Leads/LeadData/", "POST", Notes = "LMS - DirectApi")]
public class CustomXml : IRequiresRequestStream
{
public Stream RequestStream { get; set; }
}
public class RawRequestService : IService
{
public object Any(CustomXml request)
{
var xml = request.RequestStream.ReadFully().FromUtf8Bytes();
return xml;
}
}
您可以使用HTTP Utils拨打电话,例如:
var xml = @"<LeadApplications>
<LeadApplication>
<Email>daffy.duck@example.com</Email>
<FirstName>Joey</FirstName>
<MiddleName>Disney</MiddleName>
<LastName>Duck</LastName>
<Street1>1 Disneyland Street</Street1>
<Street2>2 Disneyland Street</Street2>
<City>PAUMA VALLEY</City>
<State>CA</State>
<Zip>92503</Zip>
</LeadApplication>
</LeadApplications>";
var requestUrl = Config.ServiceStackBaseUri + "/Leads/LeadData/";
var responseXml = requestUrl.PostXmlToUrl(xml);
Assert.That(responseXml, Is.EqualTo(xml));
此更改可从 v4.0.37 + 获得,现在为available on MyGet。如果您更改了使用不同内容类型的请求,例如,它也会起作用。 application/xml-lead
与任何现有的序列化程序都不匹配,但是您需要删除仅允许XML请求的[Restrict]
。