鍵盤農夫

科技凝聚实力,激情成就梦想

在NET Remoting中傳遞事件

客戶端

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

阅读更多
想对作者说点什么? 我来说一句

C#中Remoting的簡單用法

2012年10月25日 4KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭