[英]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.