Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet-svcutil: optimize the referenced wcf package version and add test. #5519

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,19 @@
using System.ServiceModel;
using System.Threading.Tasks;
using System.Linq;
using System.Xml.Linq;

namespace Microsoft.Tools.ServiceModel.Svcutil
{
internal class AddAsyncOpenClose : ClientClassVisitor
{
private bool _generateCloseAsync = false;
private bool _addCondition = false;

public AddAsyncOpenClose(CommandProcessorOptions options)
{
if (options.Project != null && options.Project.TargetFrameworks.Count() > 1 && options.Project.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && !netfxInfo.IsDnx))
{
_generateCloseAsync = true;
FrameworkInfo dnxInfo = null;
var tfx = options.Project.TargetFrameworks.FirstOrDefault(t => TargetFrameworkHelper.IsSupportedFramework(t, out dnxInfo) && dnxInfo.IsDnx);
if (!string.IsNullOrEmpty(tfx) && dnxInfo.Version.Major >= 6)
{
_addCondition = true;
}
}
else
{
if (options.TargetFramework.IsDnx)
{
if (TargetFrameworkHelper.NetCoreVersionReferenceTable.TryGetValue(options.TargetFramework.Version, out var referenceTable))
{
string version = referenceTable.FirstOrDefault().Version;
string[] vers = version.Split('.');
if (vers.Length > 1)
{
Version v = new Version(int.Parse(vers[0]), int.TryParse(vers[1], out int minor) ? minor : 0);
// For .NETCore targetframework found in the referenced table, generate CloseAsync() when WCF package version is less than 4.10
if (v.CompareTo(new Version(4, 10)) < 0)
{
_generateCloseAsync = true;
}
}
}
}
else
{
// For supported non-Dnx target frameworks (eg: net472, net48), generate CloseAsync() as before
_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 @@ -104,9 +58,9 @@ private CodeMemberMethod GenerateTaskBasedAsyncMethod(string methodName, NameSco
GenerateBeginMethodInvokeExpression(methodName),
delegateOfEndCall)));

if(_addCondition && methodName.Equals("Close"))
if(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
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
new TypeNameFixup()
};

Expand Down
21 changes: 15 additions & 6 deletions src/dotnet-svcutil/lib/src/Shared/FrameworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class FrameworkInfo
public const string Netstandard = "netstandard";
public const string Netcoreapp = "netcoreapp";
public const string Netfx = "net";
public const string Netframework = "netframework";
public const string Netversion = "version";

private FrameworkInfo()
Expand All @@ -40,6 +41,7 @@ public static FrameworkInfo Parse(string fullFrameworkName)
// framework spec form: 'net5.0'
// framework spec form: '.NETCoreApp,Version=v6.0'
// framework spec form: '.NETFramework,Version=v4.8'
// framework spec form: '.NETStandard,Version=v2.0'
// framework spec form: 'net7.0-windows10.0.19041.0', 'net7.0-windows'
for (int i = 0; i < fullFrameworkName.Length; i++)
{
Expand Down Expand Up @@ -76,15 +78,22 @@ public static FrameworkInfo Parse(string fullFrameworkName)

if (name.ToLower().Contains(Netversion))
{
//netcoreapp3.1 and lower
if (version.Major < 4)
//.NETStandard,Version=v2.0 => netstandard2.0
if (name.ToLower().Contains(Netstandard))
{
name = Netcoreapp;
name = Netstandard;
}
else
//.NETFramework,Version=v4.8 => net4.8
//.NETCoreApp,Version=v6.0 => net6.0
else if (name.ToLower().Contains(Netframework) || version.Major >= 5)
{
name = Netfx;
}
//.NETCoreApp,Version=v3.1 => netcoreapp3.1
else
{
name = Netcoreapp;
}

fullFrameworkName = string.Concat(name, version.ToString());
}
Expand All @@ -101,8 +110,8 @@ public static FrameworkInfo Parse(string fullFrameworkName)
fxInfo.Version = version;
fxInfo.IsDnx = name == Netstandard || name == Netcoreapp || version.Major >= 5;
fxInfo.IsKnownDnx = fxInfo.IsDnx &&
(TargetFrameworkHelper.NetStandardToNetCoreVersionMap.Keys.Any((netstdVersion) => netstdVersion == version) ||
TargetFrameworkHelper.NetStandardToNetCoreVersionMap.Values.Any((netcoreVersion) => netcoreVersion == version));
((name == Netstandard && TargetFrameworkHelper.NetStandardToNetCoreVersionMap.Keys.Any((netstdVersion) => netstdVersion == version)) ||
(name != Netstandard && TargetFrameworkHelper.NetCoreToWCFPackageReferenceVersionMap.Keys.Any((netcoreVersion) => netcoreVersion == version)));

return fxInfo;
}
Expand Down
20 changes: 8 additions & 12 deletions src/dotnet-svcutil/lib/src/Shared/MSBuildProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,12 @@ private XElement PacakgeReferenceGroup
_packageReferenceGroup = refItems.FirstOrDefault().Parent;
}

FrameworkInfo netfxInfo = null;
FrameworkInfo dnxInfo = null;
if (this.TargetFrameworks.Count() > 1 && this.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out netfxInfo) && !netfxInfo.IsDnx))
if (this.TargetFrameworks.Count() > 1 && this.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && !netfxInfo.IsDnx))
{
var tfx = this.TargetFrameworks.FirstOrDefault(t => TargetFrameworkHelper.IsSupportedFramework(t, out dnxInfo) && dnxInfo.IsDnx);
if (!string.IsNullOrEmpty(tfx) && dnxInfo.Version.Major >= 6)
Version ver = TargetFrameworkHelper.GetLowestNetCoreVersion(this.TargetFrameworks);
if (ver != null && ver.Major >= 6)
{
_packageReferenceGroup.Add(new XAttribute("Condition", $"'$(TargetFramework)' != '{netfxInfo.FullName}'"));
_packageReferenceGroup.Add(new XAttribute("Condition", $"!$(TargetFramework.StartsWith('net4'))"));
}
}
}
Expand Down Expand Up @@ -541,15 +539,13 @@ public bool AddDependency(ProjectDependency dependency, bool copyInternalAssets
this.ProjectReferceGroup.Add(new XElement("ProjectReference", new XAttribute("Include", dependency.FullPath)));
break;
case ProjectDependencyType.Binary:
FrameworkInfo netfxInfo = null;
FrameworkInfo dnxInfo = null;
string dnxStr = this.TargetFrameworks.FirstOrDefault(t => TargetFrameworkHelper.IsSupportedFramework(t, out dnxInfo) && dnxInfo.IsDnx);
Version ver = TargetFrameworkHelper.GetLowestNetCoreVersion(this.TargetFrameworks);
if (this.TargetFrameworks.Count() > 1 && dependency.Name.Equals(TargetFrameworkHelper.FullFrameworkReferences.FirstOrDefault().Name)
&& !string.IsNullOrWhiteSpace(dnxStr) && dnxInfo.Version.Major >= 6)
&& ver != null && ver.Major >= 6)
{
if (this.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out netfxInfo) && !netfxInfo.IsDnx))
if (this.TargetFrameworks.Any(t => TargetFrameworkHelper.IsSupportedFramework(t, out FrameworkInfo netfxInfo) && !netfxInfo.IsDnx))
{
this.ReferenceGroup.Add(new XElement("Reference", new XAttribute("Condition", $"'$(TargetFramework)' == '{netfxInfo.FullName}'"), new XAttribute("Include", dependency.AssemblyName), new XElement("HintPath", dependency.FullPath)));
this.ReferenceGroup.Add(new XElement("Reference", new XAttribute("Condition", $"$(TargetFramework.StartsWith('net4'))"), new XAttribute("Include", dependency.AssemblyName), new XElement("HintPath", dependency.FullPath)));
}
}
else
Expand Down
Loading
Loading