Identifying the client during a .NET remoting invocation

转载 2013年12月02日 16:46:02

http://stackoverflow.com/questions/527676/identifying-the-client-during-a-net-remoting-invocation/527756#527756


One of the things you can do is identify a client by IP address by implementing anIServerChannelSinkProvider.

Add this class to your remoting host project:

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


    }
}
Then when you start your remoting host do something like the following:

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

In your method calls you can access the IP address of the client by doing:

public class MyRemotedClass : MarshalByref
{
    public void DoThis()
    {
    	string clientIP = CallContext.GetData("ClientIPAddress").ToString();
    }
}

Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model

Steps Involved: 1.       Open Visual Studio 2012 (Run as administrator). 2.       Go to File=> New...
  • lviiii
  • lviiii
  • 2014年08月07日 18:57
  • 796

.Net Remoting与WCF实现Server与Client通讯比较

 .Net Remoting与WCF实现Server与Client通讯比较 2011-11-13 13:51:36     我来说两句       收藏    我要投稿   ...

.NET Remoting与分布式应用开发

.NET Remoting与分布式应用开发——初识 Remoting作者:叶小舟下载源代码一、Remoting简介  .NET Remoting(下文简称Remoting)是一种可用于开发分布式应用程...
  • wh9602
  • wh9602
  • 2011年03月31日 01:27
  • 307

使用.NET Remoting开发分布式应用——配置文件篇

http://www.csharpwin.com/csharpspace/3005.shtml
  • sage425
  • sage425
  • 2011年03月31日 18:22
  • 515

Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务

 我写的.Net Remoting系列专题: Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇 Microsoft .Net Remoting系列...

.Net Remoting(应用程序域) - Part.1(张子阳)

引言在互联网日渐普及,网络传输速度不断提高的情况下,分布式的应用程序是软件开发的一个重要方向。在.Net中,我们可以通过Web Service 或者Remoting 技术构建分布式应用程序(除此还有新...
  • ldpong
  • ldpong
  • 2011年03月22日 10:01
  • 535

.Net中Remoting通信的应用,有发送和返回信息

两个界面如图: 代码: 服务端 using System; using System.Collections.Generic; using System.ComponentM...

[转]解析.net remoting 技术要点

这两者都是基于分布式的开发,而且.Net Remoting有时也可以配置为Web Service,两者有很多的相同之处。 一般来讲,我把他们的不同之处列为5个方面。...
  • sage425
  • sage425
  • 2011年03月31日 14:36
  • 573

WebService与.NET Remoting

为了能清楚地描述Web Service 和Remoting之间的区别,我打算从他们的体系结构上来说起: Web Service大体上分为5个层次: 1. Http传输信道 2. XML的数...
  • lile010
  • lile010
  • 2014年02月10日 17:01
  • 344

.Net中Remoting技术应用简述

一,Remoting技术简介: .Net Remoting 技术是.NET平台上实现分布式对象系统的框架,它可以用于访问另一个应用程序域(也可以是另一个服务器)中的对象,可以是进程间的,也可以...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Identifying the client during a .NET remoting invocation
举报原因:
原因补充:

(最多只允许输入30个字)