Error with WCF REST (POX) Client when 201 Created and No Response Content

328 views Asked by At

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.

1

There are 1 answers

0
Steven Pardo On BEST ANSWER

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.