关于软件架构的一点想法(2)

重点介绍.net remoting的运作方式。

我们先来看看一个网上一个例子。
新建一个类库项目,类一定要继承MarshalByRefObject 类(关于这个类的介绍网上大量的文档):

//代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Tclywork.Remoting.Server
{
public class Test:MarshalByRefObject
{
public Test()
{ }
public string TestString(String strClient)
{
return strClient;
}
}
}
新建一个服务端的应用程序,加入命名空间Tclywork.Remoting.Server的引用。

//代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp ;
using System.Security.Permissions;

using Tclywork.Remoting.Server;
namespace Tclywork.Remoting.ServerApp
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}

private void btnStartSevices_Click(object sender, EventArgs e)
{

HttpChannel httpChannel = new HttpChannel(8081); ///创建Tcp通道
this.retRomotingServerAppState.Text = this.retRomotingServerAppState.Text+"创建HTTP通道tn";
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(httpChannel); ///创建Http通道
this.retRomotingServerAppState.Text = this.retRomotingServerAppState.Text + "登记HTTP通道成功tn";
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Test), "TestService", WellKnownObjectMode.Singleton);
this.retRomotingServerAppState.Text = this.retRomotingServerAppState.Text + "登记服务成功tn";

}

private void btnStopServices_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void Main_Load(object sender, EventArgs e)
{

}
}
}

那么这个服务端程序就建立好了TestService服务,并通过http方式,通道端口为8081对外提供服务。
然后建立客户端的应用程序,同样,加入命名空间Tclywork.Remoting.Server的引用。


//代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Security.Permissions;
using Tclywork.Remoting.Server;
namespace Tclywork.Frame
{
public partial class FrameMain : Form
{
public FrameMain()
{
InitializeComponent();
}

private void FrameMain_Load(object sender, EventArgs e)
{

}

private void btnTest_Click(object sender, EventArgs e)
{
HttpChannel httpChannel = new HttpChannel(8082);
ChannelServices.RegisterChannel(httpChannel);
Test test = (Test)Activator.GetObject(typeof(Tclywork.Remoting.Server.Test), "http://localhost:8081/TestService");
if (test == null)
{
MessageBox.Show("连接服务器失败");
}
else
{
string strTemp = "我在测试服务回传";
string strRe = test.TestString(strTemp);

MessageBox.Show(strRe);
}

}
}
}

在客户端建立http联机方式,采用8082端口对服务器http://localhost:8081/TestService进行通信。因为服务端和客户端是在同一台电脑上,所以这个地方采用了localhost,如果是分开两台电脑运行,就可以填写对方的ip地址。

测试代码是在客户端向服务器发送一个字符串,然后通过服务器调用类库进行处理后返送给客户端。

结果测试成功的。

那么我们在这个过程当中可以看到remoting的整个运行机制,虽然最为简单的一个例子,但是我们可以看到客户端的运作信息是通过服务器的运作完成的,而客户端只是一个表示层。那么我们往深层次看,客户端的升级,用户界面的控制,和实现方法都是可以由服务器的类库来控制的,客户端只是服务器运行结果,同时,客户端自己可以处理的事务都可以由自己来完成。

通过.net remoting模式,我们就可以完成两个Applicationg程序的通信,完成我们c/s模式下的控制。甚至我们可以通过这个建立一个通用的企业库,真正做到分布式的运作。

吃饭去了。

请使用浏览器的分享功能分享到微信等