C# 使用MarshalByRefObject跨程序调用方法

简介: MarshalByRefObject 是通过使用代理交换消息来跨应用程序域边界进行通信的对象的基类。不是从 MarshalByRefObject 继承的对象根据值隐式封送。
MarshalByRefObject  是通过使用代理交换消息来跨应用程序域边界进行通信的对象的基类。不是从   MarshalByRefObject  继承的对象根据值隐式封送。当远程应用程序引用根据值封送的对象时,将跨应用程序域边界传递该对象的副本。
===================
//程序1和程序2的共有类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemoteObject
{
    public class CommunicationInfo : MarshalByRefObject
    {
        public CommunicationInfo()
        {
        }
        public override object InitializeLifetimeService()
        {
            //////return base.InitializeLifetimeService();
            System.Runtime.Remoting.Lifetime.ILease aLease
                = (System.Runtime.Remoting.Lifetime.ILease)base.InitializeLifetimeService();

            if (aLease.CurrentState == System.Runtime.Remoting.Lifetime.LeaseState.Initial)
            {
                // 不过期
                aLease.InitialLeaseTime = TimeSpan.Zero;
            }
            return aLease;
        }
        public class CommunicationEventArg : EventArgs
        {
        }
        public delegate void JobAddEventHandler(CommunicationEventArg e);
        public event JobAddEventHandler OnJobAdd;
        public void CallJobAddEvent()
        {
            OnJobAdd(new CommunicationEventArg());
        }
    }
}
================================================
程序1(服务器端)启动时运行
参照中追加System.Runtime.Remoting

RemoteObject.CommunicationInfo _communicationInfo;

System.Runtime.Remoting.Channels.Tcp.TcpServerChannel servChannel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(18089);

System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(servChannel, true);

_communicationInfo = new RemoteObject.CommunicationInfo();

_communicationInfo.OnJobAdd += new RemoteObject.CommunicationInfo.JobAddEventHandler(_communicationInfo_OnJobAdd);

System.Runtime.Remoting.RemotingServices.Marshal(_communicationInfo, "message1",typeof(RemoteObject.CommunicationInfo));

//测试事件

privatevoid _communicationInfo_OnJobAdd(RemoteObject.CommunicationInfo.CommunicationEventArg e)

{

MessageBox.Show("TEST OK");

}
================================================
程序2(客户端)调用方法时运行
参照中追加System.Runtime.Remoting

System.Runtime.Remoting.Channels.Tcp.TcpClientChannel _clientChannel;

_clientChannel = new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();

System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_clientChannel, true);

RemoteObject.CommunicationInfo _communicationInfo;

_communicationInfo = (RemoteObject.CommunicationInfo)

System.Activator.GetObject(typeof(RemoteObject.CommunicationInfo),"tcp://localhost:18089/message1", System.Runtime.Remoting.WellKnownObjectMode.Singleton);

_communicationInfo.CallJobAddEvent();//启动事件
目录
相关文章
|
5月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
5月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
14天前
|
开发框架 .NET 程序员
C# 去掉字符串最后一个字符的 4 种方法
在实际业务中,我们经常会遇到在循环中拼接字符串的场景,循环结束之后拼接得到的字符串的最后一个字符往往需要去掉,看看 C# 提供了哪4种方法可以高效去掉字符串的最后一个字符
|
4月前
|
数据采集 数据可视化 测试技术
C#生成Selenium测试报告:实用方法与技巧
在C#中使用Selenium进行自动化测试时,结合代理IP和ExtentReports能增强测试安全性和报告质量。安装必备工具如Selenium WebDriver、NUnit和ExtentReports。在测试设置中,配置代理(如亿牛云爬虫代理)以隐藏IP,通过ChromeOptions定制UserAgent,并添加Cookie。测试代码示例展示了如何打开网页、执行搜索并生成详细的测试报告。使用ExtentReports可创建可视化测试结果,便于团队分析。
C#生成Selenium测试报告:实用方法与技巧
|
1月前
|
C#
C#一分钟浅谈:Lambda 表达式和匿名方法
本文详细介绍了C#编程中的Lambda表达式与匿名方法,两者均可用于定义无名函数,使代码更简洁易维护。文章通过基础概念讲解和示例对比,展示了各自语法特点,如Lambda表达式的`(parameters) => expression`形式及匿名方法的`delegate(parameters)`结构。并通过实例演示了两者的应用差异,强调了在使用Lambda时应注意闭包问题及其解决策略,推荐优先使用Lambda表达式以增强代码可读性。
30 8
|
2月前
|
图形学 C# 开发者
全面掌握Unity游戏开发核心技术:C#脚本编程从入门到精通——详解生命周期方法、事件处理与面向对象设计,助你打造高效稳定的互动娱乐体验
【8月更文挑战第31天】Unity 是一款强大的游戏开发平台,支持多种编程语言,其中 C# 最为常用。本文介绍 C# 在 Unity 中的应用,涵盖脚本生命周期、常用函数、事件处理及面向对象编程等核心概念。通过具体示例,展示如何编写有效的 C# 脚本,包括 Start、Update 和 LateUpdate 等生命周期方法,以及碰撞检测和类继承等高级技巧,帮助开发者掌握 Unity 脚本编程基础,提升游戏开发效率。
45 0
|
2月前
|
C#
C# async await 异步执行方法
C# async await 异步执行方法
44 0
|
2月前
|
C# 图形学
小功能⭐️C#控制小数点后位数的方法
小功能⭐️C#控制小数点后位数的方法
|
2月前
|
C#
WPF/C#:数据绑定到方法
WPF/C#:数据绑定到方法
34 0
|
4月前
|
C#
技术经验分享:C#DUID的用法及取得整数的几个方法
技术经验分享:C#DUID的用法及取得整数的几个方法
62 1