C# VS 版本控制中的远程处理 [英] Remoting in C# VS versioning
问题描述
在 C# 中使用远程处理,我连接到不同的服务器.由于我不断在客户端添加功能,服务器端的服务并不总是最新的.
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.
我想到的一个解决方案"是在我的 MarshalByRefObject
中为每个方法添加一个自定义版本属性.但是我不知道在调用每个方法时如何使用它.我可以为每个方法添加注释/属性,但是如果使用 if
语句询问版本是否足够新,这又会很痛苦.
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)
{
...
}
}
这既不美观也不行.
有什么好的解决办法吗?
Is there a good solution to this?
备注:不幸的是,在服务器和客户端中使用相同的程序集文件不是一种选择.
Remark: Using the same assembly file in server and client is unfortunately NOT an option.
感谢任何帮助.
推荐答案
在调用服务器端不存在的函数时,您期望什么样的行为?
What kind of behavior do you expect when calling a function that do not exists serverside ?
它构建了一个代理,它捕获调用实现特定接口的对象成员时发生的每个异常.您可以在 wpf 频道或远程代理的接口成员上使用它.
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
你可以稍微重写这个类以获得你想要的那种行为(比如不重新抛出错误时的异常和返回 null -如果函数没有返回 void-)
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-)
这篇关于C# VS 版本控制中的远程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!