From f5f3fdc0c5dc7b3858d817bfd01f44f2273e5c27 Mon Sep 17 00:00:00 2001 From: John Leyva Date: Tue, 5 Oct 2021 20:02:54 +0100 Subject: [PATCH] Ensure same default hostname and service path is used (#434) Accepts --hostname cli argument Co-authored-by: jleyva --- src/Samples/DesktopClient/DesktopClient.csproj | 2 +- src/Samples/DesktopClient/Program.cs | 18 +++++++++++------- src/Samples/DesktopServer/Program.cs | 2 +- src/Samples/NetCoreClient/NetCoreClient.csproj | 1 + src/Samples/NetCoreClient/Program.cs | 17 +++++++++++------ src/Samples/StandardClient/ClientLogic.cs | 14 ++++++++++++-- src/Samples/StandardClient/IEchoService.cs | 8 +------- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Samples/DesktopClient/DesktopClient.csproj b/src/Samples/DesktopClient/DesktopClient.csproj index 2d90a76a6..926878fdc 100644 --- a/src/Samples/DesktopClient/DesktopClient.csproj +++ b/src/Samples/DesktopClient/DesktopClient.csproj @@ -1,4 +1,4 @@ - + Exe net472 diff --git a/src/Samples/DesktopClient/Program.cs b/src/Samples/DesktopClient/Program.cs index 03a1421c2..d5015ff2b 100644 --- a/src/Samples/DesktopClient/Program.cs +++ b/src/Samples/DesktopClient/Program.cs @@ -6,21 +6,25 @@ namespace DesktopClient { class Program { - private static readonly string s_hostname = "localhost"; - - static void Main() + /// + /// use commanline argument localhost + /// or something similar to indicate the WCF Server hostname + /// + static void Main(string[] args) { - Settings settings = new Settings().SetDefaults(s_hostname); + string hostname = args.Length >= 1 ? args[0] : null; + + Console.Title = "WCF .Net Framework Client"; + Settings settings = ClientLogic.BuildClientSettings(hostname); void log(string value) => Console.WriteLine(value); - ClientLogic.CallUsingWcf(settings, log); + ClientLogic.InvokeEchoServiceUsingWcf(settings, log); - string rawSoapResponse = ClientLogic.CallUsingWebRequest(settings.basicHttpAddress); + string rawSoapResponse = ClientLogic.InvokeEchoServiceUsingWebRequest(settings.basicHttpAddress); Console.WriteLine($"Http SOAP Response:\n{rawSoapResponse}"); Console.WriteLine("Hit enter to exit"); Console.ReadLine(); } - } } diff --git a/src/Samples/DesktopServer/Program.cs b/src/Samples/DesktopServer/Program.cs index be73174a4..d96689b62 100644 --- a/src/Samples/DesktopServer/Program.cs +++ b/src/Samples/DesktopServer/Program.cs @@ -40,7 +40,7 @@ private static void LogHostUrls(ServiceHost host) static void Main() { - var hostEchoService = ConfigureWcfHost("EchoService"); + ServiceHost hostEchoService = ConfigureWcfHost("EchoService"); hostEchoService.Open(); diff --git a/src/Samples/NetCoreClient/NetCoreClient.csproj b/src/Samples/NetCoreClient/NetCoreClient.csproj index f7b457cc6..7e361b251 100644 --- a/src/Samples/NetCoreClient/NetCoreClient.csproj +++ b/src/Samples/NetCoreClient/NetCoreClient.csproj @@ -14,6 +14,7 @@ + diff --git a/src/Samples/NetCoreClient/Program.cs b/src/Samples/NetCoreClient/Program.cs index 70da52e4d..a0db1e23c 100644 --- a/src/Samples/NetCoreClient/Program.cs +++ b/src/Samples/NetCoreClient/Program.cs @@ -6,16 +6,21 @@ namespace NetCoreClient { class Program { - private static readonly string s_hostname = "localhost"; - - static void Main() + /// + /// use commanline argument localhost + /// or something similar to indicate the WCF Server hostname + /// + static void Main(string[] args) { - Settings settings = new Settings().SetDefaults(s_hostname, "EchoService"); + string hostname = args.Length >= 1 ? args[0] : null; + + Console.Title = "WCF .Net Core Client"; + Settings settings = ClientLogic.BuildClientSettings(hostname); static void log(string value) => Console.WriteLine(value); - ClientLogic.CallUsingWcf(settings, log); + ClientLogic.InvokeEchoServiceUsingWcf(settings, log); - string rawSoapResponse = ClientLogic.CallUsingWebRequest(settings.basicHttpAddress); + string rawSoapResponse = ClientLogic.InvokeEchoServiceUsingWebRequest(settings.basicHttpAddress); Console.WriteLine($"Http SOAP Response:\n{rawSoapResponse}"); Console.WriteLine("Hit enter to exit"); diff --git a/src/Samples/StandardClient/ClientLogic.cs b/src/Samples/StandardClient/ClientLogic.cs index 959bdcee9..354945af1 100644 --- a/src/Samples/StandardClient/ClientLogic.cs +++ b/src/Samples/StandardClient/ClientLogic.cs @@ -9,8 +9,18 @@ namespace StandardClient { public class ClientLogic { + public static Settings BuildClientSettings(string hostname) + { + const string s_hostname = "localhost"; + + string title = Console.Title; + if (string.IsNullOrWhiteSpace(hostname)) hostname = s_hostname; + Console.WriteLine(title + " - " + hostname); + Settings settings = new Settings().SetDefaults(hostname, "EchoService"); + return settings; + } - public static void CallUsingWcf( + public static void InvokeEchoServiceUsingWcf( Settings settings, Action log) { @@ -85,7 +95,7 @@ void RunExampleWsHttpsTransportWithMessageCredential () /// Creates a basic web request to the specified endpoint, /// sends the SOAP request and reads the response /// - public static string CallUsingWebRequest(Uri address) + public static string InvokeEchoServiceUsingWebRequest(Uri address) { string _soapEnvelopeContent = @" diff --git a/src/Samples/StandardClient/IEchoService.cs b/src/Samples/StandardClient/IEchoService.cs index 5e510a7a6..34dba9f66 100644 --- a/src/Samples/StandardClient/IEchoService.cs +++ b/src/Samples/StandardClient/IEchoService.cs @@ -6,14 +6,8 @@ namespace Contract [DataContract] public class EchoFault { - private string text; - [DataMember] - public string Text - { - get { return text; } - set { text = value; } - } + public string Text { get; set; } } [ServiceContract]