我分别做了WebService和Remoting放在外网上进行测试,结果发现WebService的速度明显比Remoting慢许多,同样读取1000条数据,Remoting只需100毫秒左右(为什么外网的速度比本机的还快,本机要500毫秒左右?),所以决定使用Remoting来做数据传输。

 

首先,定义一个Model,这使用的是Sqlserver中的示例库Northwind

 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

 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中加入以下代码:

[] 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);
                }
            }
        }

 

好了,服务的安装这里就不再说了,接下来写客户端程序:

 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);
        }
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-07-25
  • 2022-01-12
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2021-05-28
  • 2022-03-05
  • 2021-05-19
  • 2021-10-27
  • 2022-12-23
  • 2021-10-11
  • 2021-10-27
相关资源
相似解决方案