0

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.
    }
}
AshHegde
  • 11
  • 2
  • It is hard to tell what is wrong based on only the description of what you expect to happen. It is easier to follow along and point out where you went wrong if you include a [mcve]. Based on what you write I assume you didn't serialize all members or forgot to read stuff in GetObjectData but that is all guess work. Include code that shows what fails in your specific scenario. – rene Nov 15 '19 at 18:59
  • @Rene -- thanks for the response. I did not forget to read stuff in GetObjectData(), I dont want to add each parameters in it. I want the entire object of a class to be sent, regardless of the parameters in it. I would think I don't have add every parameters using AddValue(), then what does ISerializable really do. That I can do by sending an object over tcp/ip however I read that WCF is the efficient in doing so, which is what I am trying to test. I will create an example and post it. – AshHegde Nov 15 '19 at 19:07
  • Iserializable is a marker interface where it signals to the serializer and deserializer: *Hey, I handle all the stuff from here, don't bother looking at this type.* So you do have to add every parameter, as a matter of fact you also have to handle the (de)serialization of any members and their types and the types of those members. – rene Nov 15 '19 at 19:41
  • Updated with a example code. Thanks again! – AshHegde Nov 15 '19 at 20:34
  • Can't you make num1 and num2 public? Because then you can use the type Class2 directly in the request message without the need to fiddle with that byte array. – rene Nov 15 '19 at 20:39
  • The classes I want to send are much bigger and complex than 2 Int's, it has objects of other classes, dictionaries etc. point is how to get the Object of this class, it seems like it is in the SerializationInfo object BUT cannot find a way to get it out. – AshHegde Nov 15 '19 at 20:45

0 Answers0