Search code examples
c#wcfweb-servicestcpnettcpbinding

How to return CookieContainer via WCF?


I've got a class, which has a cookie container as its member (Marked by [DataMember]). When I try and return this class after TCP web service call, I'm getting following exception:

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be 
used for communication because it is in the Faulted state.

When I remove the [DataMember] attribute, the service call works just fine (of course, no cookie container is returned).

I need for the service call to return this object, and CookieContainer can be easily serialized via binary serializer.

So, here's my question: how do I make the service return CookieContainer without throwing up the exception?

I thought of doing a binary serialization on the CookieContainer, after which it can be converted to string via Convert.ToBase64String, but there has to be a better way of doing this.


Solution

  • You don't need to pass the CookieContainer per se - what you need to pass, I would argue, is the data that the cookie container contains. In that case why not pull the data out of the cookie container - pass that across and recreate the cookie container on the receiver side

    The types you use on service boundaries are there to aid data serialization - nothing more than that. It is not the intent to be able to pass arbitrary .NET types across service operations. WCF is a message based programming model - we only pass data, not behavior or class