Rails和WCF之间的通信

时间:2010-01-13 14:10:24

标签: c# ruby-on-rails wcf rest

我目前正在使用一个使用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 Unexpected end of file)。

    [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){“意外的文件结尾。”} 堆栈跟踪:

   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)

3 个答案:

答案 0 :(得分:1)

Content-Length: 1
Content-Type: application/xml; charset=utf-8

不是有效的XML响应。

答案 1 :(得分:0)

我已经找到了它,感谢“Darin Dimitrov”和“Nate Bross”。当你将content-type设置为application / xml时,WCF期望XML是正确的。我已经将内容类型更改为text / plain(在rails中)并且它可以正常工作。

更改轨道格式中的以下内容:

format.xml { head :ok }

format.xml { head :ok, :content_type => 'text/plain' }

答案 2 :(得分:0)

下载REST Starter Kit Preview 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);