简体   繁体   English

在C#VS版本中进行远程处理

[英]Remoting in C# VS versioning

Using Remoting in C#, I'm connecting to different servers. 在C#中使用远程处理,我正在连接到其他服务器。 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 . 我坚决提出的“解决方案”是在我的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. 对每个方法添加注释/属性是可以的,但是再次使用if语句询问版本是否足够新将是一件很痛苦的事情。

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. 您可以在wpf通道或Remoting代理的接口成员上使用此功能。

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-) 您可以重写此类以使其具有所需的行为(例如不将错误抛出异常并返回null -if函数未返回void-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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