gpt4 book ai didi

.net - 在.NET远程调用期间识别客户端

转载 作者:行者123 更新时间:2023-12-03 20:21:03 25 4
gpt4 key购买 nike

鉴于此MarshalByRef类:

public class MyRemotedClass : MarshalByRef
{
public void DoThis()
{
...
}
public void DoThat()
{
...
}
}

客户端代码:
MyRemotedClass m = GetSomehowMyRemotedClass();
m.DoThis();
m.DoThat();

我可以让几个客户同时做同一件事。
我想区分客户。 如何在远程访问方法中识别执行远程调用的方法?
例如,我可以记录谁做了什么。 (实际上,我不需要追溯真实的客户信息,我只希望能够按客户对调用进行分组。)

[编辑以添加更多背景信息]

我要编写大量代码,包括属性。因此,扩展输入参数列表不是一个选择。

最佳答案

您可以做的一件事情是通过实现IServerChannelSinkProvider通过IP地址识别客户端。

将此类添加到远程宿主项目中:
ClientIPServerSinkProvider.cs

using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Net;

namespace MyRemotingEnvironment
{
public class ClientIPServerSinkProvider :
IServerChannelSinkProvider
{
private IServerChannelSinkProvider _nextProvider = null;

public ClientIPServerSinkProvider()
{
}

public ClientIPServerSinkProvider(
IDictionary properties,
ICollection providerData)
{
}

public IServerChannelSinkProvider Next
{
get { return _nextProvider; }
set { _nextProvider = value; }
}

public IServerChannelSink CreateSink(IChannelReceiver channel)
{
IServerChannelSink nextSink = null;

if (_nextProvider != null)
{
nextSink = _nextProvider.CreateSink(channel);
}
return new ClientIPServerSink(nextSink);
}

public void GetChannelData(IChannelDataStore channelData)
{
}
}



public class ClientIPServerSink :
BaseChannelObjectWithProperties,
IServerChannelSink,
IChannelSinkBase
{

private IServerChannelSink _nextSink;

public ClientIPServerSink(IServerChannelSink next)
{
_nextSink = next;
}

public IServerChannelSink NextChannelSink
{
get { return _nextSink; }
set { _nextSink = value; }
}

public void AsyncProcessResponse(
IServerResponseChannelSinkStack sinkStack,
Object state,
IMessage message,
ITransportHeaders headers,
Stream stream)
{
IPAddress ip = headers[CommonTransportKeys.IPAddress] as IPAddress;
CallContext.SetData("ClientIPAddress", ip);
sinkStack.AsyncProcessResponse(message, headers, stream);
}

public Stream GetResponseStream(
IServerResponseChannelSinkStack sinkStack,
Object state,
IMessage message,
ITransportHeaders headers)
{

return null;

}


public ServerProcessing ProcessMessage(
IServerChannelSinkStack sinkStack,
IMessage requestMsg,
ITransportHeaders requestHeaders,
Stream requestStream,
out IMessage responseMsg,
out ITransportHeaders responseHeaders,
out Stream responseStream)
{
if (_nextSink != null)
{
IPAddress ip =
requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;
CallContext.SetData("ClientIPAddress", ip);
ServerProcessing spres = _nextSink.ProcessMessage(
sinkStack,
requestMsg,
requestHeaders,
requestStream,
out responseMsg,
out responseHeaders,
out responseStream);
return spres;
}
else
{
responseMsg = null;
responseHeaders = null;
responseStream = null;
return new ServerProcessing();
}
}


}
}

然后,当您启动远程主机时,请执行以下操作:
BinaryServerFormatterSinkProvider bp = new BinaryServerFormatterSinkProvider();
ClientIPServerSinkProvider csp = new ClientIPServerSinkProvider();
csp.Next = bp;
Hashtable ht = new Hashtable();
ht.Add("port", "1234"); // Your remoting port number
TcpChannel channel = new TcpChannel(ht, null, csp);
ChannelServices.RegisterChannel(channel, false);

RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MyRemotedClass),
"MyRemotedClass.rem",
WellKnownObjectMode.SingleCall);

在方法调用中,您可以执行以下操作来访问客户端的IP地址:
public class MyRemotedClass : MarshalByref
{
public void DoThis()
{
string clientIP = CallContext.GetData("ClientIPAddress").ToString();
}
}

关于.net - 在.NET远程调用期间识别客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/527676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com