相對於webservice,remoting是用tcp來傳遞資料的,更像c/s架構
下面的例子說明了如何使用remoting
功能:客戶端通過remoting查詢oracle的資料
第一部分:DBServer類
using
System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Threading;
namespace RemoteSample
{
// 客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
// 说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
// 说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
// 说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
public class DbServer : MarshalByRefObject
{
private static string ConnectionString = " Data Source=MIX01;Persist Security Info=True;User ID=user;Password=password;Unicode=True " ;
private static OracleConnection Conn;
private static object synclock = new object ();
static DbServer()
{ // Singleton
if (Conn == null )
{
lock (synclock)
{
if (Conn == null )
{
Conn = new OracleConnection();
Conn.ConnectionString = ConnectionString;
}
}
}
}
public void CloseConn()
{
if (Conn.State == ConnectionState.Open)
Conn.Close();
}
public DataSet GetDataSet( string sql, string DataSetName)
{
if (DbServer.Conn.State != ConnectionState.Open)
DbServer.Conn.Open();
DataSet dataSet = new DataSet(DataSetName);
OracleDataAdapter OraDA = new OracleDataAdapter(sql, Conn);
OraDA.Fill(dataSet);
return dataSet;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Threading;
namespace RemoteSample
{
// 客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
// 说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
// 说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
// 说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
public class DbServer : MarshalByRefObject
{
private static string ConnectionString = " Data Source=MIX01;Persist Security Info=True;User ID=user;Password=password;Unicode=True " ;
private static OracleConnection Conn;
private static object synclock = new object ();
static DbServer()
{ // Singleton
if (Conn == null )
{
lock (synclock)
{
if (Conn == null )
{
Conn = new OracleConnection();
Conn.ConnectionString = ConnectionString;
}
}
}
}
public void CloseConn()
{
if (Conn.State == ConnectionState.Open)
Conn.Close();
}
public DataSet GetDataSet( string sql, string DataSetName)
{
if (DbServer.Conn.State != ConnectionState.Open)
DbServer.Conn.Open();
DataSet dataSet = new DataSet(DataSetName);
OracleDataAdapter OraDA = new OracleDataAdapter(sql, Conn);
OraDA.Fill(dataSet);
return dataSet;
}
}
}
第二部分:服務端代碼(server)
using
System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemoteSample
{
class server
{
static int Main( string [] args)
{
// 注册通道
TcpServerChannel chan = new TcpServerChannel( 8085 );
ChannelServices.RegisterChannel(chan, true );
// 注册远程知名对象,即激活.
RemotingConfiguration.RegisterWellKnownServiceType( typeof (RemoteSample.DbServer), " DbServer " , WellKnownObjectMode.SingleCall); //ClassType,Url,Mode
// System.Console.WriteLine("RegisterServer!!.");
System.Console.WriteLine("Hit to exit!");
System.Console.ReadLine();
return 0 ;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemoteSample
{
class server
{
static int Main( string [] args)
{
// 注册通道
TcpServerChannel chan = new TcpServerChannel( 8085 );
ChannelServices.RegisterChannel(chan, true );
// 注册远程知名对象,即激活.
RemotingConfiguration.RegisterWellKnownServiceType( typeof (RemoteSample.DbServer), " DbServer " , WellKnownObjectMode.SingleCall); //ClassType,Url,Mode
// System.Console.WriteLine("RegisterServer!!.");
System.Console.WriteLine("Hit to exit!");
System.Console.ReadLine();
return 0 ;
}
}
}
第三部分:客戶端代碼(Client)
調用Remoting
private
void
button2_Click(
object
sender, EventArgs e)
{
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel( new TcpClientChannel());
string s_addr = " tcp://127.0.0.1:8085/DbServer " ; // IP:Port of Server's Remoting
RemotingConfiguration.RegisterWellKnownClientType( typeof (RemoteSample.DbServer),s_addr);
RemoteSample.DbServer _Client = new RemoteSample.DbServer();
string sql = " select * from dual where rownum<10000 " ; // Sql
string datasetname = " T_PDP " ; // Return DataSet
DataSet ds = _Client.GetDataSet(sql, datasetname);
string s;
for ( int i = 0 ; i < ds.Tables[ 0 ].Rows.Count; i ++ )
{
s = i.ToString();
s = s + " --- " + ds.Tables[ 0 ].Rows[i][ 0 ].ToString();
this .textBox1.Text += s + " \r\n " ;
}
}
{
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel( new TcpClientChannel());
string s_addr = " tcp://127.0.0.1:8085/DbServer " ; // IP:Port of Server's Remoting
RemotingConfiguration.RegisterWellKnownClientType( typeof (RemoteSample.DbServer),s_addr);
RemoteSample.DbServer _Client = new RemoteSample.DbServer();
string sql = " select * from dual where rownum<10000 " ; // Sql
string datasetname = " T_PDP " ; // Return DataSet
DataSet ds = _Client.GetDataSet(sql, datasetname);
string s;
for ( int i = 0 ; i < ds.Tables[ 0 ].Rows.Count; i ++ )
{
s = i.ToString();
s = s + " --- " + ds.Tables[ 0 ].Rows[i][ 0 ].ToString();
this .textBox1.Text += s + " \r\n " ;
}
}
以上只是實現了最簡單的功能,基本用法如上,可以擴展更豐富的內容.
摘自:http://www.cnblogs.com/zzyyll2/articles/982392.html
转载于:https://blog.51cto.com/1906754/558708