在NET Remoting中傳遞事件

转载 2011年07月26日 17:20:42

客戶端

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Net;
using System.Runtime.Serialization.Formatters;
using FaxCommon;

namespace Client
{
    public partial class ClientForm : Form
    {
        private IFaxBusiness m_fax =null;
        private EventWrapper m_Wrapper = null;
        private IBroadCast m_Watch = null;

        private delegate void SetTextCallBack(string Text);

        private string GetIPAddress()
        {
            IPHostEntry ipHE = Dns.GetHostEntry(Dns.GetHostName());
            return ipHE.AddressList[0].ToString();
        }

        public ClientForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 0;
            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);

            ChannelServices.RegisterChannel(channel, false);

            m_Watch = (IBroadCast)Activator.GetObject(typeof(IBroadCast), "http://192.168.168.9:1234/BroadCastMessage.soap");
            m_Wrapper = new EventWrapper();
            m_Wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
            m_Watch.BroadCastEvent += new BroadCastEventHandler(m_Wrapper.BroadCasting);

            m_fax = (IFaxBusiness)System.Activator.GetObject(
                typeof(IFaxBusiness),
                "http://km-it-p09:1234/FaxBusiness.soap");
        }

        public void BroadCastingMessage(string message)
        {
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallBack d = new SetTextCallBack(BroadCastingMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                textBox1.Text += "I got it:" + message;
                textBox1.Text += System.Environment.NewLine;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                string faxstr = "來自" + GetIPAddress() + "客戶端的傳真" + System.Environment.NewLine;
                faxstr += textBox1.Text;
                m_fax.SendFax(faxstr);
            }
            else
            {
                MessageBox.Show("請輸入傳輸內容!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_Watch.BroadCastEvent  -= new BroadCastEventHandler(m_Wrapper.BroadCasting);
            MessageBox.Show("取消訂閱廣播成功!");
        }

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Watch.BroadCastEvent -= new BroadCastEventHandler(m_Wrapper.BroadCasting);
        }
    }
}

 

 //連接庫

using System;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using FaxCommon;

namespace FaxProcess
{
    public delegate void FaxEventHandler(string Fax);

    public class FaxBusiness : MarshalByRefObject , IFaxBusiness 
    {
        public static event FaxEventHandler FaxSendedEvent = null;

        public void SendFax(string Fax)
        {
            if (FaxSendedEvent != null)
            {
                FaxSendedEvent(Fax);
            }
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }

    public class BroadCast : MarshalByRefObject, IBroadCast
    {
        public event BroadCastEventHandler BroadCastEvent = null;

        //[OneWay]
        public void BroadCastingInfo(string info)
        {
            if (BroadCastEvent != null)
            {
                BroadCastEventHandler tempEvent = null;
                int index = 1; //記錄事件訂閱者委托的索引

                foreach (Delegate del in BroadCastEvent.GetInvocationList())
                {
                    try
                    {
                        tempEvent = (BroadCastEventHandler)del;
                        tempEvent(info);
                    }
                    catch
                    {
                        MessageBox.Show("事件訂閱者" + index.ToString() + "發生錯誤,系統將取消事件訂閱");
                        BroadCastEvent -= tempEvent;
                    }

                    index++;
                }
            }
            else
            {
                MessageBox.Show("事件未被訂閱或發生錯誤!");
            }
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
//連接庫

 

using System;
using System.Collections.Generic;
using System.Text;

namespace FaxCommon
{
    public class EventWrapper : MarshalByRefObject
    {
        public event BroadCastEventHandler LocalBroadCastEvent;

        public void BroadCasting(string message)
        {
            LocalBroadCastEvent(message);
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }

    }
}
using System;

namespace FaxCommon
{
    public delegate void BroadCastEventHandler(string info);

    public interface IFaxBusiness
    {
        void SendFax(string Fax);
    }

    public interface IBroadCast
    {
        event BroadCastEventHandler BroadCastEvent;
        void BroadCastingInfo(string Info);
    }
}

//服務端

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using FaxProcess;


namespace Server
{
    public partial class ServerForm : Form
    {
        //遠程對象變量
        private  static BroadCast  Obj = null;

        delegate void SetTextCallback(string text);

        //winForm控制項線程安全訪問方式
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
                textBox1.Text += System.Environment.NewLine;
            }
        }


        public ServerForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

            IDictionary props = new Hashtable();
            props["port"] = 1234;
            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel,false );
            Obj = new BroadCast();
            ObjRef objRef = RemotingServices.Marshal(Obj, "BroadCastMessage.soap");

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(FaxBusiness),
                "FaxBusiness.soap",
                WellKnownObjectMode.Singleton);

            FaxBusiness.FaxSendedEvent += new FaxEventHandler(OnFaxSended);

        }

        public void OnFaxSended(string Fax)
        {
            this.SetText(Fax);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Obj.BroadCastingInfo(textBox1.Text);
        }
    }
}

ASP.NET MVC3如何用JQuery传递数组(集合)类型参数给JsonResult Action

ASP.NET MVC3如何用JQuery传递数组(集合)类型参数给JsonResult Action Edi_Wang 2012-10-16 星期二 22:00 ...
  • niewq
  • niewq
  • 2013年12月29日 23:42
  • 2764

.Net中Remoting通信机制

Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可...
  • CoderJYF
  • CoderJYF
  • 2016年12月06日 17:25
  • 1403

.NET Remoting 最简单示例

学习技术知识一个好的方法是先动手,再深入, 给出一个最简单的Remoting程序示例(C#)如下: Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下: us...
  • kissqw
  • kissqw
  • 2014年01月21日 15:29
  • 20222

使用.NET Remoting从服务器主动发出事件通知客户端远程广播

  • 2005年11月14日 16:03
  • 111KB
  • 下载

.net remoting事件订阅

  • 2010年11月12日 17:38
  • 83KB
  • 下载

Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触

Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触 (转)Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命...
  • yjg428
  • yjg428
  • 2011年02月07日 21:00
  • 358

Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触

我写的.Net Remoting系列专题: Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务 Microsoft .Net Rem...
  • u010476739
  • u010476739
  • 2016年09月09日 15:05
  • 495

Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触

前言:在Remoting中处理事件其实并不复杂,但其中有些技巧需要你去挖掘出来。正是这些技巧,仿佛森严的壁垒,让许多人望而生畏,或者是不知所谓,最后放弃了事件在Remoting的使用。关于这个主题,在...
  • sdwyq1238
  • sdwyq1238
  • 2013年04月08日 10:40
  • 309

Remoting事件(服务端广播改进).

  • 2008年07月04日 13:31
  • 114KB
  • 下载

Remoting事件

  • 2009年10月19日 10:58
  • 120KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在NET Remoting中傳遞事件
举报原因:
原因补充:

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