WCF透明代理類,動態調用,支持async/await

来源:http://www.cnblogs.com/felixnet/archive/2017/09/05/wcf_client_transparent_proxy_channelfactory.html
-Advertisement-
Play Games

我們希望WCF客戶端調用採用透明代理方式,不用添加服務引用,也不用Invoke的方式,通過ChannelFactory<>動態產生通道,實現服務介面進行調用,並且支持async/await,當然也不用在Config中配置serviceModel。 服務端代碼: 代理類 動態創建服務對象,Channe ...


我們希望WCF客戶端調用採用透明代理方式,不用添加服務引用,也不用Invoke的方式,通過ChannelFactory<>動態產生通道,實現服務介面進行調用,並且支持async/await,當然也不用在Config中配置serviceModel。

服務端代碼:

[ServiceContract]
public interface IGameService
{
    [OperationContract]
    Task DoWorkAsync(string arg);

    [OperationContract]
    void DoWork(string arg);
}

public class GameService : IGameService
{
    public async Task<string> DoWorkAsync(string arg)
    {
        return await Task.FromResult($"Hello {arg}, I am the GameService.");
    }
    public string DoWork(string arg)
    {
        return $"Hello {arg}, I am the GameService.";
    }
}

[ServiceContract]
public interface IPlayerService
{
    [OperationContract]
    Task<string> DoWorkAsync(string arg);

    [OperationContract]
    string DoWork(string arg);
}

public class PlayerService : IPlayerService
{
    public async Task<string> DoWorkAsync(string arg)
    {
        return await Task.FromResult($"Hello {arg}, I am the PlayerService.");
    }
    public async string DoWork(string arg)
    {
        return $"Hello {arg}, I am the PlayerService.";
    }
}

 

代理類

動態創建服務對象,ChannelFactory<T>的運用,一個抽象類

namespace Wettery.Infrastructure.Wcf
{
    public enum WcfBindingType
    {
        BasicHttpBinding,
        NetNamedPipeBinding,
        NetPeerTcpBinding,
        NetTcpBinding,
        WebHttpBinding,
        WSDualHttpBinding,
        WSFederationHttpBinding,
        WSHttpBinding
    }

    public abstract class WcfChannelClient<TChannel> : IDisposable
    {
        public abstract string ServiceUrl { get; }

        private Binding _binding;
        public virtual Binding Binding
        {
            get
            {
                if (_binding == null)
                    _binding = CreateBinding(WcfBindingType.NetTcpBinding);

                return _binding;
            }
        }

        protected TChannel _channel;
        public TChannel Channel
        {
            get { return _channel; }
        }
        protected IClientChannel ClientChannel
        {
            get { return (IClientChannel)_channel; }
        }

        public WcfChannelClient()
        {
            if (string.IsNullOrEmpty(this.ServiceUrl)) throw new NotSupportedException("ServiceUrl is not overridden by derived classes.");
            var chanFactory = new ChannelFactory<TChannel>(this.Binding, this.ServiceUrl);
            _channel = chanFactory.CreateChannel();
            this.ClientChannel.Open();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing && this.ClientChannel != null)
            {
                try
                {
                    this.ClientChannel.Close(TimeSpan.FromSeconds(2));
                }
                catch
                {
                    this.ClientChannel.Abort();
                }
            }

            //TODO: free unmanaged resources
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~WcfChannelClient()
        {
            Dispose(false);
        }

        private static Binding CreateBinding(WcfBindingType binding)
        {
            Binding bindinginstance = null;
            if (binding == WcfBindingType.BasicHttpBinding)
            {
                BasicHttpBinding ws = new BasicHttpBinding();
                ws.MaxBufferSize = 2147483647;
                ws.MaxBufferPoolSize = 2147483647;
                ws.MaxReceivedMessageSize = 2147483647;
                ws.ReaderQuotas.MaxStringContentLength = 2147483647;
                ws.CloseTimeout = new TimeSpan(0, 10, 0);
                ws.OpenTimeout = new TimeSpan(0, 10, 0);
                ws.ReceiveTimeout = new TimeSpan(0, 10, 0);
                ws.SendTimeout = new TimeSpan(0, 10, 0);
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.NetNamedPipeBinding)
            {
                NetNamedPipeBinding ws = new NetNamedPipeBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.NetPeerTcpBinding)
            {
                //NetPeerTcpBinding ws = new NetPeerTcpBinding();
                //ws.MaxReceivedMessageSize = 65535000;
                //bindinginstance = ws;
                throw new NotImplementedException();
            }
            else if (binding == WcfBindingType.NetTcpBinding)
            {
                NetTcpBinding ws = new NetTcpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                ws.Security.Mode = SecurityMode.None;
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.WebHttpBinding)
            {
                WebHttpBinding ws = new WebHttpBinding(); //Restful style
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.WSDualHttpBinding)
            {
                WSDualHttpBinding ws = new WSDualHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.WSFederationHttpBinding)
            {
                WSFederationHttpBinding ws = new WSFederationHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding == WcfBindingType.WSHttpBinding)
            {
                WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
                ws.MaxReceivedMessageSize = 65535000;
                ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
                ws.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                bindinginstance = ws;
            }
            return bindinginstance;

        }        
    }
}
View Code

針對每個WCF服務派生一個代理類,在其中重寫ServiceUrl與Binding,ServiceUrl可以配置到Config中,Binding不重寫預設採用NetTcpBinding

public class GameServiceClient : WcfChannelClient<IGameService>
    {
        public override string ServiceUrl
        {
            get
            {
                return "net.tcp://localhost:21336/GameService.svc";
            }
        }
    }

public class PlayerServiceClient : WcfChannelClient<IPlayerService>
    {
        public override string ServiceUrl
        {
            get
            {
                return "net.tcp://localhost:21336/PlayerService.svc";
            }
        }
    }

客戶端調用

using (var client = new GameServiceClient())
{
    client.Channel.DoWork("thinkpig");  //無返回值
    await client.Channel.DoWorkAsync("thinkpig");  //無返回值非同步
}

using (var client = new PlayerServiceClient())
{
    var result = client.Channel.DoWork("thinkdog");  //有返回值
    result = await client.Channel.DoWorkAsync("thinkdog");  //有返回值非同步
}

 

關於WCF寄宿主機可以參考前兩篇文章

WCF綁定netTcpBinding寄宿到控制台應用程式

WCF綁定netTcpBinding寄宿到IIS


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在《asp.net core認證與授權》中講解了固定和自定義角色授權系統許可權,其實我們還可以通過其他方式來授權,比如可以通過角色組,用戶名,生日等,但這些主要取決於ClaimTypes,其實我們也可以自定義鍵值來授權,這些統一叫策略授權,其中更強大的是,我們可以自定義授權Handler來達到靈活授權... ...
  • 在asp.net core中,微軟提供了基於認證(Authentication)和授權(Authorization)的方式,來實現許可權管理的,本篇博文,介紹基於固定角色的許可權管理和自定義角色許可權管理,本文內容,更適合傳統行業的BS應用,而非互聯網應用。 ...
  • 1 public bool IsRegistered() 2 { 3 string clsid = ConfigurationManager.AppSettings["clsid"]; 4 //參數檢查 5 Debug.Assert(!String.IsNullOrEmpty(clsid), "cl ...
  • 代碼中包含了檢測本地安裝盤符代碼 1 一,定義下載委托事件(用於實現前臺進度條更新和下載完成後事件的回調): 2 private delegate void Action(); 3 private string diverUrl = ConfigurationManager.AppSettings[ ...
  • 如果想體驗Linux環境下開發和運行.NET Core應用,我們有多種選擇。一種就是在一臺物理機上安裝原生的Linux,我們可以根據自身的喜好選擇某種Linux Distribution,目前來說像RHEL、Ubuntu、Debian、Fedora、CentOS和SUSE這些主流的Distribut... ...
  • 資料庫管理:access是資料庫其中之一。 語文:重點應用文 ,第一部字典通聞解字,第一部詩集詩經,第一部詞典爾雅,爾:接近,雅:普通話。蟲:有生命的生物,䖵:有翅膀的蟲,蟲:有毒的蟲 ...
  • 這個事情就是一個坑,耽誤了兩周時間,之前並沒有做過ActiveX這玩意,現在客戶需求如此,只能說是在網上看著教程做了。 事情是這樣的,有一臺海康威視的攝像頭,客戶需要一個ActiveX控制項嵌入到網頁中,通過點擊按鈕開始錄製和結束錄製來進行視頻的錄製和保存,關於海康攝像頭的二次開發在此就不多說了,可以 ...
  • C#跨線程調用控制項 在C#應用程式開發中,我們經常需要把UI線程和工作線程分開編程,為了防止界面停止響應。同時,我們也需要在工作線程中去更新UI界面的控制項,在CLR的線程安全中並不允許我們直接在工作線程操作UI界面。因此,介紹以下三種方式進行跨線程操作UI。 第一種方法:使用delegate和inv ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...