您好,登錄后才能下訂單哦!
小編給大家分享一下.netcore如何寫(xiě)快遞100的快遞物流信息查詢(xún)接口,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
快遞100的物流信息查詢(xún)接口,官方提供了一些demo;還好官方提供的代碼是.netcore版本寫(xiě)的,不過(guò)寫(xiě)的有點(diǎn)low;根據(jù)官方提供的代碼,我按照.netcore 的風(fēng)格重構(gòu)了代碼;核心代碼如下:
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)題了。
以上是“.netcore如何寫(xiě)快遞100的快遞物流信息查詢(xún)接口”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。