C# 中的远程处理 VS 版本控制
在 C# 中使用远程处理,我连接到不同的服务器。由于我不断在客户端添加功能,因此服务器端的服务并不总是最新的。
如果我在接口中调用一个方法(在服务器上远程调用该方法)并且该方法在服务器端的实现中不存在,如何解决这个问题?
客户端自然崩溃,因为有一个我无法捕获的异常。是的,我可以,但是由于我以这种方式调用了许多不同的方法,因此为每个方法添加异常处理会很痛苦。
我考虑过的一个“解决方案”是为我的 MarshalByRefObject
中的每个方法添加一个自定义版本属性。但我不知道在调用每个方法时如何使用它。对我来说,为每个方法添加注释/属性是可以的,但是使用 if
语句询问版本是否足够新又会很痛苦。
我想到的另一种方法是使用继承来实现这一点。
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();
}
但这会导致类似的结果:
public AbstractServer getRemoteObject(string host, int port)
{
object obj = Activator.GetObject(typeof(AbstractServer2), "tcp://" + host + ":" + port.ToString() + "/ServiceName");
if (typeof(obj) == ServerClass1)
{
...
}
}
这既不美观,也不起作用。
对此有好的解决办法吗?
备注:不幸的是,在服务器和客户端中使用相同的程序集文件不是一个选项。
任何帮助表示赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当调用服务器端不存在的函数时,您期望什么样的行为?
此代码可能对您有用
它构建了一个代理,可以捕获调用时发生的每个异常实现特定接口的对象的成员。
您可以在 wpf 通道或远程代理的接口成员上使用它。
您可以稍微重写这个类以获得您想要的行为(例如在错误时不重新抛出异常并返回 null -if 函数不返回 void-)
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.
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-)