locked
Passing a class object inside of a DataContract RRS feed

  • Question

  • I created a WCF Service with multiple OperationContracts that work fine. Every operation contract returns a DataContract called Status which is structured like so:

            [DataContract]
    	public class Status
    	{
    		public enum Error { 
    			NoError, 
    			AuthTokenExpired, 
    			InvalidAuthToken, 
    			InvalidUsername, 
    			InvalidPassword, 
    			UsernameAlreadyRegistered,  
    			DatabaseInsertFail, 
    			DatabaseUpdateFail, 
    			PasswordResetRequired,
    			UnauthorizedAccess,
    			DatabaseRequestFail,
    			EmailServiceError,
    			DatabaseDeleteFail,
    			InvalidEmail,
    			GeneralNetworkException,
    			NotCurrentVersion
    		};
    		[DataMember] public Error status_code;
    		[DataMember] public object content;
    
                    public Status(Error status_code, object content)
                    {
                        this.status_code = status_code;
                        this.content = content;
                    }
            }

    As you can see one of the fields is just a generic object so that I can pass back any number of things and still use the Status class. This works beautifully when I want to pass back basic data types like string, ints, floats, etc but fails miserably when I want to pass back another class. If I use the same class as the return type for the OperationContract by itself the call works fine , but somehow having it nested inside of my DataContract causes issues.

    The error I get on the client side is this:

    An error occurred while receiving the HTTP response to http://[IP Sanitized for post]/DataService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. 

    I originally thought it might have to do with the size of the request so I made another fake class with a boolean in it and had the same issue. Then I tried adjusting the buffer size in the config files and that still didn't work. Process of elimination would suggest there is something odd going on just by sending a class inside of an object field in a DataMember. There are similar threads but none of them have quite clear answers so does anyone have any experience with this or have a solution other than "just set the return type of that function to blah" which I will inevitably do as a last resort.


    Thursday, May 3, 2012 12:00 AM

All replies

  • Worked at this some last night and it looks like the DataContract serializer just doesn't like serializing DataContract classes into generic objects. Gave up and eventually just started passing XML and parsing it client side which I probably should have just done to begin with. If anyone knows a way around the issue feel free to pitch in otherwise my advice to anyone else attempting to do something similar is to just pass a parsable string (XML, JSON, etc).

    • Proposed as answer by vkbishnoi Thursday, May 10, 2012 8:47 AM
    Thursday, May 3, 2012 9:13 AM
  • Hi,

    The problem maybe due to the object type is not supported by the Data Contract Serializer. You can refer to the below link about the types supported by the Data Contract Serializer.

    http://msdn.microsoft.com/en-us/library/ms731923.aspx

    Tuesday, May 8, 2012 2:14 PM
  •  
    You just can't pass "Object" as a type. It has to be a known-type object.
     
    Tuesday, May 8, 2012 3:01 PM
  • Not the problem, I upped all of the maximum message size constraints and it still had the issue unfortunately.
    Tuesday, May 8, 2012 11:35 PM
  • Not true. Like I said in my post I used the generic object in this class to serialize and successfully pass back every basic data type back to the client. The issue is when you have are trying to pass a complex object that isn't part of the .NET framework.
    Tuesday, May 8, 2012 11:38 PM