Is it possible to access a property from a remote object like in .Net Remoting?

103 Views Asked by At

i'm using .Net Remoting to communicate with Server-Activated-Objects. Now its time to migrate to another communication technology because Remoting is deprecated. Here is an example from my classes to explain my problem:

    public class Class1 : MarshalByRefObject
    {
        public Class1()
        {
            MyProp = new Class2();

        }
        public Class2 MyProp { get; set; }
    }

    public class Class2 : MarshalByRefObject
    {
        public string GetText()
        {
            return File.ReadAllText(@"c:\file.txt", Encoding.UTF8);
        }
    }

On the server i create a channel and marshal the class:

var c1 = new Class1();
RemotingServices.Marshal(c1, "myUri.rem", typeof(Class1));

After creating a connection to the server from the client i can do this:

var proxy = (Class1)RemotingServices.Connect(typeof(Class1), "myUri.rem");
var result = proxy.MyProp.GetText();

With .Net Remoting i can access the property MyProp on the remote object and call the function GetText. The result from GetText is serialised back to the client.
The question is how can i access the property MyProp as a TransparentProxy (remote object) with WCF, gRPC or whatever?

My test with WCF is serializing Class2 back to the client, but this is not what i want.

1

There are 1 best solutions below

2
QI You On

I don't quite understand what you mean, but I've seen an example of a piece of WCF remote calls that is based on callback operations.

It is based on Nettcpbinding. The callback method is written on the client so that the server can be both a server and a client.

Server:

ICalculator.cs

 [ServiceContract(CallbackContract = typeof(ICallback))]
    public interface ICalculator
    {
        [OperationContract(IsOneWay = true)]
        void Multiple(double a, double b);
    }

ICallback.cs

 public interface ICallback
    {
        [OperationContract(IsOneWay = true)]
        void DisplayResult(double x, double y, double result);
    }

Calculator.cs

public class CalculatorService : ICalculator
     {
         public void Multiple(double a, double b)
         {
             double result = a * b;
             ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
             callback.DisplayResult(a, b, result);
         }
     }

Server startup:

static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            {
                host.Opened += delegate
                {
                  Console.WriteLine("Start");
                };
                host.Open(); 
                Console.Read();
            }
        }

Client

CallbackWCFService.cs

  public class CallbackWCFService : ICalculatorCallback
    {
        public void DisplayResult(double a, double b, double result)
         {
             Console.WriteLine("{0} * {1} = {2}", a, b, result);
         }
}

Client startup:

 internal class Program
    {
        static void Main(string[] args)
        {
            InstanceContext instanceContex = new InstanceContext(new CallbackWCFService());
                        CalculatorClient proxy = new CalculatorClient(instanceContex);
                        proxy.Multiple(2, 3);
            
             Console.Read();
        }
    }

If it does not meet your expectations, please provide more information. I will continue to help you solve the problem.