您好,登錄后才能下訂單哦!
小編給大家分享一下.netcore如何寫快遞100的快遞物流信息查詢接口,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
快遞100的物流信息查詢接口,官方提供了一些demo;還好官方提供的代碼是.netcore版本寫的,不過寫的有點low;根據官方提供的代碼,我按照.netcore 的風格重構了代碼;核心代碼如下:
public class KuaiDi100Helper { private ILogger _logger; private MuXueConfigHelper _configHelper; HttpClient _client; /// <summary> /// 快遞100幫助類 /// </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> /// 實時快遞查詢接口 /// </summary> /// <param name="tenant_id"></param> /// <param name="shop_code"></param> /// <param name="com">查詢的快遞公司的編碼, 一律用小寫字母</param> /// <param name="num">查詢的快遞單號, 單號的最大長度是32個字符</param> /// <param name="phone">收、寄件人的電話號碼(手機和固定電話均可,只能填寫一個,順豐單號必填,其他快遞公司選填。如座機號碼有分機號,分機號無需上傳。)</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實時快遞查詢接口異常:{ex.Message}"); return null; } return result; } }
上面的代碼一眼看,就知道必須要使用依賴注入;我們看到 在構造函數里使用了HttpClient _client 這個東西;(因為要調用快遞100的接口),
我們在startup里接著寫:
services.AddScoped<KuaiDi100Helper>();
如上代碼應該是最常用的注冊方法;結果報錯,錯誤信息如下:
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'.
根據錯誤信息看,應該是httpclient出了問題了;找了很久才找到解決方法,修改startup里的注冊方法
services.AddHttpClient<KuaiDi100Helper>(); //這樣注入
這樣就沒問題了。
以上是“.netcore如何寫快遞100的快遞物流信息查詢接口”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。