.Net Remote分布式远程对象应用程序开发一例
.Net Remote分布式远程对象应用程序开发一例
RemoteObject类库工程:
//----------------------------------------------------------------------------------------
public interface IRegisterSelf
{
void RegisterSelf();
}
//定义:远程对象类
public class myobject : MarshalByRefObject, IRegisterSelf
{
public myobject()
{
}
public int add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
#region IRegisterSelf 成员
public void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(myobject), "RemoteObject.myobject", WellKnownObjectMode.Singleton);
}
#endregion
}
public class DbBaseClass : MarshalByRefObject, IRegisterSelf
{
public DbBaseClass()
{
}
private DbConnectionWrapper m_ConnWrap=null;
public DbConnectionWrapper ConnWrap
{
get
{
if (m_ConnWrap == null)
{
Net.Utility.Builder.ConnectionStringOleDBAccess dbaccessBuilder = new Net.Utility.Builder.ConnectionStringOleDBAccess();
dbaccessBuilder.DataSource = Application.StartupPath + "\\user.mdb";
string connstr=dbaccessBuilder.CreateConnectionString();
m_ConnWrap = DbConnectionWrapperFactory.GetDbConnectionWrapper(DataProviderType.Access,connstr);
}
return m_ConnWrap;
}
}
#region IRegisterSelf 成员
public virtual void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DbBaseClass), "RemoteObject.DbBaseClass", WellKnownObjectMode.Singleton);
}
#endregion
}
public class UserClass:DbBaseClass
{
public UserClass()
{
}
public List<string> GetUserName()
{
List<string> list_str=new List<string>();
if (this.ConnWrap != null)
{
string x = "select caption from SC_QSDM";
DataTable dt=this.ConnWrap.ExecuteDataTable(x);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
list_str.Add(dt.Rows[i]["caption"].ToString());
}
}
if (dt != null)
{
dt.Dispose();
dt = null;
}
}
return list_str;
}
public DataTable GetUserNameReturnDataTable()
{
DataTable dt = null;
if (this.ConnWrap != null)
{
string x = "select caption from SC_QSDM";
dt = this.ConnWrap.ExecuteDataTable(x);
}
return dt;
}
public override void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(UserClass), "RemoteObject.UserClass", WellKnownObjectMode.Singleton);
}
}
RemoteServer控制台服务器端项目:
//----------------------------------------------------------------------------------------
class Program
{
static void Main(string[] args)
{
try
{
//System.Runtime.Remoting.RemotingConfiguration.Configure("RemoteServer.exe.config", true);
Console.WriteLine("完成1");
//远程对象并自注册
RemoteObject.myobject myobj = new RemoteObject.myobject();
myobj.RegisterSelf();
UserClass userobj = new UserClass();
userobj.RegisterSelf();
//
//注册通道
TcpChannel ch1 = new TcpChannel(9999);
ChannelServices.RegisterChannel(ch1,false);
Console.WriteLine("完成2");
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
Console.ReadLine();
}
}
app.config文件内容:(不过测试代码中没有使用本配置参数)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<!--
<wellknown typeName="RemoteObject.myobject,RemoteObject" assembleName="RemoteObject.myobject" objecturi="RemoteObject.myobject" mode="Singleton"/>
-->
<wellknown type="RemoteObject.myobject,RemoteObject" objecturi="RemoteObject.myobject" mode="Singleton" />
</service>
<channels>
<chanel ref="tcp" port="9999"></chanel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
RemoteClient控制台客户端项目
//----------------------------------------------------------------------------------------
namespace RemoteClient
{
class Program
{
static void Main(string[] args)
{
string serviceurl = "";// System.Configuration.ConfigurationSettings.AppSettings["serviceurl"];
serviceurl=System.Configuration.ConfigurationManager.AppSettings["serviceurl"];
Console.WriteLine(serviceurl);
//
string user_url = System.Configuration.ConfigurationManager.AppSettings["user_url"];
Console.WriteLine(user_url);
//Console.ReadLine();
//获取远程对象 (服务器端激活)
myobject myobj = null;
UserClass userobj = null;
try
{
myobj = (myobject)Activator.GetObject(typeof(myobject), serviceurl);
userobj = (UserClass)Activator.GetObject(typeof(UserClass), user_url);
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
try
{
//获取远程对象 (客户端激活)
//app = (myobject)Activator.CreateInstance(typeof(myobject),null, serviceurl);
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
//调用远程对象方法
if (myobj != null)
{
try
{
Console.WriteLine("1+2=" + myobj.add(1, 2).ToString());
Console.WriteLine("1-2=" + myobj.Sub(1, 2).ToString());
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
}
if (userobj != null)
{
try
{
//方法一
Console.WriteLine("方法一");
List<string> list_str=userobj.GetUserName();
for(int i=0;i<list_str.Count;i++)
{
Console.WriteLine(list_str[i]);
}
//方法二
Console.WriteLine("方法二");
DataTable dt=userobj.GetUserNameReturnDataTable();
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows[i][0].ToString());
}
}
if (dt != null)
{
dt.Dispose();
dt = null;
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
}
Console.ReadLine();
}
}
}
app.config配置文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="serviceurl" value ="tcp://hsg:9999/RemoteObject.myobject"/>
<add key ="user_url" value="tcp://hsg:9999/RemoteObject.UserClass"/>
</appSettings>
</configuration>
--------------the end-------------------------
RemoteObject类库工程:
//----------------------------------------------------------------------------------------
public interface IRegisterSelf
{
void RegisterSelf();
}
//定义:远程对象类
public class myobject : MarshalByRefObject, IRegisterSelf
{
public myobject()
{
}
public int add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
#region IRegisterSelf 成员
public void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(myobject), "RemoteObject.myobject", WellKnownObjectMode.Singleton);
}
#endregion
}
public class DbBaseClass : MarshalByRefObject, IRegisterSelf
{
public DbBaseClass()
{
}
private DbConnectionWrapper m_ConnWrap=null;
public DbConnectionWrapper ConnWrap
{
get
{
if (m_ConnWrap == null)
{
Net.Utility.Builder.ConnectionStringOleDBAccess dbaccessBuilder = new Net.Utility.Builder.ConnectionStringOleDBAccess();
dbaccessBuilder.DataSource = Application.StartupPath + "\\user.mdb";
string connstr=dbaccessBuilder.CreateConnectionString();
m_ConnWrap = DbConnectionWrapperFactory.GetDbConnectionWrapper(DataProviderType.Access,connstr);
}
return m_ConnWrap;
}
}
#region IRegisterSelf 成员
public virtual void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DbBaseClass), "RemoteObject.DbBaseClass", WellKnownObjectMode.Singleton);
}
#endregion
}
public class UserClass:DbBaseClass
{
public UserClass()
{
}
public List<string> GetUserName()
{
List<string> list_str=new List<string>();
if (this.ConnWrap != null)
{
string x = "select caption from SC_QSDM";
DataTable dt=this.ConnWrap.ExecuteDataTable(x);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
list_str.Add(dt.Rows[i]["caption"].ToString());
}
}
if (dt != null)
{
dt.Dispose();
dt = null;
}
}
return list_str;
}
public DataTable GetUserNameReturnDataTable()
{
DataTable dt = null;
if (this.ConnWrap != null)
{
string x = "select caption from SC_QSDM";
dt = this.ConnWrap.ExecuteDataTable(x);
}
return dt;
}
public override void RegisterSelf()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(UserClass), "RemoteObject.UserClass", WellKnownObjectMode.Singleton);
}
}
RemoteServer控制台服务器端项目:
//----------------------------------------------------------------------------------------
class Program
{
static void Main(string[] args)
{
try
{
//System.Runtime.Remoting.RemotingConfiguration.Configure("RemoteServer.exe.config", true);
Console.WriteLine("完成1");
//远程对象并自注册
RemoteObject.myobject myobj = new RemoteObject.myobject();
myobj.RegisterSelf();
UserClass userobj = new UserClass();
userobj.RegisterSelf();
//
//注册通道
TcpChannel ch1 = new TcpChannel(9999);
ChannelServices.RegisterChannel(ch1,false);
Console.WriteLine("完成2");
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
Console.ReadLine();
}
}
app.config文件内容:(不过测试代码中没有使用本配置参数)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<!--
<wellknown typeName="RemoteObject.myobject,RemoteObject" assembleName="RemoteObject.myobject" objecturi="RemoteObject.myobject" mode="Singleton"/>
-->
<wellknown type="RemoteObject.myobject,RemoteObject" objecturi="RemoteObject.myobject" mode="Singleton" />
</service>
<channels>
<chanel ref="tcp" port="9999"></chanel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
RemoteClient控制台客户端项目
//----------------------------------------------------------------------------------------
namespace RemoteClient
{
class Program
{
static void Main(string[] args)
{
string serviceurl = "";// System.Configuration.ConfigurationSettings.AppSettings["serviceurl"];
serviceurl=System.Configuration.ConfigurationManager.AppSettings["serviceurl"];
Console.WriteLine(serviceurl);
//
string user_url = System.Configuration.ConfigurationManager.AppSettings["user_url"];
Console.WriteLine(user_url);
//Console.ReadLine();
//获取远程对象 (服务器端激活)
myobject myobj = null;
UserClass userobj = null;
try
{
myobj = (myobject)Activator.GetObject(typeof(myobject), serviceurl);
userobj = (UserClass)Activator.GetObject(typeof(UserClass), user_url);
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
try
{
//获取远程对象 (客户端激活)
//app = (myobject)Activator.CreateInstance(typeof(myobject),null, serviceurl);
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
//调用远程对象方法
if (myobj != null)
{
try
{
Console.WriteLine("1+2=" + myobj.add(1, 2).ToString());
Console.WriteLine("1-2=" + myobj.Sub(1, 2).ToString());
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
}
if (userobj != null)
{
try
{
//方法一
Console.WriteLine("方法一");
List<string> list_str=userobj.GetUserName();
for(int i=0;i<list_str.Count;i++)
{
Console.WriteLine(list_str[i]);
}
//方法二
Console.WriteLine("方法二");
DataTable dt=userobj.GetUserNameReturnDataTable();
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows[i][0].ToString());
}
}
if (dt != null)
{
dt.Dispose();
dt = null;
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
}
Console.ReadLine();
}
}
}
app.config配置文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="serviceurl" value ="tcp://hsg:9999/RemoteObject.myobject"/>
<add key ="user_url" value="tcp://hsg:9999/RemoteObject.UserClass"/>
</appSettings>
</configuration>
--------------the end-------------------------