ConsoleServer:
配置文件:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknow mode="SingleCall" type="ConsoleServer.HelloServer,ConsoleServer" objectUri="HelloServer" />
</service>
<channels>
<channel ref="tcp" port="8888"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
代码:
ConsoleServer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace ConsoleServer
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("ConsoleServer.exe.config",false);
创建TCP通道
//TcpChannel chan = new TcpChannel(8888); //必须指定端口号
注册通道
//ChannelServices.RegisterChannel(chan, false); //借用IIS的安全机制
加载远程对象
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "HelloServer", WellKnownObjectMode.Singleton);
Console.Read();
}
}
}
HelloServer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleServer
{
/// <summary>
/// 业务代码
/// </summary>
public class HelloServer:MarshalByRefObject
{
public string HelloWorld(string str)
{
return "张三说: Hello" + str;
}
}
}
ConsoleClient:
配置文件:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.runtime.remoting>
<application>
<client>
<wellknow type="ConsoleServer.HelloServer,ConsoleServer" url="tcp://localhost:8888/HelloServer" />
</client>
</application>
</system.runtime.remoting>
</configuration>
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using ConsoleServer;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("ConsoleClient.exe.config",false);
HelloServer h = new HelloServer();
建立通信通道
//TcpChannel chan = new TcpChannel();
注册通道
//ChannelServices.RegisterChannel(chan, false);
//HelloServer h = (HelloServer)Activator.GetObject(typeof(ConsoleServer.HelloServer),"tcp://localhost:8888/HelloServer");
string str = h.HelloWorld("aaa");
Console.WriteLine(str);
Console.ReadKey();
}
}
}