Skip to content

Commit

Permalink
Optimize generation of CloseAsync() method and update test baselines.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang committed Aug 16, 2024
1 parent a3454cd commit 5882023
Show file tree
Hide file tree
Showing 93 changed files with 649 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,14 @@ namespace Microsoft.Tools.ServiceModel.Svcutil
{
internal class AddAsyncOpenClose : ClientClassVisitor
{
//default behavior: won't generate CloseAsync()
private bool _generateCloseAsync = false;
private bool _addCondition = false;

public AddAsyncOpenClose(CommandProcessorOptions options)
{
if (options.Project != null)
{
Version lowestNetcoreVer = TargetFrameworkHelper.GetLowestNetCoreVersion(options.Project.TargetFrameworks);
bool containsNet6OrGreater = options.Project.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && netfxInfo.IsDnx && netfxInfo.Version.Major >= 6);
bool containsNetFx = options.Project.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && !netfxInfo.IsDnx);

// netfx or lowest.net core < 6.0
if (lowestNetcoreVer == null || (lowestNetcoreVer != null && lowestNetcoreVer.Major < 6))
{
_generateCloseAsync = true;
if (containsNet6OrGreater)
{
_addCondition = true;
}
}
//lowest .net core >= 6.0
else if (containsNetFx)
{
_generateCloseAsync = true;
_addCondition = true;
}
}
else if (options.TargetFramework.Version.Major < 6)
{
_generateCloseAsync = true;
}
}

protected override void VisitClientClass(CodeTypeDeclaration type)
{
base.VisitClientClass(type);

using (NameScope nameScope = new CodeTypeNameScope(type))
{
type.Members.Add(GenerateTaskBasedAsyncMethod("Open", nameScope));
if(_generateCloseAsync)
{
type.Members.Add(GenerateTaskBasedAsyncMethod("Close", nameScope));
}
type.Members.Add(GenerateTaskBasedAsyncMethod("Close", nameScope));
}
}

Expand Down Expand Up @@ -95,7 +58,7 @@ private CodeMemberMethod GenerateTaskBasedAsyncMethod(string methodName, NameSco
GenerateBeginMethodInvokeExpression(methodName),
delegateOfEndCall)));

if(_addCondition && methodName.Equals("Close"))
if(methodName.Equals("Close"))
{
CodeIfDirective ifStart = new CodeIfDirective(CodeIfMode.Start, "!NET6_0_OR_GREATER");
CodeIfDirective ifEnd = new CodeIfDirective(CodeIfMode.End, "");
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-svcutil/lib/src/CodeDomFixup/VisitorFixup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static CodeDomVisitor[] GetVisitors(ServiceContractGenerator generator,
new SpecialIXmlSerializableRemapper(arrayOfXElementTypeHelper),
new EnsureAdditionalAssemblyReference(),
new CreateCallbackImpl((generator.Options & ServiceContractGenerationOptions.TaskBasedAsynchronousMethod) == ServiceContractGenerationOptions.TaskBasedAsynchronousMethod, generator),
new AddAsyncOpenClose(options), // this one need to run after CreateCallbakImpl which provide name of VerifyCallbackEvents method
new AddAsyncOpenClose(), // this one need to run after CreateCallbakImpl which provide name of VerifyCallbackEvents method
};

if (options.Sync != true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ public virtual System.Threading.Tasks.Task OpenAsync()
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

#if !NET6_0_OR_GREATER
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
#endif

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.defaultEndpoint))
Expand Down
Loading

0 comments on commit 5882023

Please sign in to comment.