Distributed Remoting example

Source: Internet
Author: User

I tested WebService and Remoting on the Internet separately, and found that WebService is much slower than Remoting, and 1000 pieces of data are also read, remoting takes about 100 milliseconds (why is the Internet faster than the local machine, and the local machine requires about 500 milliseconds ?), Therefore, we decided to use Remoting for data transmission.

 

First, define a Model, which uses the example library Northwind in Sqlserver.

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>
{
}
}

 

Write a DAL

Code
Using System;
Using FaibClass. Data;
Using FaibClass. Data. Configuration;

Namespace FBS. Chance. DAL
{
Public class Test: externalbyrefobject
{
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;
}
}
}

 

Now, write a Windows Service Program, and add the following code to OnStart and OnStop:

Code
Protected override void OnStart (string [] args)
{
Try
{
TcpChannel channel = new tcpchannels (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 );
}
}
}

 

Well, we won't talk about the installation of the service here. Next we will write the client program:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.