diff --git a/AutoRest/Generators/CSharp/CSharp/AutoRest.Generator.CSharp.csproj b/AutoRest/Generators/CSharp/CSharp/AutoRest.Generator.CSharp.csproj index 1a54330cc9097..4885670c6bdc5 100644 --- a/AutoRest/Generators/CSharp/CSharp/AutoRest.Generator.CSharp.csproj +++ b/AutoRest/Generators/CSharp/CSharp/AutoRest.Generator.CSharp.csproj @@ -36,6 +36,7 @@ True Resources.resx + diff --git a/AutoRest/Generators/CSharp/CSharp/SyncWrapperGenerationMode.cs b/AutoRest/Generators/CSharp/CSharp/SyncWrapperGenerationMode.cs new file mode 100644 index 0000000000000..a5112f2b2e860 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp/SyncWrapperGenerationMode.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + + +namespace Microsoft.Rest.Generator.CSharp +{ + /// + /// Defines supported modes for sync wrapper generation + /// + public enum SyncWrapperGenerationMode + { + All = 0, + Essential = 1, + None = 2 + } +} diff --git a/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs b/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs index 22d1ca54614d0..c27bb73925fb5 100644 --- a/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs +++ b/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs @@ -92,22 +92,39 @@ public virtual string GetAsyncMethodParameterDeclaration() } /// - /// Generate the method parameter declaration for async methods and extensions + /// Generate the method parameter declaration for sync methods and extensions /// /// If true add the customHeader to the parameters /// Generated string of parameters - public virtual string GetAsyncMethodParameterDeclaration(bool addCustomHeaderParameters) + public virtual string GetSyncMethodParameterDeclaration(bool addCustomHeaderParameters) { var declarations = this.SyncMethodParameterDeclaration; - if (!string.IsNullOrEmpty(declarations)) + if (!string.IsNullOrEmpty(declarations) && addCustomHeaderParameters) { declarations += ", "; } if (addCustomHeaderParameters) { - declarations += "Dictionary> customHeaders = null, "; + declarations += "Dictionary> customHeaders = null"; } + + return declarations; + } + + /// + /// Generate the method parameter declaration for async methods and extensions + /// + /// If true add the customHeader to the parameters + /// Generated string of parameters + public virtual string GetAsyncMethodParameterDeclaration(bool addCustomHeaderParameters) + { + var declarations = this.GetSyncMethodParameterDeclaration(addCustomHeaderParameters); + + if (!string.IsNullOrEmpty(declarations)) + { + declarations += ", "; + } declarations += "CancellationToken cancellationToken = default(CancellationToken)"; return declarations; @@ -129,12 +146,12 @@ public string SyncMethodInvocationArgs /// /// Get the invocation args for an invocation with an async method /// - public string GetAsyncMethodInvocationArgs (string customHeaderReference) + public string GetAsyncMethodInvocationArgs (string customHeaderReference, string cancellationTokenReference = "cancellationToken") { List invocationParams = new List(); LocalParameters.ForEach(p => invocationParams.Add(p.Name)); invocationParams.Add(customHeaderReference); - invocationParams.Add("cancellationToken"); + invocationParams.Add(cancellationTokenReference); return string.Join(", ", invocationParams); } diff --git a/AutoRest/Generators/CSharp/CSharp/Templates/ExtensionMethodTemplate.cshtml b/AutoRest/Generators/CSharp/CSharp/Templates/ExtensionMethodTemplate.cshtml index aaba0ea91e175..c831d5619b740 100644 --- a/AutoRest/Generators/CSharp/CSharp/Templates/ExtensionMethodTemplate.cshtml +++ b/AutoRest/Generators/CSharp/CSharp/Templates/ExtensionMethodTemplate.cshtml @@ -40,6 +40,7 @@ foreach (var parameter in Model.LocalParameters) @:Task.Factory.StartNew(s => ((I@(Model.MethodGroupName))s).@(Model.Name)Async(@(Model.SyncMethodInvocationArgs)), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); } @:} + @EmptyLine @if (!String.IsNullOrEmpty(Model.Description) || !String.IsNullOrEmpty(Model.Summary)) { @@ -93,5 +94,33 @@ foreach (var parameter in Model.LocalParameters) @:await operations.@(Model.Name)WithHttpMessagesAsync(@(Model.GetAsyncMethodInvocationArgs("null"))).ConfigureAwait(false); } @:} + +@EmptyLine +@if (!String.IsNullOrEmpty(Model.Description) || !String.IsNullOrEmpty(Model.Summary)) +{ +@:/// +@:@WrapComment("/// ", String.IsNullOrEmpty(Model.Summary) ? Model.Description.EscapeXmlComment() : Model.Summary.EscapeXmlComment()) +@:/// +} +@if (!String.IsNullOrEmpty(Model.Description) && !String.IsNullOrEmpty(Model.Summary)) +{ +@:@WrapComment("/// ", Model.Description.EscapeXmlComment()) +} +@:/// +@:/// The operations group for this extension method. +@:/// +foreach (var parameter in Model.LocalParameters) +{ +@:/// +@:@WrapComment("/// ", parameter.Documentation.EscapeXmlComment()) +@:/// +} +@:/// +@:/// Headers that will be added to request. +@:/// +@:public static @Model.OperationResponseReturnTypeString @(Model.Name)WithHttpMessages(@Model.GetExtensionParameters(Model.GetSyncMethodParameterDeclaration(true))) +@:{ +@: return operations.@(Model.Name)WithHttpMessagesAsync(@(Model.GetAsyncMethodInvocationArgs("customHeaders", "CancellationToken.None"))).ConfigureAwait(false).GetAwaiter().GetResult(); +@:} @: }