基于WCF通信的C/S系统快速开发框架的部分架构设计
作者:C/S框架网  发布日期:2018-12-14 21:39:24
  基于WCF通信的C/S系统快速开发框架的部分架构设计


 一、WCF架构通信流程图 (WCF Communication Flow)





 二、WCF层包含两个逻辑层:分别是WCF服务层,WCF客户端代理层。

贴图图片


 WCF客户端代理层封装在CSFrameworkV5.WebRef.dll


 三、WCF服务端代码参考

 WCF接口和WCF合约代码列表。

贴图图片

 WCF接口部分代码:

C# Code:

namespace CSFrameworkV5.WCFContract
{
   [ServiceContract(Namespace = "http://www.csframework.com")]
   public interface ICommonService
   {
      [OperationContract]
      byte[] GetAttachedFiles(byte[] loginTicket, string docID);
      
      [OperationContract]
      byte[] GetBackupHistory(byte[] loginTicket, int topList);
      
      [OperationContract]
      byte[] GetBusinessTables(byte[] loginTicket);
      
      //.....省略很多接口的代码.....      
   }
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



 WCF合约部分代码:


C# Code:


namespace CSFrameworkV5.WCFContract.WCF_Class
{
   [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
   [WCF_ExceptionBehaviour(typeof(WCF_ExceptionHandler))]
   public class CommonService : ICommonService
   {
      
      public byte[] GetAttachedFiles(byte[] loginTicket, string docID)
      {
         try
         {
            Loginer loginer = WebSecurity.ValidateLoginer(loginTicket);
            
            DataTable data = new dalAttachFile(loginer).GetData(docID);
            return ZipTools.CompressionDataSet(ServerLibrary.TableToDataSet(data));
         }
         catch (Exception ex)
         {
            throw new FaultException(ex.Message);
         }
      }
      
      public byte[] GetBackupHistory(byte[] loginTicket, int topList)
      {
         try
         {
            Loginer loginer = WebSecurity.ValidateLoginer(loginTicket);
            DataTable data = new dalCommon(null).GetBackupHistory(topList);
            return ZipTools.CompressionDataSet(ServerLibrary.TableToDataSet(data));
         }
         catch (Exception ex)
         {
            throw new FaultException(ex.Message);
         }
      }
      
      
      public byte[] GetBusinessTables(byte[] loginTicket)
      {
         try
         {
            Loginer loginer = WebSecurity.ValidateLoginer(loginTicket);
            
            DataTable data = new dalCommon(loginer).GetBusinessTables();
            return ZipTools.CompressionDataSet(ServerLibrary.TableToDataSet(data));
         }
         catch (Exception ex)
         {
            throw new FaultException(ex.Message);
         }
      }      //.....省略很多接口的代码.....      
   }
   
}


//来源:C/S框架网(www.csframework.com) QQ:23404761


 四、ServiceHost类参考:


贴图图片


 启动WCF服务器


C# Code:

/// <summary>
/// 启动WCF服务
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
   //this.WriteLog("准备启动服务...");
   
   Debugger.Launch(); //运行调试程序!!!必须
   
   LogUserOperate.Write("准备启动服务...");
   ServiceHost host = null;
   
   //获取WCF服务清单s
   List<Type> list = WCFServiceProvider.GetWCFServiceType();
   foreach (Type type in list)
   {
      //this.WriteLog("准备启动服务..." + type.FullName);
      
      try
      {
         //根据服务名称从app.config文件取URL地址
         Uri address = new Uri(ConfigurationManager.AppSettings[type.Name]);
         host = new ServiceHost(type, address);
         
         Binding myBinding = WCFServiceProvider.CreateBinding(BindingType.NetTcpBinding);
         host.AddServiceEndpoint(WCFServiceProvider.GetWCFInterfaceType(type), myBinding, address);
         host.UnknownMessageReceived += Host_UnknownMessageReceived;
         host.Closing += Host_Closing;
         host.Faulted += Host_Faulted;
         
         host.CloseTimeout = new TimeSpan(24, 0, 0);
         host.Open();
         
         
         //添加到服务列表
         _HostList.Add(host);
         
         LogUserOperate.Write("启动" + type.Name + "服务成功:" + address.AbsoluteUri);
      }
      catch (Exception ex)
      {
         LogUserOperate.Write("启动" + type.Name + "服务失败!" + ex.Message);
         if (host != null) host.Abort();
      }
      
      host = null;//初始null,准备下一次实例化
   }
}

//来源:C/S框架网(www.csframework.com) QQ:23404761


 五、客户端调用WCF接口

1.客户端使用透明代理机制创建WCF实例,然后调用接口方法存取数据。
2.开发框架预设使用net.tcp协议
3.调用WCF接口需要传入用户登陆信息,将对象转换为byte数组格式,提高系统安全性。

C# Code:

/// <summary>
/// 测试客户端访问WCF接口
/// </summary>
private void TestCommonService()
{
   string uri = "net.tcp://localhost:8585/CommonService";
   
   //创建透明代理
   ICommonService svc = WCFInvokeContext.CreateWCFService<ICommonService>(uri, BindingType.NetTcpBinding);
   try
   {
      (svc as ICommunicationObject).Open();//打开WCF通道
      
      //模拟用户登陆信息
      byte[] loginTicket = new byte[] { 22, 141, 34, 123, 55, 2, 23, 73, 22 };
      
      //调用WCF接口
      byte[] bs = svc.GetAttachedFiles(loginTicket, "PO201805130002");
      
      //将byte数组转换为DataSet
      DataSet ds = ZipTools.DecompressionDataSet(bs);
   }
   finally
   {
      (svc as ICommunicationObject).Close();//关闭通道
   }
}

//来源:C/S框架网(www.csframework.com) QQ:23404761


 开发框架代码参考:





贴图图片
上一篇 下一篇