Skip to content

Commit

Permalink
Update generation of CloseAsync() and corresponding test updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang committed Jul 16, 2024
1 parent f3524ab commit a3454cd
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.ServiceModel;
using System.Threading.Tasks;
using System.Linq;
using static Microsoft.Tools.ServiceModel.Svcutil.TargetFrameworkHelper;

namespace Microsoft.Tools.ServiceModel.Svcutil
{
Expand All @@ -19,21 +18,29 @@ internal class AddAsyncOpenClose : ClientClassVisitor

public AddAsyncOpenClose(CommandProcessorOptions options)
{
//multi TFMs contain .NET Framework targets. Sample forms: net48;net6.0 / net472;net48
if (options.Project != null && options.Project.TargetFrameworks.Count() > 1 && options.Project.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && !netfxInfo.IsDnx))
if (options.Project != null)
{
_generateCloseAsync = true;
FrameworkInfo dnxInfo = 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);

//TFMs contain both .NET Framework and .NET (Core) targets, add condition for generated CloseAsync()
var tfx = options.Project.TargetFrameworks.FirstOrDefault(t => TargetFrameworkHelper.IsSupportedFramework(t, out dnxInfo) && dnxInfo.IsDnx);
if (!string.IsNullOrEmpty(tfx))
// 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;
}
}
// For supported non-Dnx target frameworks (eg: net472, net48), generate CloseAsync() as before
else if (!options.TargetFramework.IsDnx)
else if (options.TargetFramework.Version.Major < 6)
{
_generateCloseAsync = true;
}
Expand Down Expand Up @@ -90,7 +97,7 @@ private CodeMemberMethod GenerateTaskBasedAsyncMethod(string methodName, NameSco

if(_addCondition && methodName.Equals("Close"))
{
CodeIfDirective ifStart = new CodeIfDirective(CodeIfMode.Start, "NETFRAMEWORK");
CodeIfDirective ifStart = new CodeIfDirective(CodeIfMode.Start, "!NET6_0_OR_GREATER");
CodeIfDirective ifEnd = new CodeIfDirective(CodeIfMode.End, "");
implMethod.StartDirectives.Add(ifStart);
implMethod.EndDirectives.Add(ifEnd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,11 @@ 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));
}

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));
}

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 @@ -9,7 +9,7 @@
"*, ServiceReference"
],
"outputFile": "Reference.cs",
"targetFramework": "N.N",
"targetFramework": "net46",
"typeReuseMode": "All"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ 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));
}

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));
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ITypeReuseSvc))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ 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 NETFRAMEWORK
#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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ 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));
}

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));
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ITypeReuseSvc))
Expand Down
5 changes: 0 additions & 5 deletions src/dotnet-svcutil/lib/tests/Baselines/TFM/tfm45/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ 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));
}

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));
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ITypeReuseSvc))
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet-svcutil/lib/tests/src/GlobalToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ public void FullFramework(string testCaseName)

var uri = Path.Combine(g_TestCasesDir, "wsdl", "WcfProjectNService", "tempuri.org.wsdl");
var outDir = Path.Combine(this_TestCaseOutputDir, "ServiceReference");
var options = $"{uri} -nl -d {outDir} -tf net8.0";
var options = $"{uri} -nl -d {outDir}";

TestGlobalSvcutil(options, expectSuccess: true);
}

[Trait("Category", "BVT")]
[Theory]
[InlineData("net48")] //System.ServiceModel get referenced and CloseAsync() is generated
[InlineData("net50")] //WCF package V4.10 get referenced and CloseAsync() is not generated
[InlineData("netstd20")] //WCF package V4.10 get referenced and CloseAsync() is not generated
[InlineData("net50")] //WCF package V4.10 get referenced and CloseAsync() is generated
[InlineData("netstd20")] //WCF package V4.10 get referenced and CloseAsync() is generated
[InlineData("net60")] //WCF package V6.* get referenced and CloseAsync() is not generated
[InlineData("net60net48")] //WCF package V6.* and System.ServiceModel.dll are conditionally referenced base on TFM and CloseAsync() is generarted with compilation marker
public async Task MultiTargetCloseAsyncGeneration(string testCaseName)
Expand Down

0 comments on commit a3454cd

Please sign in to comment.