Remoting dll 主要代码(C#):
namespace RemotingServer
{
public class Class1 : System.MarshalByRefObject
{
public Class1()
{ }
public String sayHello()
{ return "Hello world!"; }
}
}
Windows服务工程的主要代码(C#):
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;
public Service1()
{
InitializeComponent();
}
// 进程的主入口点
static void
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
TcpChannel MyChannel = new TcpChannel(8881);
ChannelServices.RegisterChannel(MyChannel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.Class1),"Class1",WellKnownObjectMode.SingleCall);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{ components.Dispose(); }
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{ }
protected override void OnStop()
{ }
}
服务安装文件:
ProjectInstaller.cs
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
[RunInstallerAttribute(true)]
public class ProjectInstaller: Installer{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public ProjectInstaller(){
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "RemotingServer Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
调用端:
C#建的 所有工程都能够调用成功,主要代码如下;
try
{
RemotingServer.Class1 b = (RemotingServer.Class1) Activator.GetObject(typeof(RemotingServer.Class1),"http://172.21.14.61:8883/Class1");
string s = b.sayHello();
}
catch(ArgumentNullException ae) //type 或 url 为空引用
{ }
catch(RemotingException re) //type 未通过引用或某个接口封送处理
{ }

浙公网安备 33010602011771号