.netcore 寫(xiě)快遞100的快遞物流信息查詢(xún)接口的實(shí)現(xiàn)
快遞100的物流信息查詢(xún)接口,官方提供了一些demo;還好官方提供的代碼是.netcore版本寫(xiě)的,不過(guò)寫(xiě)的有點(diǎn)low;根據(jù)官方提供的代碼,我按照.netcore 的風(fēng)格重構(gòu)了代碼;核心代碼如下:
/// <summary> /// 沐雪微淘快遞100幫助類(lèi). /// </summary> public class KuaiDi100Helper { private ILogger _logger; private MuXueConfigHelper _configHelper; HttpClient _client; /// <summary> /// 快遞100幫助類(lèi) /// </summary> /// <param name="logger"></param> /// <param name="configHelper"></param> public KuaiDi100Helper(ILogger<KuaiDi100Helper> logger, HttpClient client, MuXueConfigHelper configHelper) { _configHelper = configHelper; _logger = logger; _client = client; } /// <summary> /// 實(shí)時(shí)快遞查詢(xún)接口 /// </summary> /// <param name="tenant_id"></param> /// <param name="shop_code"></param> /// <param name="com">查詢(xún)的快遞公司的編碼, 一律用小寫(xiě)字母</param> /// <param name="num">查詢(xún)的快遞單號(hào), 單號(hào)的最大長(zhǎng)度是32個(gè)字符</param> /// <param name="phone">收、寄件人的電話號(hào)碼(手機(jī)和固定電話均可,只能填寫(xiě)一個(gè),順豐單號(hào)必填,其他快遞公司選填。如座機(jī)號(hào)碼有分機(jī)號(hào),分機(jī)號(hào)無(wú)需上傳。)</param> /// <returns></returns> public async Task<QueryTackResult> QueryTrack(long tenant_id, string shop_code,string com,string num,string phone="") { QueryTackResult result = new QueryTackResult(); try { TenantConfig config = await _configHelper.GetTenantAllAsync(tenant_id, shop_code); QueryTrackParam queryTrackParam = new QueryTrackParam(); if (com== "shunfeng") { queryTrackParam = new QueryTrackParam() { com = com, num = num, phone = phone }; } else { queryTrackParam = new QueryTrackParam() { com = com, num = num, }; } QueryTrackReq query = new QueryTrackReq() { customer = config.KuaiDi100CustomerID, sign = SignUtils.GetMD5(queryTrackParam.ToString() + config.KuaiDi100Key + config.KuaiDi100CustomerID), param = queryTrackParam }; var requestParam = ObjectToDictionaryUtils.ObjectToMap(query); if (requestParam == null) { return null; } result = await HttpClientHelper.PostFormAsync<QueryTackResult>(_client, ApiInfoConstant.QUERY_URL, requestParam); } catch (Exception ex) { _logger.LogError(ex, $"快遞100實(shí)時(shí)快遞查詢(xún)接口異常:{ex.Message}"); return null; } return result; } }
上面的代碼一眼看,就知道必須要使用依賴(lài)注入;我們看到 在構(gòu)造函數(shù)里使用了HttpClient _client 這個(gè)東西;(因?yàn)橐{(diào)用快遞100的接口),
我們?cè)趕tartup里接著寫(xiě):
services.AddScoped<KuaiDi100Helper>();
如上代碼應(yīng)該是最常用的注冊(cè)方法;結(jié)果報(bào)錯(cuò),錯(cuò)誤信息如下:
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper Lifetime: Scoped ImplementationType: MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper': Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper'.)”
InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper'.
根據(jù)錯(cuò)誤信息看,應(yīng)該是httpclient出了問(wèn)題了;找了很久才找到解決方法,修改startup里的注冊(cè)方法
services.AddHttpClient<KuaiDi100Helper>(); //這樣注入
這樣就沒(méi)問(wèn)題了。
到此這篇關(guān)于.netcore 寫(xiě)快遞100的快遞物流信息查詢(xún)接口的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān).netcore 快遞查詢(xún)接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05ASP.NET Core中如何利用多種方式給Action傳參
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何利用多種方式給Action傳參的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12log4net在Asp.net MVC4中的使用過(guò)程
這篇文章主要介紹了log4net在Asp.net MVC4中的使用過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05Asp.net 頁(yè)面調(diào)用javascript變量的值
開(kāi)發(fā)過(guò)程中碰到了這種情況,我想將javascript中定義的變量賦值給頁(yè)面中的TextBox控件.2009-12-12