views:

36

answers:

3

I have an object, lets call it UnSerializableObject, that I can't and change that I need to serialize. So I created my own object called SerializableObject that is serializable and contains all of the same data as the original object. which looks something like this:

namespace test  
[Serializable]  
class SerializableObject  
{  
    private int item1;  
    public int Item1  
    {  
        get;  
        set;  
    }  
    public SerializableObject()  
    {}  
}  

Then I created another class that converts to and from a SerializableObject and an UnSerializableObject. on the service side everything works fine. The method in the service takes a SerializableObject as a parameter and I convert it to an UnSerializableObject. The problem is on the client side. I used svcutil to create the client code. Then I take an Object convert it to a SerializableObject and use the SerializableObject in the service call. Like this:

  TTSServiceClient aClient = new TTSServiceClient(); 
        UnSerializableObject loMMessage = new MostMessage();
        SerializableObject loSMMessage = ObjectConverter.ToSerializableObject(loMMessage);
        aClient.Allocate_OnStartResultAck(loSMMessage);

When I compile the client I get the error:

Error 59 Argument '1': cannot convert from 'test.Utilities.SerializableObject [c:\Projects\Client\Client\test.Utilities.dll]' to 'test.Utilities.SerializableObject [C:\Projects\Client\Client\Service.cs(19)]'

static Class ObjectConverter
{
   static public SerializableObject ToSerializableObject(UnSerializableObject usObject)
   {   
        SerializableObject sObject = new SerializableObject();
        sObject.Item1 = usObject.Item1;
        return sObject;
   }
}

why is this happening? and how do I fix it? Is there a better way to handle this?

A: 

Please make sure, that both your client and your server reference common project, that contains definition of your SerializableObject class. In addition, mark your class as below:

[Serializable]  
[DataContract()]
class SerializableObject  
{  
    [DataMember()]
    public int Item1  
    {  
        get;  
        set;  
    }  

    public SerializableObject()  
    {}  
}  
Boris Modylevsky
that didn't work. I get the data I want now, which helps. but I still got the same error.
scott
A: 

When you add service reference it by default creates new object with the same structure in the namespace of your service reference. If you want to use shared converter you have to share object as well and reuse it during service referencing.

Btw. WCF supports infrastructure for wrapping unserializable objects. Check IDataContractSuroggate.

Ladislav Mrnka
A: 

I had to rewrite my conversion method on the client side and remove the references to the test.utilities.dll. so essentially this

TTSServiceClient aClient = new TTSServiceClient(); 
UnSerializableObject loMMessage = new MostMessage();
SerializableObject loSMMessage = ObjectConverter.ToSerializableObject(loMMessage);
aClient.Allocate_OnStartResultAck(loSMMessage);

became

TTSServiceClient aClient = new TTSServiceClient(); 
UnSerializableObject loMMessage = new MostMessage();
SerializableObject loSMMessage = new SerializableObject();
loSMMessage.Item1 = loMMessage.Item1;
aClient.Allocate_OnStartResultAck(loSMMessage);

which is kind of annoying but it works.

scott