.NET 远程处理线程模型

发布于 2024-07-13 04:26:33 字数 582 浏览 4 评论 0原文

我想知道如何使用 MarshalByRef 对象在服务器端处理线程。

给定我的远程 MarshalByRef 类:

public class MyRemotedClass : MarshalByRef
{
  public int MyInt;
  public string MyString;
}

客户端代码(单线程):

MyRemotedClass m = GetSomehowMyRemotedClass(); 
m.MyInt = 5; // Write operation 1
m.MyString = "Hello World"; // Write operation 2

在服务器端,我们有两个写入操作。 我假设线程来自ThreadPool。 但是,由于该类是 MarshalByRef,因此这两个操作都是单独的远程过程调用。 他们将在单独的线程上运行吗? 如果是,是否会在操作 1 完成之前执行操作 2?

PS:制作 MyRemotedClass MarshalByRef 是一个错误的决定。 但我无权改变这一点,所以请不要提出这一点。

I would like to know how threads are handled on the server side using MarshalByRef objects.

Given my remoted MarshalByRef class:

public class MyRemotedClass : MarshalByRef
{
  public int MyInt;
  public string MyString;
}

Client code (single threaded):

MyRemotedClass m = GetSomehowMyRemotedClass(); 
m.MyInt = 5; // Write operation 1
m.MyString = "Hello World"; // Write operation 2

On the server side, we have the two write operations. I assume that the thread is from the ThreadPool. However, since the class is MarshalByRef, both opeations are separate remote procedure calls. Are they going to run on separate thread? If yes, can it occur that operation 2 is going to be executed before operation 1 finishes?

PS: Making MyRemotedClass MarshalByRef is BAD decision. But I am not allowed to change that, so please do not propose that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-07-20 04:26:33

我不是这方面的专家,但我确实希望在客户端事件尝试写入 MyString< 之前,对 MyInt 的写入能够可靠地完成/code> 除非你发生了一些奇怪的异步行为。

毕竟,如果分配由于某种原因失败,唯一明智的处理方法是在继续之前抛出异常,IMO。

I'm not an expert on this, but I'd really expect the write to MyInt to have reliably completed before the client event tries to write to MyString unless you've got some funky asynchronous behaviour going on.

After all, if the assignment fails for some reason the only sensible way of handling that is to throw an exception before proceeding, IMO.

早茶月光 2024-07-20 04:26:33

事实上,据我所知,设置 2 个属性并不是异步发生的,因此客户端会等待第一个 RPC 完成,然后才会启动 RPC 2。

无论如何,如果您的 MyRemotedClass 未配置为单例服务器激活的对象,还请记住,对于您在 MyRemotedClass 上执行的每个 RPC,都将在服务器上创建一个新实例。 这意味着远程对象不应包含任何状态。

Indeed, as far as I would think about it, setting the 2 properties is not happening asynchronously, so the client is waiting until the 1st RPC is finished before it will start with RPC 2.

Anyway, if your MyRemotedClass is not configured as a singleton server-activated object, also keep in mind that for every RPC that you do on MyRemotedClass, a new instance will be created on the server. That means that the remote object should not contain any state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文