1. 程式人生 > 其它 >在.NET5中替代WCF進行通訊的解決方案

在.NET5中替代WCF進行通訊的解決方案

一、先說說WCF和WebService的概念。

參考文章:https://www.cnblogs.com/zhao123/p/5599096.html

Web Service 是一種可以接收從Internet(網際網路)或者Intranet(內聯網)上的其它系統中傳遞過來的請求,輕量級的獨立的通訊技術。是通過SOAP在Web上提供的軟體服務,使用WSDL檔案進行說明,並通過UDDI進行註冊。WebService可用基於XML的SOAP來表示資料和呼叫請求,並且通過HTTP協議來傳輸這些XML格式的資料。嚴格來說是行業標準,也就是WebService規範,也稱作WS-*規範,既不是框架,也不是技術。

XML:(Extensible Markup Language)擴充套件型可標記語言。面向短期的臨時資料處理、面向全球資訊網絡,是Soap的基礎。

Soap:(Simple Object Access Protocol)簡單物件存取協議。當用戶通過UDDI找到你的WSDL描述文件後,他通過可以SOAP呼叫你建立的Web服務中的一個或多個操作。SOAP是XML文件形式的呼叫方法的規範,它可以支援不同的底層介面,像HTTP(S)或者SMTP。

WSDL:(Web Services Description Language) WSDL 檔案是一個 XML 文件,用於說明一組 SOAP 訊息以及如何交換這些訊息。大多數情況下由軟體自動生成和使用。

UDDI (Universal Description, Discovery, and Integration) 是一個主要針對Web服務供應商和使用者的新專案。UDDI是一種根據描述文件來引導系統查詢相應服務的機制。UDDI利用SOAP訊息機制(標準的XML/HTTP)來發布,編輯,瀏覽以及查詢註冊資訊。它採用XML格式來封裝各種不同型別的資料,並且傳送到註冊中心或者由註冊中心來返回需要的資料。

ASP.NET WebService微軟的Web服務實現稱為ASP.NET WebService。

微軟提供了Web服務的開發框架,屬於ASP.NETFramework的一部分,但是支援早起的WS規範。比如SOAP1.1。

PS:後來微軟做了ASP.NETWebService的安全,效能,資料加密、解密,託管宿主等多方面的擴充套件,稱為WSE系列,這個是過度產品,最高到WSE3.0.後來就是WCF時代。

WCF(之前的版本名為“Indigo”)是使用託管程式碼建立和執行面向服務(ServiceOriented)應用程式的統一框架。它使得開發者能夠建立一個跨平臺的安全、可信賴、事務性的解決方案,且能與已有系統相容協作。

WCF 能夠建立相容 Web 服務的服務,也就是說可以建立能夠與Web 服務互聯互通的服務,他們兩個並不能用簡單包含或者等同關係來表述。WCF 是一套框架,用來建立各種服務。其中包括建立 Web服務(採用 basicHttpBinding繫結的服務就是一個Web 服務)。

二、使用SoapCore

那麼在.NET5.0已經不支援WCF框架的情況下,我們只能使用WebService來進行跨平臺的通訊。這裡需要引用一個包SoapCore。他是通過中介軟體的方式來進行SOAP協議的解析,具體參考:https://www.cnblogs.com/linezero/p/aspnetcoresoap.html

SoapCore包的原始碼:https://github.com/DigDes/SoapCore

使用SoapCore開發示例:https://www.cnblogs.com/xyyhcn/p/14862728.html

三、使用Dapper

在示例Demo成功之後,我們可以開始進行正式的業務邏輯處理。

首先少不了的是對資料庫的資料互動。在這裡我使用Dapper代替EF。Dapper更輕量級,只要通過Nuget引用Dapper.SqlBuilder包和System.Data.SqlClient系統包(用於支援SqlConnection)。

我們建立AppSetting類來提取appsettings.json檔案中配置的資料(比如資料庫連結地址)。

public class AppSettings

{

private static readonly IConfigurationRoot _config;

public static class ConnectionStrings

{

public static string Database => _config["ConnectionStrings:Default"];

}

}

再建立一個ConnectionFactory類來封裝對資料庫的連線。

public class ConnectionFactory

{

public static IDbConnection GetConnection()

{

return new SqlConnection(AppSettings.ConnectionStrings.Database);

}

}

建立倉儲介面ICustomerRepository來封裝與資料庫的互動(sql語句)。

在介面中定義Get和Add方法。

public interface ICustomerRepository

{

Task<Customer> Get(string code,string name);

Task Add(Customer model);

}

建立CustomerRepository實現ICustomerRepository的介面。

public class CustomerRepository : ICustomerRepository

{

private readonly string TenantId = AppSettings.Tenants.FindTenantWithIndex(0).Id;

public async Task<Customer> Getr(string code, string name)

{

using IDbConnection connection = ConnectionFactory.GetConnection();

SqlBuilder build = new SqlBuilder();

var sql = @"

select * from Customer_Project

where IsDeleted=0

and TenantId=@TenantId

and ( Code =@Code or Name=@Name)";

var selector = build.AddTemplate(sql, new

{

TenantId = TenantId,

Code = code,

Name = name

});

var results = await connection.QueryAsync<Customer>(selector.RawSql, selector.Parameters);

return results.FirstOrDefault();

}

public async Task Add(Customer model)

{

model.TenantId = Guid.Parse(TenantId);

var sql = @"INSERT INTO [dbo].[Customer_Info]

([Id]

,[CreationTime]

,[CreatorId]

,[TenantId]

,[Code]

,[Name]

,[ManagerId]

,[CategoryCode]

,[Status])

VALUES

(@Id

,@CreationTime

,@CreatorId

,@TenantId

,@Code

,@Name

,@ManagerId

,@CategoryCode

,@Status)

";

using IDbConnection connection = ConnectionFactory.GetConnection();

var result = await connection.ExecuteAsync(sql, model);

}

}

從程式碼中可以看到,首先我們通過ConnectionFactory和資料庫進行連線,即using IDbConnection connection = ConnectionFactory.GetConnection();

通過SqlBuilder來進行sql語句的值填充。最後執行QueryAsync/ExecuteAsync等方法對資料庫進行增刪改查操作。

四、Service實現業務邏輯

建立IMoldService對外提供介面。

[ServiceContract]

public interface IMoldService

{

[OperationContract]

Task<string> SyncMold(string input,string opt="I");

}

再建立MoldService實現IMoldService的介面,處理業務邏輯。