当前位置:  开发笔记 > IOS > 正文

在.NET远程处理调用期间标识客户端

如何解决《在.NET远程处理调用期间标识客户端》经验,为你挑选了1个好方法。

鉴于此MarshalByRef类:

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

客户端代码:

MyRemotedClass m = GetSomehowMyRemotedClass();
m.DoThis();
m.DoThat();

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

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

我有大量的代码需要覆盖,包括属性.因此,不能选择扩展输入参数列表.



1> Kev..:

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

将此类添加到远程主机项目:

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();
    }
}


获取客户端IP地址真的很复杂吗?
@dboarman - 这是唯一的方法.但是如果你已经习惯于编写自己的通道接收器类,那么它就成了远程生活的正常部分.
推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有