测试通过后,对系统的架构布局有了新的认识。
在以前,做WEB系统都是参考微软给的示例进行设计部署,一个系统经常分了好几层,简而言之,纯粹为了分层而分层。那时自己也觉得修改维护挺麻烦,特别是做的WEB系统也不是很大,一般也就四五十个表左右的系统。
虽然如此,我还是坚持采用多层结构,并且层与层之间的设计越来越规范,基本上可以做到当修改其中某个层的内容时,不会对其他的受影响。并且设计的结构也越来越复杂,现在随便设计一个WEB系统,十来层正常的,就好象Petshop一样,第四版比第三版多了N个层。
当然,如果仅仅是WEB系统,倒没有必要这么复杂。这也是为了以后公司可能会出现相关的需求,比如说C/S结构的系统,或者要提供接口给其他兄弟单位。就好象现在SOA就非常流行,那么WEB Service ,Remoting应用就在所难免了,这样一旦系统改变,我仅仅需要新增一个项目,把接口继承过来即可。甚至可以编写程序自动生成WEB Service或者Remoting的类文件,去年就差点写这么个自动工具,后来接口比较统一,数量也不多,所以就取消了。当经历过这些后,多层结构的优势我才真正感觉出来。
废话少说,下面就讲Remotintg的简单实现以及服务器端的windows service注册,本文是以VS2005做示范的,VS2008里已经有了WCF,所以不提供windows service项目模板了。
首先,服务器端的代码,基本上就是建立一个tcp或者http之类的通道:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Model;
namespace WindowsService1

{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
TcpChannel chan = new TcpChannel(8085);

ChannelServices.RegisterChannel(chan, false /**//*ensureSecurity*/);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "SayHello", WellKnownObjectMode.SingleCall);
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}
其次,建立接口,这样是为了在客户端时不需要把封装业务流程的dll,而只需要接口即可。
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface

{
public interface IHelloServer 
{
String HelloMethod(String name);
}
}
第三,建立业务类:
using System;
using System.Collections.Generic;
using System.Text;
using Interface;
namespace Model
{
public class HelloServer : MarshalByRefObject, IHelloServer
{
public HelloServer()
{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
{
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
}
}
第四,建立客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Interface;
namespace ConsoleApplication1

{
class Program
{
public static int Main(string[] args)
{
try
{
IHelloServer obj = (IHelloServer)Activator.GetObject(
typeof(IHelloServer),
"tcp://10.192.0.12:8085/SayHello");
if (obj == null) System.Console.WriteLine("Could not locate server");
else Console.WriteLine(obj.HelloMethod("KiloNet"));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
System.Console.ReadLine();
return 0;
}
}
}
这个时候,基本上可以运行了,如果大家有.Net的QuickStart的话,可以查看到,我服务器端和客户端的代码都是从那里复制过来的。
但是如果把服务器端的程序注册到windows service,我之前以为很简单,弄个安装项目,结果没有成功,查资料才明白,还需要在服务端的项目文件里加一个Install class:
namespace WindowsService1

{
partial class Installer1
{
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

Component Designer generated code#region Component Designer generated code

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
// 设定服务名称
this.sInstaller.ServiceName = "WindowsService1";
// 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[]
{ this.spInstaller, this.sInstaller });
}
#endregion
}
}最后用cmd命令安装服务器端即可,当然你也可以选择新建一个安装项目:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WindowsService1.exe
net start WindowsService1
如果想删除服务,则运行
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WindowsService1.exe /u
现在大功告成,只需要添枝加叶,就可以成为一个分布式的多层系统。
技术有点老,但也很成熟,实用于企业内部系统应用。
RSS订阅






收 藏
推 荐