接口定义
实体定义,注意需要序列化
using System;
namespace Interface
{
[Serializable]
public class DataEntity
{
public string ReportName { get; set; }
public string ReportId { get; set; }
}
}
接口定义
namespace Interface
{
public interface IDataCenter
{
string Query();
DataEntity QueryData();
}
}
服务端
实现类
using System;
using Interface;
namespace Server
{
class DataCenterImpl : MarshalByRefObject, Interface.IDataCenter
{
#region IDataCenter 成员
public string Query()
{
return "I'm server";
}
#endregion
#region IDataCenter 成员
public Interface.DataEntity QueryData()
{
DataEntity ret = new DataEntity();
ret.ReportId = Guid.NewGuid().ToString();
ret.ReportName = "GSM报告";
return ret;
}
#endregion
}
}
主方法
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("我是服务端");
TcpServerChannel chan = new TcpServerChannel(9999);
ChannelServices.RegisterChannel(chan, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataCenterImpl), "server", WellKnownObjectMode.SingleCall);
Console.WriteLine("按q退出");
while (Console.ReadKey().KeyChar != 'q')
{
Thread.Sleep(10);
}
}
}
}
客户端
using System;
using System.IO;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Interface;
namespace Client
{
class Program
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure(new FileInfo("Log4net.xml"));
TcpClientChannel chan = new TcpClientChannel();
ChannelServices.RegisterChannel(chan, false);
IDataCenter idc = (IDataCenter)Activator.GetObject(typeof(IDataCenter), "tcp://localhost:9999/server", null);
string value = idc.Query();
logger.Info(value);
var entity = idc.QueryData();
Console.WriteLine(entity.ReportId + "/t" + entity.ReportName);
Console.Read();
}
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/20967.html