如安在Asp.Net Core中集成Refit(6)

  在上面接口中界说好这个要领今后我们就可以直接在我们的规模类中引入这个接口IDmsAfterSalesApi ,然后就直接利用这个接口中的要领,讲到这里便有疑问,这个接口的实现到底在那边?这里当我们界说好接口然后点击内里的要领转到实现的时候我们发明内里会转到一个叫做RefitStubs.g.cs的类中,然后自动的生成下面的要领。

/// <inheritdoc /> [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Diagnostics.DebuggerNonUserCode] [Preserve] [global::System.Reflection.Obfuscation(Exclude=true)] partial class AutoGeneratedIDmsAfterSalesApi : IDmsAfterSalesApi {     /// <inheritdoc />     public HttpClient Client { get; protected set; }     readonly IRequestBuilder requestBuilder;       /// <inheritdoc />     public AutoGeneratedIDmsAfterSalesApi(HttpClient client, IRequestBuilder requestBuilder)     {         Client = client;         this.requestBuilder = requestBuilder;     }       /// <inheritdoc />     Task<ResponseBody> IDmsAfterSalesApi.UpdateCustomerAmount(PartRetailSettlementModel input)     {         var arguments = new object[] { input };         var func = requestBuilder.BuildRestResultFuncForMethod("UpdateCustomerAmount", new Type[] { typeof(PartRetailSettlementModel) });         return (Task<ResponseBody>)func(Client, arguments);     }       /// <inheritdoc />     Task<RepairShortagePartResponseBody> IDmsAfterSalesApi.RepairShortagePartCheckCustomerAccount(RepairShortagePartModel input)     {         var arguments = new object[] { input };         var func = requestBuilder.BuildRestResultFuncForMethod("RepairShortagePartCheckCustomerAccount", new Type[] { typeof(RepairShortagePartModel) });         return (Task<RepairShortagePartResponseBody>)func(Client, arguments);     }       /// <inheritdoc />     Task<GetMemberCodeBrandCodeForVehicleBody> IDmsAfterSalesApi.GetMemberCodeBrandCodeForVehicle(Guid vehicleId)     {         var arguments = new object[] { vehicleId };         var func = requestBuilder.BuildRestResultFuncForMethod("GetMemberCodeBrandCodeForVehicle", new Type[] { typeof(Guid) });         return (Task<GetMemberCodeBrandCodeForVehicleBody>)func(Client, arguments);     } }  

  这内里的焦点是挪用一个BuildRestResultFuncForMethod的要领,后头我们再来阐明这内里到底是怎么实现的,这里我们首先把这整个利用流程说完,之前我们说过Refit的许多设置都是通过标签的方法来注入进去的,这里包罗请求范例、相对请求地点,那么我们的默认超时时间和BaseAddress到底是奈何来设置的呢?下面我们就来重点报告。

  2.4  在Startup中设置基本设置信息

public IServiceProvider ConfigureServices(IServiceCollection services) {           //refit dms after sales处事           services.AddRefitClient<IDmsAfterSalesApi>()               .ConfigureHttpClient(c => {                   c.BaseAddress = new Uri(_appConfiguration["DependencyServices:DmsAfterSales"]);                   c.Timeout = TimeSpan.FromMilliseconds(_appConfiguration.GetValue<int>("AppSettings:ServiceTimeOutMs"));               });       }

  这里我们看到通过一个AddRefitClient要领我们就可以或许去设置我们的基本信息,讲到这里我们是不是对整个进程都有一个清楚的认识呢?通过上下两种方法的比拟,相信你对整个Refit的利用都有本身的领略。

  2.5 留意事项

  由于我们的Headers常常需要我们去设置一组数据,那么我们应该怎么设置多个项呢?

[Headers("User-Agent: Dms-Parts", "Content-Type: application/json")]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjjpz.html