简体   繁体   中英

Remoting in C# VS versioning

Using Remoting in C#, I'm connecting to different servers. As I'm continuously adding features on the client side, the services on the server side isn't always up to date.

How can I solve the issue that if I call a method in my interface (which invokes the method remotely on the server) and this method doesn't exist in the implementation on the server side?

The client naturally crashes because there's an exception I cannot catch. Yes I could, but as there are many different Methods I call this way, it would be painful to add Exception handling for each of them.

One "solution" I tought about was to put a custom version-attribute to each of the method in my MarshalByRefObject . But I don't know how to use this afterwards when calling each method. It's okay for me to add an annotation/attribute to each method, but it again would be a pain to have an if statement asking whether the version is new enough or not.

Another way I thought about was to use inheritance for this.

public abstract class AbstractServer1 : MarshalByRefObject
{
    public abstract void feature1();
}

public abstract class AbstractServer2 : AbstractServer1
{
    public abstract featureInVersion2();
    public abstract string reallyNewFeature();
}

public class ServerClass1 : AbstractServer1
{
    public void feature1();
}

public class ServerClass2 : ServerClass1, AbstractServer2
{
    public void featureInVersion2();
    public string reallyNewFeature();
}

But this would result in something like:

public AbstractServer getRemoteObject(string host, int port)
{
    object obj = Activator.GetObject(typeof(AbstractServer2), "tcp://" + host + ":" + port.ToString() + "/ServiceName");
    if (typeof(obj) == ServerClass1)
    {
        ...
    }
}

And this is neither beautiful nor does it really work.

Is there a good solution to this?

Remark: Using the same assembly file in server and client is unfortunately NOT an option.

Any help is appreciated.

What kind of behavior do you expect when calling a function that do not exists serverside ?

This code might be usefull to you

It builds a proxy which catches every exception that occured when calling members of an object implementing a specific interface. You could use this on wpf channels or on interface members of your Remoting proxies.

IMyInterface myService = [...];
IMyInterface myExceptionLoggedService = ExceptionProxyGenerator.Generate<IMyInterface>(myService);
(myExceptionLoggedService  as IExceptionProxy).Error+=(sender,method,exception)=>{
   // do what you want on exception
};
// Use my service :
myExceptionLoggedService.MyExistingFunction();// ok
myExceptionLoggedService.MyNotExistingFunction();// calling the above delegate

You could rewrite a bit this class to have the kind of behavior you want (like not rethrowing the exception on error and returning null -if function is not returning void-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM