简单Remoting 客户端服务器端调用测试

本文介绍了如何使用 C# 实现客户端和服务端的远程对象调用,包括客户端连接配置、服务端配置文件设置及共享库定义。

1.Client

 

ContractedBlock.gifExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemotingClient
{
class MyClient
{
[STAThread]
static void Main(string[] args)
{
RemotingLib.MyObject app
=
(RemotingLib.MyObject)Activator.GetObject(
typeof(RemotingLib.MyObject),
System.Configuration.ConfigurationSettings.AppSettings[
"ServiceURL"]);
int i = app.Add(1, 2);
Console.WriteLine(i);
Console.ReadLine();
}
}
}

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://192.168.17.199:9999/RemotingLib.MyObject"/>
</appSettings>
</configuration>

 

注意:添加Lib 的Dll, 与Server 引用的相同

 

2.Server

 

ContractedBlock.gifExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;

namespace RemotingServer
{
class MyServer
{
[STAThread]
static void Main(string[] args)
{
RemotingConfiguration.Configure(
"RemotingServer.exe.config");
Console.ReadLine();
}
}
}

<?xml version="1.0"?>
<configuration>
<system.runtime.remoting>
<application name="RemotingServer">
<service>
<wellknown type="RemotingLib.MyObject,RemotingLib" objectUri="RemotingLib.MyObject"
mode
="Singleton" />
</service>
<channels>
<channel ref="tcp" port="9999"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

 

注意:添加Lib 的Dll, 与Client 引用的相同

 

3.Lib

 

ContractedBlock.gifExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemotingLib
{
public class MyObject : MarshalByRefObject
{
public int Add(int a, int b)
{
return a + b;
}
}
}

 

注意:Lib 的命名空间注意一定要与编译后的相同,要不然无法加载

 

转载于:https://www.cnblogs.com/liulf/archive/2010/08/17/1801593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值