WCF Serialization using ISerializable Interface

75 Views Asked by At

This should be possible however I have searched extensively and have found no answers to this, using WCF ISerializable interface concept. Hopefully the experts can help.

Any documentation I have read only refers to info.AddValue(...) concept similar to the ones in https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.iserializable?view=netframework-4.8

In summary I have a SW which does operations like Image processing in a single EXE. I would like to use WCF Service/Client approach and do this processing on separate services on same PC or different PC's. Well basically distribute the process.

I have a WCF service with endpoints, connects & communicates to the services etc, all good here.

I want to send the entire object of a class(with its local variables/objects), which has been created on the client, across to the service.

I have the class derived from the ISerializable interface. Class attribute is [Serializable]. It has the required constructor & GetObjectData() public Foo(SerializationInfo info, StreamingContext context) public void GetObjectData(SerializationInfo info, StreamingContext context)

This object is Serialized using the BinaryFormatter to a byte[] and sent. On the service side it is Deserialized to the class object.

During Serialization it does go into GetObjectData() and does its task. BUT during deserialization the class object I get is just the default object not the object which was sent, local variables/objects are not initialized.

This class object is not a singleton, nor I want to that as there are many instances of this on separate threads. I would think one does not have to do a Addvalue on every parameter of a class. As this is just one class I referring to, eventually I have to send many such class objects.

I have tried using a helper class with IObjectReference interface concept using the GetRealObject() --> BUT how do I get the original object from this?

I have read about Surrogates, again no good exaple on how to send the entire object of a class.

I want the entire object of this class on the Service side.

Hope I am clear on my question. I am new to the WCF conecpts, I am probably missing some secret to make this work.

Please suggest any ideas!

Example Code, this is the basic version of what I want to do.

I need cls2 object which has Add() performed already thus num1 & num2 are updated, to be received on the service side with the right values of Add() like 100 & 200. ImgProcessingConnector() is handling all the stuff for connecting to the Service.

WCF Client

    public Form1()
    {
        InitializeComponent();
        cls2 = new Class2();            
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] cls2Byted = Class1.SerializedtoByte(cls2);

        ImgProcessingConnector.Instance().PerformAction(cls2Byted);
    }

    Class2 cls2 = null;

WCF Service

public class ImageProcessingWCFService : IImageProcessingWCFService
{
    public ImageProcessingWCFService()
    {

    }

    public bool PerformAction(byte[] cls2Byted)
    {
        Class2 cls2 = Class1.DeSerializedfromByte(cls2Byted) as Class2;

        cls2.Subtract();

        return true;
    }
}

Class2

[Serializable]
public class Class2 : ISerializable
{
    public Class2()
    {
        Add();
    }

    public Class2(SerializationInfo info, StreamingContext context)
    {

    }

    public void Add()
    {
        num1 = 50 + 50;
        num2 = 100 + 100;
    }

    public void Subtract()
    {
        num1 = 50 - 50;
        num2 = 100 - 100;
    }

    int num1;
    int num2;

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        //nothing to do send ALL.
    }
}
0

There are 0 best solutions below