Remoting in C# VS versioning

时光毁灭记忆、已成空白 提交于 2019-12-08 01:38:39

问题


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.


回答1:


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-)



来源:https://stackoverflow.com/questions/7441294/remoting-in-c-sharp-vs-versioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!