一、创建一个类库项目,创建用于remoting的业务类。编译后生成dll。
namespace RemoteHello
{
public class Hello : System.MarshalByRefObject
{
public Hello()
{
System.Console.WriteLine("constructor hello object!");
}
~Hello()
{
}
public string HelloWorld(string name)
{
return "Hi," + name;
}
}
}
二、创建服务端。新建一个控制台程序,完成main函数:
namespace HelloService
{
class Program
{
static void Main(string[] args)
{
//IPC调用
/*
IpcServerChannel ipc = new IpcServerChannel("HelloService");
ChannelServices.RegisterChannel(ipc, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
*/
//TCP调用
/*
TcpServerChannel channel = new TcpServerChannel(6666);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
*/
//配置文件
RemotingConfiguration.Configure("server.config", false);
System.Console.WriteLine("按任意键退出.\n");
System.Console.ReadLine();
}
}
}
创建server.config配置文件,其中指定通道、发布类等信息:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="HelloService">
<channels>
<channel ref="tcp" port="6666">
<clientProviders>
<formatter ref="binary"/>
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
<service>
<!--SingleCall每次调用创建一个实例 SingleTon单例 -->
<!-- ellknown mode="Singleton" type="RemoteHello.Hello, RemoteHello" objectUri="HelloWorld" / -->
<!--<activated mode="SingleTon" type="RemoteHello.Hello, RemoteHello" /> -->
<wellknown mode="Singleton" type="RemoteHello.Hello, RemoteHello" objectUri="HelloWorld" />
<wellknown mode="Singleton" type="RemoteHello.RemoteObject, RemoteHello" objectUri="RemoteObject" />
</service>
</application>
</system.runtime.remoting>
</configuration>
三、创建客户端。新建一个控制台程序,完成main函数:
namespace HelloClient
{
class Program
{
static void Main(string[] args)
{
//IPC调用
/*
ChannelServices.RegisterChannel(new IpcClientChannel(), false);
Hello obj = (Hello)Activator.GetObject(typeof(Hello), "ipc://HelloService/HelloWorld");
*/
//TCP
/*
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:6666/HelloWorld");
*/
//配置文件
//RemotingConfiguration.Configure("Client.config", false);
RemotingConfiguration.Configure(@"IISClient.config", false);
//cao调用,sao调用相同
for (int i = 0; i < 6; i++)
{
//Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:6666/HelloWorld");
Hello obj = new Hello();
Console.WriteLine(obj.HelloWorld("MadRam.neo"));
}
IRemoteObject iRem = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "tcp://localhost:6666/RemoteObject");
Console.WriteLine(">>>>" + iRem.SayHello());
Console.ReadLine();
}
}
}
创建Client.config配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp">
<clientProviders>
<formatter ref="binary"/>
</clientProviders>
</channel>
</channels>
<client>
<!-- url="tcp://localhost:6666/HelloService"-->
<!--wellknown是服务端创建的对象(sao) activated是客户端创建的对象(cao)-->
<!-- wellknown type="RemoteHello.Hello, RemoteHello" url="tcp://localhost:6666/HelloWorld" / -->
<!--activated mode="SingleTon" type="RemoteHello.Hello, RemoteHello" /> -->
<wellknown type="RemoteHello.Hello, RemoteHello" url="tcp://localhost:6666/HelloWorld"/>
<wellknown type="RemoteHello.IRemoteObject, RemoteHello" url="tcp://localhost:6666/RemoteObject" />
</client>
</application>
</system.runtime.remoting>
</configuration>
至此,运行服务端和客户端,就可以测试了。通过修改客户端和服务端配置文件,就可以调整通道(tcp、http、ipc)。
四、部署到IIS。
新建一个目录D:\IIS_Remoting、D:\IIS_Remoting\Bin。在IIS_Remoting下创建一个web.config配置文件,将步骤一创建的dll拷贝到Bin目录。
web.config配置文件内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="Singleton"
type="RemoteHello.Hello, RemoteHello"
objectUri="Hello.soap"/>
</service>
<channels>
<channel
name="MyChannel"
priority="100"
ref="http"
/>
</channels>
</application>
</system.runtime.remoting>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="True"/>
</security>
</system.webServer>
</configuration>
在IIS7上新建网站,虚拟目录指定为D:\IIS_Remoting,并开放IIS的isapi权限。
在步骤三中的配置文件替换为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type="RemoteHello.Hello, RemoteHello" url="http://localhost:8081/Hello.soap"/>
</client>
</application>
</system.runtime.remoting>
</configuration>
启动IIS网站,并执行客户端程序,测试成功。