.NET Core調用WCF的最佳實踐

来源:https://www.cnblogs.com/lishilei0523/archive/2018/04/19/8886483.html
-Advertisement-
Play Games

現在.NET Core貌似很火,與其他.NET開發者交流不說上幾句.NET Core都感覺自己落伍了一樣。但是冷靜背後我們要也看到.NET Core目前還有太多不足,別的不多說,與自家的服務框架WCF集成起來就不咋地,從最初不支持,到現在有個笨笨咔咔的Web Service Reference Pr ...


現在.NET Core貌似很火,與其他.NET開發者交流不說上幾句.NET Core都感覺自己落伍了一樣。但是冷靜背後我們要也看到.NET Core目前還有太多不足,別的不多說,與自家的服務框架WCF集成起來就不咋地,從最初不支持,到現在有個笨笨咔咔的Web Service Reference Provider,生成的代理類簡直不堪入目,還特別的慢。所以本人本著為將來框架的相容性做準備,就著手研究了下能不能不通過代理類訪問WCF,好在微軟開源了一部分WCF代碼。

WCF的開發者一定很熟悉,WCF的所有配置都是可以通過代碼和配置文件兩種方式,而大多數開發者都會選擇配置文件,但是從.NET Core開始,微軟幹掉了Web/App.config,不知道別人怎麼樣,反正我是非常之不習慣。幹掉Web/App.config的後果,就是開源支持.NET Core的那部分Client Side沒有配置文件的支持,翻看源碼後發現,只要是讀取配置文件的地方,都是這樣的代碼 —— PlatformNotSupported。

protected void InitializeEndpoint(string configurationName, EndpointAddress address)
{
            _serviceEndpoint = this.CreateDescription();

            ServiceEndpoint serviceEndpointFromConfig = null;

            // Project N and K do not support System.Configuration, but this method is part of Windows Store contract.
            // The configurationName==null path occurs in normal use.
            if (configurationName != null)
            {
                throw ExceptionHelper.PlatformNotSupported();
                // serviceEndpointFromConfig = ConfigLoader.LookupEndpoint(configurationName, address, this.serviceEndpoint.Contract);
       }
}

 

但是好在微軟又推出了System.Configuration.ConfigurationManager的NuGet包,所以本人仿照WCF原生的配置文件,自己實現一套配置,經過兩個晚上的戰鬥,已經成功。好了,廢話不多說,上代碼了。

先看看最終的效果是什麼樣的,WCF服務端的代碼結構如下:

採用標準的WCF分層方式,用控制台做宿主,其中IAppService項目版本為.NET Standard 2.0,每個終結點同時使用BasicHttpBinding與NetTcpBinding雙重綁定,配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--WCF配置-->
  <system.serviceModel>
    <!--WCF服務配置,手動增加service節點-->
    <services>
      <!--產品服務配置-->
      <service behaviorConfiguration="DefaultBehavior" name="WCF.AppService.Implements.ProductService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8098/Hosts/ProductService.svc" />
            <add baseAddress="net.tcp://localhost:8099/Hosts/ProductService.svc" />
          </baseAddresses>
        </host>
        <endpoint binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="WCF.IAppService.Interfaces.IProductService" />
        <endpoint binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="WCF.IAppService.Interfaces.IProductService" />
      </service>
      <!--訂單服務配置-->
      <service behaviorConfiguration="DefaultBehavior" name="WCF.AppService.Implements.OrderService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8098/Hosts/OrderService.svc" />
            <add baseAddress="net.tcp://localhost:8099/Hosts/OrderService.svc" />
          </baseAddresses>
        </host>
        <endpoint binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="WCF.IAppService.Interfaces.IOrderService" />
        <endpoint binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="WCF.IAppService.Interfaces.IOrderService" />
      </service>
      <!--集成服務配置-->
      <service behaviorConfiguration="DefaultBehavior" name="WCF.AppService.Implements.IntegrationService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8098/Hosts/IntegrationService.svc" />
            <add baseAddress="net.tcp://localhost:8099/Hosts/IntegrationService.svc" />
          </baseAddresses>
        </host>
        <endpoint binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="WCF.IAppService.Interfaces.IIntegrationService" />
        <endpoint binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="WCF.IAppService.Interfaces.IIntegrationService" />
      </service>
    </services>
    <!--WCF行為配置,配置好無需修改-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <!--是否允許get請求訪問-->
          <serviceMetadata httpGetEnabled="true" />
          <!--允許從請求消息頭中檢索元數據地址信息-->
          <useRequestHeadersForMetadataAddress />
          <!--是否顯示異常信息-->
          <serviceDebug includeExceptionDetailInFaults="true" />
          <!--最大序列化的對象個數-->
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--WCF綁定配置,配置好無需修改-->
    <bindings>
      <netTcpBinding>
        <binding name="tcpBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" />
      </netTcpBinding>
      <basicHttpBinding>
        <binding name="basicBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" />
      </basicHttpBinding>
    </bindings>
    <!--WCF多宿主綁定配置-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

 

運行測試沒問題,服務端不多說,重點是客戶端,因為IAppService是.NET Standard 2.0的類庫版本,所以.NET Core客戶端是可以正常引用的,新建.NET Core控制台,並引用上述IAppService項目。

客戶端項目結構如下:

客戶端配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!--WCF配置節點-->
    <section name="system.serviceModel" type="System.ServiceModel.ServiceModelSection, System.ServiceModel.Toolkits" />
  </configSections>

  <!--WCF配置-->
  <system.serviceModel>
    <!--WCF客戶端配置,手動增加endpoint節點-->
    <client>
      <!--商品服務契約配置-->
      <endpoint address="net.tcp://localhost:8099/Hosts/ProductService.svc" binding="netTcpBinding" contract="WCF.IAppService.Interfaces.IProductService" name="WCF.IAppService.Interfaces.IProductService">
        <headerProvider type="WCF.Core.Client.HeaderProviders.MyHeaderProvider" assembly="WCF.Core.Client"/>
      </endpoint>
      <!--訂單服務契約配置-->
      <endpoint address="net.tcp://localhost:8099/Hosts/OrderService.svc" binding="netTcpBinding" contract="WCF.IAppService.Interfaces.IOrderService" name="WCF.IAppService.Interfaces.IOrderService" />
      <!--集成服務契約配置-->
      <endpoint address="net.tcp://localhost:8099/Hosts/IntegrationService.svc" binding="netTcpBinding" contract="WCF.IAppService.Interfaces.IIntegrationService" name="WCF.IAppService.Interfaces.IIntegrationService" />
    </client>
  </system.serviceModel>
</configuration>

 

Main方法中代碼如下:

class Program
    {
        static void Main(string[] args)
        {
            //初始化容器
            IContainer container = InitContainer();

            //調用
            IProductService productService = container.Resolve<IProductService>();
            string products = productService.GetProducts();

            Console.WriteLine(products);

            container.Dispose();
            Console.ReadKey();
        }

        static IContainer InitContainer()
        {
            ContainerBuilder builder = new ContainerBuilder();
            Assembly wcfInterfaceAssembly = Assembly.Load("WCF.IAppService");

            //獲取WCF介面類型集
            IEnumerable<Type> types = wcfInterfaceAssembly.GetTypes().Where(type => type.IsInterface);

            //獲取服務代理泛型類型
            Type proxyGenericType = typeof(ServiceProxy<>);

            //註冊WCF介面
            foreach (Type type in types)
            {
                Type proxyType = proxyGenericType.MakeGenericType(type);
                PropertyInfo propChannel = proxyType.GetProperty(ServiceProxy.ChannelPropertyName, type);

                builder.RegisterType(proxyType).OnRelease(proxy => ((IDisposable)proxy).Dispose());
                builder.Register(container => propChannel.GetValue(container.Resolve(proxyType))).
                    As(type).
                    OnRelease(channel => channel.CloseChannel());
            }

            return builder.Build();
        }
    }

 

啟動運行結果如下:

怎麼樣?是不是覺得很清爽?如果你有興趣,可以到我的Git看全部源碼,地址如下:

https://gitee.com/lishilei0523/WCF-DotNetCore

Ps:因為微軟公開的WCF類庫本身就不完善,所以我也沒法提供全部的功能,本人所作調用方式目前支持BasicHttpBinding和NetTcpBinding,並且包含消息頭支持。如果你覺得代碼對你有幫助,麻煩點個Star,不勝感激。


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

-Advertisement-
Play Games
更多相關文章
  • 上一篇我們已經創建好了一個Xadmin的基礎項目,現在我們將在此基礎上構建一個同樣很基礎的學生信息管理系統。 一、創建模型 模型是表示我們的資料庫表或集合類,並且其中所述類的每個屬性是表或集合的欄位,在 app/models.py 中定義。 1、首先,導入models模塊 接下來創建個學生信息類,其 ...
  • 名字 全局變數使用描述性的名字,局部變數則使用簡潔的名字。根據定義,全局變數可以出現在整個程式中的任何地方,因此他們需要一個足夠長並且詳細描述的名字讓讀者想起它們的意義。給每個全局變數聲明附加一個簡短註釋也非常有幫助: 全局函數、類和結構應該也要有描述性的名字,以表明它們在程式里扮演的角色。對於局部 ...
  • 之前有大概介紹了音頻採樣相關的思路,詳情見《簡潔明瞭的插值音頻重採樣演算法例子 (附完整C代碼)》。 音頻方面的開源項目很多很多。 最知名的莫過於谷歌開源的WebRTC, 其中的音頻模塊就包含有 AGC自動增益補償(Automatic Gain Control)自動調麥克風的收音量,使與會者收到一定的 ...
  • 測試代碼1: 輸出結果(myapplication為包名): Environment.getDataDirectory():/dataEnvironment.getExternalStorageDirectory():/storage/emulated/0 Environment.getRootDi ...
  • 1、多線程有什麼用? 一個可能在很多人看來很扯淡的一個問題:我會用多線程就好了,還管它有什麼用?在我看來,這個回答更扯淡。所謂”知其然知其所以然”,”會用”只是”知其然”,”為什麼用”才是”知其所以然”,只有達到”知其然知其所以然”的程度才可以說是把一個知識點運用自如。OK,下麵說說我對這個問題的看 ...
  • 本文來告訴大家如何在 Direct2D1 繪製基本圖形,包括線段、矩形、橢圓 ...
  • 本文告訴大家如何使用dot net core 和其他進程進行通信 ...
  • OpenCVOpenCV是一個開源的跨平臺的電腦視覺庫,它的設計目標時執行速度儘量快,主要關註實時應用。可以運行在Linux、Windows、Mac OS、Android、IOS、Maemo、FreeBSD、OpenBSD等操作系統上。OpenCV採用C/C++語言開發和使用外還支持使用C#、Ru... ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...