CSFramework平台的WCF体系架构 (Win服务承载与透明代理)
作者:C/S框架网  发布日期:2017-03-01 20:58:59
  CSFramework平台的WCF体系架构 (Win服务承载与透明代理)


贴图图片

服务端WCF服务使用Windows服务承载。

C# Code:

protected override void OnStart(string[] args)
{
   //this.WriteLog("准备启动服务...");
   
   LogUserOperate.Write("准备启动服务...");
   ServiceHost host = null;
   
   //获取WCF服务清单
   List<Type> list = this.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 = CreateBinding(BindingType.NetTcpBinding);
         host.AddServiceEndpoint(GetWCFInterfaceType(type), myBinding, address);
         host.Open();
         
         //添加到服务列表
         _HostList.Add(host);
         
         LogUserOperate.Write("启动" type.Name "服务成功:" address.AbsoluteUri);
      }
      catch (CommunicationException)
      {
         LogUserOperate.Write("启动" type.Name "服务失败!");
         if (host != null) host.Abort();
      }
      
      host = null;//初始null,准备下一次实例化
   }
}

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


客户端创建WCF服务透明代理。

C# Code:

/// <summary>
/// 创建公共服务的WCF客户端
/// </summary>
/// <returns></returns>
internal static ICommonService CreateCommonService()
{
   string serviceName = WCFs.CommonService.ToString();//WCF服务实例
   string uri = WCFConfig.GetWinServiceUrl(serviceName);//地址
   ICommonService svc = WCFInvokeContext.CreateWCFService<ICommonService>(uri, _BindingType);//动态创建透明代理
   return svc;
}

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


 


上一篇 下一篇