当前位置:编程学习 > C#/ASP.NET >>

remoting event

using System;
using System.Collections.Generic;
using System.Text;
using Wayfarer.BroadCast.RemoteObject;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;

namespace server
{
    class Server
    {
        public static BroadCastObj Obj = null;

        public Server() {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 8080;
            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel);
            Obj = new BroadCastObj();
            ObjRef objRef = RemotingServices.Marshal(Obj, "BroadCastMessage.soap");
            Console.WriteLine("server start");
        }
        static void Main(string[] args)
        {
            Server se = new Server();
            Console.WriteLine("server start2");

            Console.Read();
            Server.Obj.BroadCastingInfo("success");
            Console.WriteLine("server start4");
            Console.Read();
            Console.Read();
        }
    }
}
--------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Channels.Http;
using System.Collections;
using Wayfarer.BroadCast.Common;

namespace client
{
    class client
    {
        private IBroadCast watch = null;
        private EventWrapper wrapper = null;
        public client() {
            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);

            watch = (IBroadCast)Activator.GetObject(
                typeof(IBroadCast), "http://localhost:8080/BroadCastMessage.soap");

            wrapper = new EventWrapper();
            wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
            watch.BroadCastEvent += new BroadCastEventHandler(wrapper.BroadCasting);
        }
        public void BroadCastingMessage(string message)
        {
            Console.WriteLine ("I got it:" + message);
           // txtMessage.Text += System.Environment.NewLine;
        }

        private void btnCancle_Click(object sender, System.EventArgs e)
        {
            watch.BroadCastEvent -= new BroadCastEventHandler(wrapper.BroadCasting);
            Console.WriteLine("取消订阅广播成功!");
        }
        static void Main(string[] args)
        {
            client cl = new client();
            Console.Read();
        }
    }

}
--------------------编程问答-------------------- using System;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using Wayfarer.BroadCast.Common;
namespace Wayfarer.BroadCast.RemoteObject
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class BroadCastObj:MarshalByRefObject,IBroadCast
{
public event BroadCastEventHandler BroadCastEvent;
#region IBroadCast 成员
//[OneWay]
public void BroadCastingInfo(string info)
{
if (BroadCastEvent != null)
{
BroadCastEventHandler tempEvent = null;

int index = 1; //记录事件订阅者委托的索引,为方便标识,从1开始。
foreach (Delegate del in BroadCastEvent.GetInvocationList())
{
try
{
tempEvent = (BroadCastEventHandler)del;
tempEvent(info);
}
catch
{
MessageBox.Show("事件订阅者" + index.ToString() + "发生错误,系统将取消事件订阅!");
BroadCastEvent -= tempEvent;
}
index++;
}
}
}
#endregion
public override object InitializeLifetimeService()
{
return null;
}

}
}
AssemblyInfo.cs:using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
--------------------编程问答-------------------- using System;
using System.Runtime.Remoting.Messaging;
namespace Wayfarer.BroadCast.Common
{
/// <summary>
/// EventClass 的摘要说明。
/// </summary>
public class EventWrapper:MarshalByRefObject
{
public event BroadCastEventHandler LocalBroadCastEvent;

//[OneWay]
public void BroadCasting(string message)
{
LocalBroadCastEvent(message);
}
public override object InitializeLifetimeService()
{
return null;
}

}
}
using System;

namespace Wayfarer.BroadCast.Common
{
public delegate void BroadCastEventHandler(string info);

public inte易做图ce IBroadCast
{
event BroadCastEventHandler BroadCastEvent;
void BroadCastingInfo(string info);
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,