将Remoting放到Windows Service中
我分别做了WebService和Remoting放在外网上进行测试,结果发现WebService的速度明显比Remoting慢许多,同样读取1000条数据,Remoting只需100毫秒左右(为什么外网的速度比本机的还快,本机要500毫秒左右?),所以决定使用Remoting来做数据传输。
首先,定义一个Model,这使用的是Sqlserver中的示例库Northwind
Code
using System;
using FaibClass.Data;
namespace FBS.Chance.DAL
{
[Serializable]
public class Order : IDataModel
{
private int m_OrderID;
public int OrderID
{
get { return m_OrderID; }
set { m_OrderID = value; }
}
private string m_CustomerID;
public string CustomerID
{
get { return m_CustomerID; }
set { m_CustomerID = value; }
}
private int m_EmployeeID;
public int EmployeeID
{
get { return m_EmployeeID; }
set { m_EmployeeID = value; }
}
private DateTime m_OrderDate;
public DateTime OrderDate
{
get { return m_OrderDate; }
set { m_OrderDate = value; }
}
private decimal m_Freight;
public decimal Freight
{
get { return m_Freight; }
set { m_Freight = value; }
}
private string m_ShipName;
public string ShipName
{
get { return m_ShipName; }
set { m_ShipName = value; }
}
private string m_ShipAddress;
public string ShipAddress
{
get { return m_ShipAddress; }
set { m_ShipAddress = value; }
}
}
[Serializable]
public class Orders : DataModelList<Order>
{
}
}
using System;
using FaibClass.Data;
namespace FBS.Chance.DAL
{
[Serializable]
public class Order : IDataModel
{
private int m_OrderID;
public int OrderID
{
get { return m_OrderID; }
set { m_OrderID = value; }
}
private string m_CustomerID;
public string CustomerID
{
get { return m_CustomerID; }
set { m_CustomerID = value; }
}
private int m_EmployeeID;
public int EmployeeID
{
get { return m_EmployeeID; }
set { m_EmployeeID = value; }
}
private DateTime m_OrderDate;
public DateTime OrderDate
{
get { return m_OrderDate; }
set { m_OrderDate = value; }
}
private decimal m_Freight;
public decimal Freight
{
get { return m_Freight; }
set { m_Freight = value; }
}
private string m_ShipName;
public string ShipName
{
get { return m_ShipName; }
set { m_ShipName = value; }
}
private string m_ShipAddress;
public string ShipAddress
{
get { return m_ShipAddress; }
set { m_ShipAddress = value; }
}
}
[Serializable]
public class Orders : DataModelList<Order>
{
}
}
写一个DAL
Code
using System;
using FaibClass.Data;
using FaibClass.Data.Configuration;
namespace FBS.Chance.DAL
{
public class Test : MarshalByRefObject
{
public Orders GetOrders()
{
try{
SqlServer sql = new SqlServer(new SysXmlConnectionConfig("wtcp.config").Read("//config/ConnectionString"));
Orders list = new Orders();
sql.FillModelList(list, "select * from Orders");
sql.Dispose();
return list;
}
catch (Exception exp)
{
}
return null;
}
}
}
using System;
using FaibClass.Data;
using FaibClass.Data.Configuration;
namespace FBS.Chance.DAL
{
public class Test : MarshalByRefObject
{
public Orders GetOrders()
{
try{
SqlServer sql = new SqlServer(new SysXmlConnectionConfig("wtcp.config").Read("//config/ConnectionString"));
Orders list = new Orders();
sql.FillModelList(list, "select * from Orders");
sql.Dispose();
return list;
}
catch (Exception exp)
{
}
return null;
}
}
}
现在,写一个Windows Service程序,OnStart和OnStop中加入以下代码:
Code
protected override void OnStart(string[] args)
{
try
{
TcpChannel channel = new TcpChannel(8093);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.ApplicationName = "ChanceService";
RemotingConfiguration.RegisterActivatedServiceType(typeof(Test));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected override void OnStop()
{
foreach (IChannel chan in ChannelServices.RegisteredChannels)
{
if (chan.ChannelName == "tcp")
{
TcpChannel tcpChannel = (TcpChannel)chan;
tcpChannel.StopListening(null);
ChannelServices.UnregisterChannel(tcpChannel);
}
}
}
protected override void OnStart(string[] args)
{
try
{
TcpChannel channel = new TcpChannel(8093);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.ApplicationName = "ChanceService";
RemotingConfiguration.RegisterActivatedServiceType(typeof(Test));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected override void OnStop()
{
foreach (IChannel chan in ChannelServices.RegisteredChannels)
{
if (chan.ChannelName == "tcp")
{
TcpChannel tcpChannel = (TcpChannel)chan;
tcpChannel.StopListening(null);
ChannelServices.UnregisterChannel(tcpChannel);
}
}
}
好了,服务的安装这里就不再说了,接下来写客户端程序:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using FBS.Chance.DAL;
using System.Runtime.Remoting.Activation;
using FaibClass.Data;
namespace FSB.Chance.Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Test test;
DateTime d = DateTime.Now;
string serverStr = "tcp://localhost:8093/ChanceService";
object[] attrs = { new UrlAttribute(serverStr) };
test = (Test)Activator.CreateInstance(typeof(Test), new object[] { }, attrs);
Console.WriteLine(test.GetOrders().Count);
Console.WriteLine(((TimeSpan)(DateTime.Now - d)).Milliseconds);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using FBS.Chance.DAL;
using System.Runtime.Remoting.Activation;
using FaibClass.Data;
namespace FSB.Chance.Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Test test;
DateTime d = DateTime.Now;
string serverStr = "tcp://localhost:8093/ChanceService";
object[] attrs = { new UrlAttribute(serverStr) };
test = (Test)Activator.CreateInstance(typeof(Test), new object[] { }, attrs);
Console.WriteLine(test.GetOrders().Count);
Console.WriteLine(((TimeSpan)(DateTime.Now - d)).Milliseconds);
}
}
}