远程处理中当前调用的端口

发布于 2024-07-11 05:41:12 字数 360 浏览 3 评论 0原文

我的服务可以在同一台计算机上有多个实例,每个实例都位于不同的端口上。

如何检索当前请求所通过的端口号以区分它们?

为了澄清,如果客户端调用以下方法:

class OrdersService : MarshalByRefObject
{
    public void EnqueueOrder(Order order)
    {
        //int port = ??
    }
}

我需要发出请求的端口号。

PS:我尝试实现 IServerChannelSink 接口,但它只能访问客户端的 URI(如 /foo/bar)和 IP 地址。

谢谢

There can be several instances of my service on the same computer, each of them on a different port.

How can I retrieve the port number from which the current request came through to differentiate them?

To clarify, if client is calling the following method:

class OrdersService : MarshalByRefObject
{
    public void EnqueueOrder(Order order)
    {
        //int port = ??
    }
}

I need the port number from which the request was made.

PS: I tried implementing the IServerChannelSink interface but it can only access the URI (like /foo/bar) and IP address of the client.

Thanks

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

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

发布评论

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

评论(2

_失温 2024-07-18 05:41:12

如果您需要回调客户端,那么这应该是您合同的一部分:

public void EnqueueOrder(Order order, IClient client)
{
  // Work with the order.

  // Call back to the client.
}

在客户端,您可以使用派生自 MarshalByRefObject 的类来实现 IClient 接口,并将其传递给 EnqueueOrder 方法。

If you require to call back to the client, then that should be a part of your contract:

public void EnqueueOrder(Order order, IClient client)
{
  // Work with the order.

  // Call back to the client.
}

On the client side, you would implement the IClient interface with a class that derives from MarshalByRefObject, and pass that to the EnqueueOrder method.

半夏半凉 2024-07-18 05:41:12

这是令人发指的,而且不是很普遍,但应该对你有用:

TcpChannel       channel = (TcpChannel)ChannelServices.RegisteredChannels[0];
ChannelDataStore data    = (ChannelDataStore)channel.ChannelData;
string           uri     = data.ChannelUris[0];
int              port    = int.Parse(uri.Substring(uri.LastIndexOf(':')));

请注意,你需要对 System.Runtime.Remoting.dll 的引用,并且你需要 using 用于 System .Runtime.Remoting.ChannelsSystem.Runtime.Remoting.Channels.Tcp

当然,如果您注册了多个通道,和/或它不是 TCP,和/或您有多个通道的 URI,那么您必须编写比我上面所做的 hack-job 更好的代码,但无论如何,它应该给你一个想法。


无论如何,这些单独的服务器实例是如何启动的? 首先如何选择端口? 客户端如何发现端口? 这些问题的答案可能会揭示一种更理想的方法来区分服务器代码中的实例,例如在命令行上传递的实例标识符。

This is heinous, and not very generalized, but should work for you:

TcpChannel       channel = (TcpChannel)ChannelServices.RegisteredChannels[0];
ChannelDataStore data    = (ChannelDataStore)channel.ChannelData;
string           uri     = data.ChannelUris[0];
int              port    = int.Parse(uri.Substring(uri.LastIndexOf(':')));

Note that you'll need a reference to System.Runtime.Remoting.dll and you'll need usings for System.Runtime.Remoting.Channels, and System.Runtime.Remoting.Channels.Tcp.

Of course, if you have more than one channel registered, and/or it's not TCP, and/or you have more than one URI for the channel, then you'll have to write better code than the hack-job I did above, but it should give you the idea, anyway.


How are these separate server instances started, anyway? How is the port selected in the first place? How does the client discover the port? The answers to these questions may reveal a more ideal method for distinguishing the instance within the server's code, such as an instance identifier passed on the command line.

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