部署 服务器端Remoting程序有三种方法,第一是利用控制台程序;第二是利用Windows服务;第三是利用IIS服务。今天我们将介绍第二种。
首先我们要建立一个WindowsService程序,在Program.cs写入如下代码:
首先我们要建立一个WindowsService程序,在Program.cs写入如下代码:
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using General;
namespace WindowsService2
{
public class RemotingService : System.ServiceProcess.ServiceBase
{
private static EventLog evt = new EventLog("Application");
public static String SVC_NAME = ".NET Remoting Sample Service";
public RemotingService()
{
this.ServiceName = SVC_NAME;
}
static void Main()
{
//注册服务名
evt.Source = SVC_NAME;
//写入日志
evt.WriteEntry("Remoting Service intializing");
//启动服务
ServiceBase.Run(new RemotingService());
}
protected override void OnStart(string[] args)
{
evt.WriteEntry("Remoting Service started");
//加载配置文件
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
);
//给服务器类赋值
HelloServer.Str = "meinv";
}
protected override void OnStop()
{
evt.WriteEntry("Remoting Service stopped");
}
}
}
在建立一个叫MyProjectInstaller.cs的类,他是一个自定义安装类,代码如下:using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using General;
namespace WindowsService2
{
public class RemotingService : System.ServiceProcess.ServiceBase
{
private static EventLog evt = new EventLog("Application");
public static String SVC_NAME = ".NET Remoting Sample Service";
public RemotingService()
{
this.ServiceName = SVC_NAME;
}
static void Main()
{
//注册服务名
evt.Source = SVC_NAME;
//写入日志
evt.WriteEntry("Remoting Service intializing");
//启动服务
ServiceBase.Run(new RemotingService());
}
protected override void OnStart(string[] args)
{
evt.WriteEntry("Remoting Service started");
//加载配置文件
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
);
//给服务器类赋值
HelloServer.Str = "meinv";
}
protected override void OnStop()
{
evt.WriteEntry("Remoting Service stopped");
}
}
}
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
namespace WindowsService2
{
[RunInstallerAttribute(true)]
public class MyProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private Service1 service11;
private RemotingService remotingService1;
private ServiceProcessInstaller processInstaller;
public MyProjectInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
//设置级别
processInstaller.Account = ServiceAccount.LocalSystem;
//服务启动方式
serviceInstaller.StartType = ServiceStartMode.Automatic;
//服务名字
serviceInstaller.ServiceName = RemotingService.SVC_NAME;
//添加制定的安装程序
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
private void InitializeComponent()
{
this.service11 = new WindowsService2.Service1();
this.remotingService1 = new WindowsService2.RemotingService();
//
// service11
//
this.service11.ExitCode = 0;
this.service11.ServiceName = "Service1";
//
// remotingService1
//
this.remotingService1.ExitCode = 0;
this.remotingService1.ServiceName = ".NET Remoting Sample Service";
}
}
}
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
namespace WindowsService2
{
[RunInstallerAttribute(true)]
public class MyProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private Service1 service11;
private RemotingService remotingService1;
private ServiceProcessInstaller processInstaller;
public MyProjectInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
//设置级别
processInstaller.Account = ServiceAccount.LocalSystem;
//服务启动方式
serviceInstaller.StartType = ServiceStartMode.Automatic;
//服务名字
serviceInstaller.ServiceName = RemotingService.SVC_NAME;
//添加制定的安装程序
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
private void InitializeComponent()
{
this.service11 = new WindowsService2.Service1();
this.remotingService1 = new WindowsService2.RemotingService();
//
// service11
//
this.service11.ExitCode = 0;
this.service11.ServiceName = "Service1";
//
// remotingService1
//
this.remotingService1.ExitCode = 0;
this.remotingService1.ServiceName = ".NET Remoting Sample Service";
}
}
}
编译程序。
之后打开Microsoft .NET Framework SDK v2.0的SDK命令提示,
在命令提示里输入installutil,将出现installutil的帮助文件。在SDk里输入你建立WindowsService的路径,安装你所建立的服务
将会出现以下的结果提示:
在此之后在Windows的服务里会多出一个名字叫“.NET Remoting Sample Service”的服务,启动它。查看Windows服务管理器就会多出个WindowsService2.exe的进程。这样我们就把服务器段给建立部署好了。运行前一次我们建立的客户端代码就OK了:
完整代码下载