远程处理中当前调用的端口
我的服务可以在同一台计算机上有多个实例,每个实例都位于不同的端口上。
如何检索当前请求所通过的端口号以区分它们?
为了澄清,如果客户端调用以下方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要回调客户端,那么这应该是您合同的一部分:
在客户端,您可以使用派生自 MarshalByRefObject 的类来实现 IClient 接口,并将其传递给 EnqueueOrder 方法。
If you require to call back to the client, then that should be a part of your contract:
On the client side, you would implement the IClient interface with a class that derives from MarshalByRefObject, and pass that to the EnqueueOrder method.
这是令人发指的,而且不是很普遍,但应该对你有用:
请注意,你需要对 System.Runtime.Remoting.dll 的引用,并且你需要
using
用于System .Runtime.Remoting.Channels
和System.Runtime.Remoting.Channels.Tcp
。当然,如果您注册了多个通道,和/或它不是 TCP,和/或您有多个通道的 URI,那么您必须编写比我上面所做的 hack-job 更好的代码,但无论如何,它应该给你一个想法。
无论如何,这些单独的服务器实例是如何启动的? 首先如何选择端口? 客户端如何发现端口? 这些问题的答案可能会揭示一种更理想的方法来区分服务器代码中的实例,例如在命令行上传递的实例标识符。
This is heinous, and not very generalized, but should work for you:
Note that you'll need a reference to System.Runtime.Remoting.dll and you'll need
using
s forSystem.Runtime.Remoting.Channels
, andSystem.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.