From b03e74758ed30f16fa019d48c4b6d1242b011920 Mon Sep 17 00:00:00 2001 From: cedricboon Date: Wed, 20 Feb 2019 12:06:41 +0100 Subject: [PATCH 1/2] Apply async best practices in libraries Added the possibility to pass a cancellation token in the async calls + call the HttpClient methods with the .ConfigureAwait(false) option. --- .../Templates/CSharpProxyTemplate.tt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt b/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt index 09fc389..8278273 100644 --- a/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt +++ b/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt @@ -1,4 +1,4 @@ -<#@ template language="C#" #> +<#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> @@ -21,6 +21,7 @@ using System.Threading.Tasks; using System.Net.Http.Formatting; using System.Linq; using System.Net; +using System.Threading; using System.Web; using <#= Configuration.Namespace#>.Models; @@ -139,9 +140,9 @@ namespace <#= Configuration.Namespace#>.Interfaces <# if (Configuration.GenerateAsyncReturnTypes == false || String.IsNullOrEmpty(concreteReturnType)) { #> /// - Task <#= method.Name #>Async(<#= parameterList#>); + Task <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken); <# } else { #> - Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#>); + Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken); <# } #> <# foreach(var p in method.UrlParameters) {#> @@ -379,9 +380,9 @@ namespace <#= Configuration.Namespace#>.Clients /// <#= p.Description.ToSummary() #> <# } #> /// - protected virtual async Task <#= method.Name #>AsyncMsg(<#= parameterList#>) + protected virtual async Task <#= method.Name #>AsyncMsg(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken)) { - return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>); + return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false); } <# if (Configuration.GenerateAsyncReturnTypes == false || String.IsNullOrEmpty(concreteReturnType)) { #> @@ -392,9 +393,9 @@ namespace <#= Configuration.Namespace#>.Clients /// <#= p.Description.ToSummary() #> <# } #> /// - public virtual async Task <#= method.Name #>Async(<#= parameterList#>) + public virtual async Task <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken)) { - return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>); + return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false); } <# } else { #> @@ -405,9 +406,9 @@ namespace <#= Configuration.Namespace#>.Clients /// <#= p.Description.ToSummary() #> <# } #> /// - public virtual async Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#>) + public virtual async Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken)) { - var result = await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>); + var result = await HttpClient.<#=method.Type.ToTitle()#><#= postOrPutOrPatch ? "AsJson" : "" #>Async<#= postOrPutOrPatch && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPutOrPatch ? bodyParameterString:""#>, cancellationToken).ConfigureAwait(false); EnsureSuccess(result); @@ -437,4 +438,3 @@ namespace <#= Configuration.Namespace#>.Clients <# } #> } #endregion - From b386bc38a93e639b93ea7982d60634d4518b52a9 Mon Sep 17 00:00:00 2001 From: cedricboon Date: Wed, 20 Feb 2019 15:02:05 +0100 Subject: [PATCH 2/2] Fixed backwards compatibility of the interfaces --- WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt b/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt index 8278273..86b4f7f 100644 --- a/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt +++ b/WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt @@ -140,9 +140,9 @@ namespace <#= Configuration.Namespace#>.Interfaces <# if (Configuration.GenerateAsyncReturnTypes == false || String.IsNullOrEmpty(concreteReturnType)) { #> /// - Task <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken); + Task <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken)); <# } else { #> - Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken); + Task<<#= concreteReturnType #>> <#= method.Name #>Async(<#= parameterList#><#= parameterList.Any() ? ", " : "" #>CancellationToken cancellationToken = default(CancellationToken)); <# } #> <# foreach(var p in method.UrlParameters) {#>