[英]Communication between Rails and WCF
我目前正在研究使用Rails REST服務的WCF應用程序。 問題是當我執行更新或刪除請求時,Rails不返回XML,僅返回以下標頭:
HTTP/1.1 200 OK
Date: Wed, 13 Jan 2010 13:56:25 GMT
Server: Apache/2.2.14 (Debian)
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.7
X-Runtime: 20
Cache-Control: private, max-age=0, must-revalidate
Set-Cookie: _Shop-R+Server_session=BAh7BzoPc2Vzc2lvbl9pZCIlODY0NmZlZjQyZTg1OTcyNTE0ZTRlN2NkNTcyZDVmYTEiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7BjoLbm90aWNlIidDdXN0b21lciB3YXMgc3VjY2Vzc2Z1bGx5IHVwZGF0ZWQuBjoKQHVzZWR7BjsHRg%3D%3D--88d0f739a64ea3a92e3a034d73365393dcfeee1e; path=/; HttpOnly
Content-Length: 1
Status: 200
Content-Type: application/xml; charset=utf-8
據我所知,這是可以預期和正確的。 但是,從WCF調用以下服務請求時,我們會收到ProtocolException(InnerException:XMLException文件的意外結尾)。
[ServiceContract]
[XmlSerializerFormat]
public interface ICustomerService
{
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "customers/{id}.xml")]
Customer GetCustomer(string id);
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "customers/{id}.xml",
Method = "PUT")]
void UpdateCustomer(string id, Customer newCustomer);
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "customers.xml",
Method = "POST")]
Customer CreateCustomer(Customer newCustomer);
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "customers/{id}.xml",
Method = "DELETE")]
void DeleteCustomer(string id);
}
GetCustomer和CreateCustomer方法可以正常工作,UpdateCustomer和DestroyCustomer引發異常。 我們懷疑這是因為期望XML作為響應。
是否有人對Rails和WCF有任何經驗,並且知道此問題的解決方案/解決方法?
為了完整起見,以下是異常詳細信息:
ProtocolException {“從網絡接收到的XML出現問題。有關更多詳細信息,請參閱內部異常。”}
XMLException(InnerException){“文件結尾意外。”} StackTrace:
at System.Xml.EncodingStreamWrapper.ProcessBuffer(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding)
at System.Xml.XmlUTF8TextReader.SetInput(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
at System.Xml.XmlDictionaryReader.CreateTextReader(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.UTF8BufferedMessageData.TakeXmlReader()
at System.ServiceModel.Channels.BufferedMessageData.GetMessageReader()
at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders)
at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType)
at System.ServiceModel.Channels.WebMessageEncoderFactory.WebMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType)
at System.ServiceModel.Channels.HttpInput.DecodeBufferedMessage(ArraySegment`1 buffer, Stream inputStream)
Content-Length: 1
Content-Type: application/xml; charset=utf-8
不是有效的XML響應。
感謝“ Darin Dimitrov”和“ Nate Bross”,我已經找到了它。 當您將內容類型設置為application / xml時,WCF期望XML是正確的。 我已經將內容類型更改為text / plain(在rails中),並且可以正常工作。
在rails格式中更改以下內容:
format.xml { head :ok }
至
format.xml { head :ok, :content_type => 'text/plain' }
去下載REST Starter Kit預覽版2 ,看看Microsoft.Http命名空間。 使用此庫執行到Rails界面的POST就像這樣簡單:
var client = new HttpClient("http://railsinterface.com");
var content = HttpContent.CreateXmlSerializable<Customer>(customer);
var response = client.Post("customers.xml",content);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.