创建 201 且无响应内容时 WCF REST (POX) 客户端出现错误

发布于 2024-12-10 05:08:37 字数 1392 浏览 0 评论 0原文

我正在根据固定规范使用通用 ClientBase 基类创建 WCF POX 代理。

该规范包含通过对端点的 POST 调用创建的项目。没有主体响应数据包,标头状态为 (201 Created) 以及标头集合中项目的 ID,键为 Location。

正在创建该项目,但我的 WCF 代理引发以下错误:

Unexpected end of file

我已开始创建自定义 IClientMessageFormatter 的路线,但:

IClientMessageFormatter.DeserializeReply(Message message, object[] parameters)

仍然被调用得太晚。我假设,为了在读取响应流之前拦截它,我必须更深入地连接到行为类中。

任何帮助将不胜感激。

修订1.

HTTP/1.1 201 Created
Server: nginx/1.0.5
Date: Thu, 13 Oct 2011 23:52:34 GMT
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-Throttle-Max: 500
X-Throttle-Count: 1
Location: https://sample.com/categories/1.xml
X-Throttle-Horizon: 2011-10-13T23:52:40Z
X-Runtime: 232
Set-Cookie: transition_token=BAhbB2kD8bSUSXU6CVRpbWUNwOUbwAkZIdIGOh9AbWFyc2hhbF93aXRoX3V0Y19jb2VyY2lvblQ%3D--de2dc7f909ce89e0ce9f2307ac43ddfc171442b7; path=/; secure
Set-Cookie: _sample_session=BAh7BzoQaWRlbnRpdHlfaWRpA2eiKjoPc2Vzc2lvbl9pZCIlNGVlNWMzNWY4MzdmZmM3MGNlNzE1ZjdjZmM5MDhmYmY%3D--d8927e076329846e844f9f73b4d587c697628481; path=/; HttpOnly; secure
Cache-Control: no-cache
Strict-Transport-Security: max-age=31536000

1

0

以上是回复。它没有主体,并且内容类型 = application/xml。由于某种原因,WCF 不知道如何协调这一点。因此,这就是为什么我试图在读取responseStream 之前拦截WCF 绑定或行为。

我想看看是否有人能给我指出正确的方向。谢谢。

I am creating a WCF POX proxy using the generic ClientBase base class against a fixed specification.

The specification has items created with a POST call to an endpoint. With no body response packet and header status of (201 Created) and the ID of the item in the header collection with the key of Location.

The item is being created but my WCF proxy is raising the following error:

Unexpected end of file

I have started down the route of creating a custom IClientMessageFormatter but the:

IClientMessageFormatter.DeserializeReply(Message message, object[] parameters)

is still being called to late. I am assuming, in order to intercept the responseStream before it is read I have to hook in deeper within the Behavior class.

Any help will be greatly appreciated.

Revision 1.

HTTP/1.1 201 Created
Server: nginx/1.0.5
Date: Thu, 13 Oct 2011 23:52:34 GMT
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-Throttle-Max: 500
X-Throttle-Count: 1
Location: https://sample.com/categories/1.xml
X-Throttle-Horizon: 2011-10-13T23:52:40Z
X-Runtime: 232
Set-Cookie: transition_token=BAhbB2kD8bSUSXU6CVRpbWUNwOUbwAkZIdIGOh9AbWFyc2hhbF93aXRoX3V0Y19jb2VyY2lvblQ%3D--de2dc7f909ce89e0ce9f2307ac43ddfc171442b7; path=/; secure
Set-Cookie: _sample_session=BAh7BzoQaWRlbnRpdHlfaWRpA2eiKjoPc2Vzc2lvbl9pZCIlNGVlNWMzNWY4MzdmZmM3MGNlNzE1ZjdjZmM5MDhmYmY%3D--d8927e076329846e844f9f73b4d587c697628481; path=/; HttpOnly; secure
Cache-Control: no-cache
Strict-Transport-Security: max-age=31536000

1

0

Above is the response. It has both no body and a content-type = application/xml. WCF doesn't know how to reconcile this for some reason. So, that is why I am trying to intercept the WCF Binding or Behavior before the responseStream is read.

I would like to see if someone can point me the right direction. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-12-17 05:08:37

想通了。我必须创建一个 WebContentTypeMapper 并将其分配给 WebMessageEncodingBindingElement.ContentTypeMapper。我遇到的唯一问题是 TypeMapper 不仅依赖于 contentType,还依赖于方法的 ReturnType。下面是一个示例:

internal class RestWebContentTypeMapper : WebContentTypeMapper
{
    public RestWebContentTypeMapper() : base() { }

    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        var type = this.ReturnType;

        if (type.FullName == "System.Void" || type.FullName == "RootName.Client.NewEntity")
            return WebContentFormat.Raw;
        else
            return WebContentFormat.Xml;
    }

    public Type ReturnType { get; set; }
}

因此,我必须传递 RestWebContentTypeMapper 实例以及 OperationDescription,直到它到达我的自定义 IClientMessageFormatter。将以下代码添加到 SerializeRequest 以传递 ReturnType。

public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
    this.Mapper.ReturnType = this.Description.SyncMethod.ReturnType;

    return Original.SerializeRequest(messageVersion, parameters);
}

以下工作代码很快就会发布到:http://dotnet37signals.codeplex.com

我知道现在有人做出了回应,但我希望这对其他人有帮助。

Figured it out. I had to create a WebContentTypeMapper and assign it to the WebMessageEncodingBindingElement.ContentTypeMapper. The only issue I had, was that the TypeMapper was not only dependent on the contentType but also the ReturnType of the method. Below is an example:

internal class RestWebContentTypeMapper : WebContentTypeMapper
{
    public RestWebContentTypeMapper() : base() { }

    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        var type = this.ReturnType;

        if (type.FullName == "System.Void" || type.FullName == "RootName.Client.NewEntity")
            return WebContentFormat.Raw;
        else
            return WebContentFormat.Xml;
    }

    public Type ReturnType { get; set; }
}

Therefore, I had to pass the RestWebContentTypeMapper instance along with the OperationDescription around until it reached my custom IClientMessageFormatter. The following code was added to the SerializeRequest in order to pass the ReturnType.

public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
    this.Mapper.ReturnType = this.Description.SyncMethod.ReturnType;

    return Original.SerializeRequest(messageVersion, parameters);
}

The following working code will soon be posted to: http://dotnet37signals.codeplex.com.

I know that now one responded but I hope this helps someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文