C#构建一个简单的分布式应用程序(.net远程处理)

本文介绍了一个简单的.NET远程对象交互示例,包括三个程序集:SimpleRemotingAsm.dll、SimpleRemoteObjectServer.exe 和 SimpleRemoteObjectClient.exe。示例通过 HttpChannel 实现了客户端和服务端之间的远程对象调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由3个.NET程序集构成:

SimpleRemotingAsm.dll

SimpleRemoteObjectServer.exe

SimpleRemoteObjectClient.exe

下面是代码

SimpleRemotingAsm控制台程序

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

namespace SimpleRemotingAsm
{

//这个类型在被远程访问时会以引用方式封送(MBR)
public class RemoteMessageObject : MarshalByRefObject
{
public RemoteMessageObject()
{
Console.WriteLine("Constructing RemoteMessageObject!");


}
//这个方法从调用那里获取一个输入字符串
public void DisplayMessage(string msg)
{
Console.WriteLine("Message is:{0}", msg);
}
//这个方法把值返回调用方
public string ReturnMessage()
{
return "Hello from the server";
}
}

}

SimpleRemoteObjectServer控制台程序

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;

namespace SimpleRemoteObjectServer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*****SimpleRemoteObjectServer started!******");
Console.WriteLine("Hit enter to end");

//注册一个新的信道
HttpChannel c = new HttpChannel(32469);
ChannelServices.RegisterChannel(c, false);

//注册一个WKO类型,使用单例激活

RemotingConfiguration.RegisterWellKnownServiceType(typeof(SimpleRemotingAsm.RemoteMessageObject), "RemoteMsgObj.soap", WellKnownObjectMode.Singleton);
Console.ReadLine();
}
}
}

SimpleRemoteObjectClient控制台程序

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;
using SimpleRemotingAsm;

namespace SimpleRemoteObjectClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*****SimpleRemoteObjectClient started!******");
Console.WriteLine("Hit enter to end");
//注册一个新的信道
HttpChannel c = new HttpChannel();
ChannelServices.RegisterChannel(c,false);

//注册一个WKO类型
object remoteObj = Activator.GetObject(typeof(SimpleRemotingAsm.RemoteMessageObject),"http://localhost:4313/RemoteMsgObj.soap");
//现在使用远程对象
RemoteMessageObject simple = (RemoteMessageObject)remoteObj;
simple.DisplayMessage("Hello from the client");
Console.WriteLine("Server says:{0}",simple.ReturnMessage());
Console.ReadLine();

}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值