//Share.cs
namespace Microshaoft
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Text;
public static class RemotingHelper
{
public static void StartRemoting
(
Type RemotingType
, string Url
, int Port
)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary ht = new Hashtable();
ht["port"] = Port;
TcpChannel tc = new TcpChannel(ht, null, provider);
ChannelServices.RegisterChannel(tc, false);
RemotingConfiguration.RegisterWellKnownServiceType(RemotingType, Url, WellKnownObjectMode.Singleton);
Console.WriteLine("Remoting Object Started ...");
}
public static void StartRemoting<T>
(
string Url
, int Port
)
{
StartRemoting(typeof(T), Url, Port);
}
public static T GetRemotingLocalClientProxyObject<T>
(
string Url
)
{
return (T) Activator.GetObject
(
typeof(T)
, Url
//, "tcp://127.0.0.1:8080/queueUrl"
);
}
}
}
namespace Microshaoft.RemotingObjects.Share
{
using System;
[Serializable]
public class BillingResponse
{
private int _Result;
public int Result
{
get
{
return _Result;
}
set
{
_Result = value;
}
}
private string _Description;
public string Description
{
get
{
return _Description;
}
set
{
_Description = value;
}
}
}
}
//client.cs
namespace ConsoleApplication
{
using System;
using Microshaoft;
using Microshaoft.RemotingObjects;
using Microshaoft.RemotingObjects.Share;
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
//[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
BillingServiceRemoting x = RemotingHelper.GetRemotingLocalClientProxyObject<BillingServiceRemoting>("tcp://127.0.0.1:9090/testUrl");
BillingResponse y = x.Billing("1", "2", "3", "4", "5");
//x.HelloWorld("asdas");
Console.WriteLine(x.HelloWorld("asdas"));
Console.WriteLine(y.Description);
Console.WriteLine(Environment.Version.ToString());
}
}
}
namespace Microshaoft.RemotingObjects
{
using System;
using Microshaoft.RemotingObjects.Share;
public interface BillingServiceRemoting
{
BillingResponse Billing
(
string UserID
, string ProductID
, string BizModel
, string IP
, string TimeStamp
);
string HelloWorld(string s);
}
}
//Server.cs
namespace Microshaoft.RemotingObjects
{
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using Microshaoft.RemotingObjects.Share;
using Microshaoft;
class BillingServiceRemoting : MarshalByRefObject
{
public static readonly string ServiceName = "Billing Remoting Service";
public string HelloWorld
(
string User
)
{
Console.WriteLine("Helo");
return "Hello " + User;
}
public BillingResponse Billing
(
string UserID
, string ProductID
, string BizModel
, string IP
, string TimeStamp
)
{
BillingResponse response = new BillingResponse();
response.Result = 0; //表示成功
response.Description = "成功了耶";
Console.WriteLine("bill");
return response;
}
}
}
namespace Microshaoft
{
using System;
using System.ServiceProcess;
using System.ComponentModel;
using System.Security.Principal;
using System.Configuration.Install;
using Microshaoft.RemotingObjects;
public class SimpleService : ServiceBase //继承于 ServiceBase
{
public static readonly string serviceName = "Hello-World Service1";
public static void Main(string[] args)
{
SimpleService x = new SimpleService();
int l = 0;
if (args != null)
{
l = args.Length;
}
if (l > 0) //有参数时以 console 方式运行
{
Console.WriteLine("Run as Console");
x.OnStart(null);
Console.ReadLine();
}
else
//intallutil 成服务后
//即: 无参数时,以 Service 方式运行
{
Console.WriteLine("Run as Service");
ServiceBase.Run(x);
}
}
public SimpleService()
{
CanPauseAndContinue = true;
ServiceName = SimpleService.serviceName;
}
protected override void OnStart(string[] args)
{
Console.WriteLine(".Net Version: {0}", Environment.Version.ToString());
Console.WriteLine("Current Identity: {0}", WindowsIdentity.GetCurrent().Name);
Console.WriteLine("{0} started,at {1}", SimpleService.serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ss"));
RemotingHelper.StartRemoting<BillingServiceRemoting>
(
"testurl"
, 9090
);
//log 写入 windows 应用程序日志
EventLog.WriteEntry(string.Format(".Net Version: {0}", Environment.Version.ToString()));
EventLog.WriteEntry(string.Format("Current Identity: {0}", WindowsIdentity.GetCurrent().Name));
EventLog.WriteEntry(string.Format("{0} started, at {1}", SimpleService.serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ss")));
//在这里写你的程序
}
/// protected override void OnStop()
/// {
/// EventLog.WriteEntry("Hello-World Service stopped");
/// }
///
/// protected override void OnPause()
/// {
/// EventLog.WriteEntry("Hello-World Service paused");
/// }
///
/// protected override void OnContinue()
/// {
/// EventLog.WriteEntry("Hello-World Service continued");
/// }
}
//以下就是比普通应用程序多出的 ProjectInstaller
[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.Manual;
serviceInstaller.ServiceName = SimpleService.serviceName;
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}