这节谈谈.Net Remoting比较重要的部分,数据传递,这次我们在上一节的基础上,对数据契约做出如下修改
public interface IMyService
{
//1.返回一个字符串给客户端
string SayHello(string name);
//2.返回一个datatable给客户端
DataTable GetTable();
//3.返回一个PersonData的实例对象给客户端
PersonData GetPerson();
//4.从客户端传datatable到服务器端
void ClientToServer(DataTable dt);
//5.实例的集合
IList<PersonData> GetPersons();
}
{
//1.返回一个字符串给客户端
string SayHello(string name);
//2.返回一个datatable给客户端
DataTable GetTable();
//3.返回一个PersonData的实例对象给客户端
PersonData GetPerson();
//4.从客户端传datatable到服务器端
void ClientToServer(DataTable dt);
//5.实例的集合
IList<PersonData> GetPersons();
}
大家可以看到,从服务端可以返回字符串,数据表,实例对象,以及实例的集合,并且4有一个方法是从客户端传入一个数据表到服务端,下面我们来看看服务端的简单实现
public class MyServiceImpl : MarshalByRefObject,ShareDLL.IMyService
{
public string SayHello(string name)
{
return string.Format(@" Message from Server :"+ name);
}
//返回一个datatable给客户端
public DataTable GetTable()
{
//这个地方模拟一个datatable,实际可以从数据库中获得
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
DataRow row = dt.NewRow();
row["Id"] = Guid.NewGuid().ToString().ToUpper();
row["Name"] = "Tom";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = Guid.NewGuid().ToString().ToUpper();
row["Name"] = "Jack";
dt.Rows.Add(row);
return dt;
}
//返回一个PersonData的实例对象给客户端
public ShareDLL.PersonData GetPerson()
{
return new ShareDLL.PersonData("Mike", 23);
}
//从客户端传datatable到服务器端
public void ClientToServer(DataTable dt)
{
Console.WriteLine("datatable from client:" + dt.Rows[0][0].ToString());
}
//实例的集合
public IList<ShareDLL.PersonData> GetPersons()
{
IList<ShareDLL.PersonData> list=new List<ShareDLL.PersonData>(2);
list.Add(new ShareDLL.PersonData("Mike", 23));
list.Add(new ShareDLL.PersonData("Tom", 25));
return list;
}
}
{
public string SayHello(string name)
{
return string.Format(@" Message from Server :"+ name);
}
//返回一个datatable给客户端
public DataTable GetTable()
{
//这个地方模拟一个datatable,实际可以从数据库中获得
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
DataRow row = dt.NewRow();
row["Id"] = Guid.NewGuid().ToString().ToUpper();
row["Name"] = "Tom";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = Guid.NewGuid().ToString().ToUpper();
row["Name"] = "Jack";
dt.Rows.Add(row);
return dt;
}
//返回一个PersonData的实例对象给客户端
public ShareDLL.PersonData GetPerson()
{
return new ShareDLL.PersonData("Mike", 23);
}
//从客户端传datatable到服务器端
public void ClientToServer(DataTable dt)
{
Console.WriteLine("datatable from client:" + dt.Rows[0][0].ToString());
}
//实例的集合
public IList<ShareDLL.PersonData> GetPersons()
{
IList<ShareDLL.PersonData> list=new List<ShareDLL.PersonData>(2);
list.Add(new ShareDLL.PersonData("Mike", 23));
list.Add(new ShareDLL.PersonData("Tom", 25));
return list;
}
}
实现后,我们开启服务端应用程序:
RemotingConfiguration.Configure("Server.exe.config",false);
Console.WriteLine("Server is Running...");
Console.Read();
Console.WriteLine("Server is Running...");
Console.Read();
接着我们开启客户端调用程序:
RemotingConfiguration.Configure("Client.exe.config", false);
WellKnownClientTypeEntry[] types = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
if (types.Length > 0)
{
ShareDLL.IMyService service = (ShareDLL.IMyService)Activator.GetObject(types[0].ObjectType,
types[0].ObjectUrl);
try
{
//从服务端返回字符串
string returnString = service.SayHello("mike");
Console.WriteLine(returnString);
//从服务端返回datatable
DataTable dt = service.GetTable();
Console.WriteLine("DataTable from Server:" + dt.Rows[0][0].ToString());
//从客户端传datatable到服务器端
service.ClientToServer(dt);
//从服务端返回PersonData(实例对象)
ShareDLL.PersonData person = service.GetPerson();
Console.WriteLine("PersonData instance from server:" + person.ToString());
//实例的集合
IList<ShareDLL.PersonData> persons = service.GetPersons();
Console.WriteLine("list from server:"+persons.Count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
Console.WriteLine("no service reigstered");
Console.Read();
WellKnownClientTypeEntry[] types = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
if (types.Length > 0)
{
ShareDLL.IMyService service = (ShareDLL.IMyService)Activator.GetObject(types[0].ObjectType,
types[0].ObjectUrl);
try
{
//从服务端返回字符串
string returnString = service.SayHello("mike");
Console.WriteLine(returnString);
//从服务端返回datatable
DataTable dt = service.GetTable();
Console.WriteLine("DataTable from Server:" + dt.Rows[0][0].ToString());
//从客户端传datatable到服务器端
service.ClientToServer(dt);
//从服务端返回PersonData(实例对象)
ShareDLL.PersonData person = service.GetPerson();
Console.WriteLine("PersonData instance from server:" + person.ToString());
//实例的集合
IList<ShareDLL.PersonData> persons = service.GetPersons();
Console.WriteLine("list from server:"+persons.Count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
Console.WriteLine("no service reigstered");
Console.Read();
下面我们分别看看服务调用的结果
客户端调用结果
服务端调用后的结果:
全部实例代码下载:https://files.cnblogs.com/jackhuclan/RemotingConfig.rar