private void UnsecuredTcp() { //<snippet2> // Create an instance of the NetTcpBinding and set the // security mode to none. NetTcpBinding myBinding = new NetTcpBinding(); myBinding.Security.Mode = SecurityMode.None; // Create the address string, or get it from configuration. string tcpUri = "net.tcp://machineName:8008/Calculator"; // Create an endpoint address with the address. EndpointAddress myEndpointAddress = new EndpointAddress(tcpUri); // Create an instance of the WCF client. The client // code was generated using the Svcutil.exe tool. CalculatorClient cc = new CalculatorClient(myBinding, myEndpointAddress); try { cc.Open(); //</snippet2> // Begin using the calculator. Console.WriteLine(cc.Divide(100, 2)); // Close the client. cc.Close(); } catch (TimeoutException tex) { Console.WriteLine(tex.Message); cc.Abort(); } catch (CommunicationException cex) { Console.WriteLine(cex.Message); cc.Abort(); } finally { Console.WriteLine("Closed the client"); Console.ReadLine(); } }
private void UnsecuredHttp() { //<snippet1> // Create an instance of the BasicHttpBinding. // By default, there is no security. BasicHttpBinding myBinding = new BasicHttpBinding(); // Create the address string, or get it from configuration. string httpUri = "http://localhost/Calculator"; // Create an endpoint address with the address. EndpointAddress myEndpoint = new EndpointAddress(httpUri); // Create an instance of the WCF client. The client // code was generated using the Svcutil.exe tool. CalculatorClient cc = new CalculatorClient(myBinding, myEndpoint); try { cc.Open(); // Begin using the calculator. Console.WriteLine(cc.Divide(100, 2)); // Close the client. cc.Close(); } catch (TimeoutException tex) { Console.WriteLine(tex.Message); cc.Abort(); } catch (CommunicationException cex) { Console.WriteLine(cex.Message); cc.Abort(); } finally { Console.WriteLine("Closed the client"); Console.ReadLine(); } //</snippet1> }
static void AInvocateCalclatorServiceViaConfiguration() { Console.WriteLine("Invocate self-host calculator service "); #region Invocate Self-host service using (CalculatorClient calculator_http = new CalculatorClient("selfHostEndpoint_http")) { using (CalculatorClient calculator_tcp = new CalculatorClient("selfHostEndpoint_tcp")) { try { Console.WriteLine("Begin to invocate calculator service via http transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator_http.Add(1, 2)); Console.WriteLine("Begin to invocate calculator service via tcp transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator_tcp.Add(1, 2)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } #endregion Console.WriteLine("\n\nInvocate IIS-host calculator service "); #region Invocate IIS-host service using (CalculatorClient calculator = new CalculatorClient("iisHostEndpoint")) { try { Console.WriteLine("Begin to invocate calculator service via http transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator.Add(1, 2)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } #endregion }
static void InvocateCalclatorServiceViaConfiguration() { Binding httpBinding = new BasicHttpBinding(); Binding tcpBinding = new NetTcpBinding(); EndpointAddress httpAddress = new EndpointAddress("http://localhost:8888/generalCalculator"); EndpointAddress tcpAddress = new EndpointAddress("net.tcp://localhost:9999/generalCalculator"); EndpointAddress httpAddress_iisHost = new EndpointAddress("http://localhost/wcfservice/GeneralCalculatorService.svc"); Console.WriteLine("Invocate self-host calculator service "); using (CalculatorClient calculator_http = new CalculatorClient(httpBinding, httpAddress)) { using (CalculatorClient calculator_tcp = new CalculatorClient(tcpBinding, tcpAddress)) { try { Console.WriteLine("Begin to invocate calculator service via http transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator_http.Add(1, 2)); Console.WriteLine("Begin to invocate calculator service via tcp transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator_tcp.Add(1, 2)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } Console.WriteLine("\n\nInvocate IIS-host calculator service "); using (CalculatorClient calculator = new CalculatorClient(httpBinding, httpAddress_iisHost)) { try { Console.WriteLine("Begin to invocate calculator service via http transport "); Console.WriteLine("x + y = {2} where x = {0} and y = {1}", 1, 2, calculator.Add(1, 2)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
static void Main(string[] args) { //Step 1: Create an endpoint address and an instance of the WCF Client. CalculatorClient client = new CalculatorClient(); // Step 2: Call the service operations. // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3: Closing the client gracefully closes the connection and cleans up resources. client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Client Connecting..."); CalculatorClient client = new CalculatorClient(); client.Open(); Console.Write("Adding 2 + 2 - Answer: "); Console.WriteLine(client.Add(2, 2)); Console.Write("Subtracting 2 - 2 - Answer: "); Console.WriteLine(client.Subtract(2, 2)); Console.Write("Dividing 2 / 2 - Answer: "); Console.WriteLine(client.Divide(2, 2)); Console.Write("Multiplying 2 * 2 - Answer: "); Console.WriteLine(client.Multiply(2, 2)); client.Close(); Console.ReadLine(); }
static void Main(string[] args) { Action <string> PrintTitle = (title) => { Console.WriteLine(); Console.WriteLine(new String('=', 70)); Console.WriteLine(title); Console.WriteLine(new String('=', 70)); }; CalculatorClient client = new CalculatorClient(); PrintTitle("Adding Example"); var result = client.Add(50, 50); Console.WriteLine($"50 + 50 = {result}"); PrintTitle("Subtraction Example"); var subresult = client.Subtract(500, 50); Console.WriteLine($"500 - 50 = {subresult}"); PrintTitle("Multiplication Example"); var mulresult = client.Multiply(25, 25); Console.WriteLine($"25 * 25 = {mulresult}"); PrintTitle("Division Example"); var divresult = client.Divide(500, 50); Console.WriteLine($"500 / 50 = {divresult}"); client.Close(); }
static void Main(string[] args) { NetNamedPipeBinding binding = new NetNamedPipeBinding(); EndpointAddress address = new EndpointAddress( "net.pipe://localhost/ServiceModelSamples/Service/CalculatorService"); // Step 1: Create instance of the WCF Client. CalculatorClient client = new CalculatorClient(binding, address); try { Application.Run(new CalculatorForm(client)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Application.Exit(); client.Close(); } }
public CalculatorForm(CalculatorClient client) { this.client = client; InitializeComponent(); }
//</snippet26> //<snippet27> public void snippet27(CalculatorClient client) { client.ClientCredentials.SupportInteractive = false; }
//</snippet25> //<snippet26> public void snippet26(CalculatorClient client) { PeerCredential peercred = client.ClientCredentials.Peer; }
//</snippet23> //<snippet24> public void snippet24(CalculatorClient cc) { X509CertificateRecipientClientCredential rcc = cc.ClientCredentials.ServiceCertificate; X509Certificate2 cert = rcc.DefaultCertificate; }
//</snippet20> //<snippet21> public void snippet21(CalculatorClient cc) { X509CertificateRecipientClientCredential rcc = cc.ClientCredentials.ServiceCertificate; X509ServiceCertificateAuthentication xauth = rcc.Authentication; }
private void Run() { //<snippet31> //<snippet30> WSHttpBinding b = new WSHttpBinding(); EndpointAddress ea = new EndpointAddress("http://localhost/Calculator"); CalculatorClient client = new CalculatorClient(b, ea); //<snippet9> IssuedTokenClientCredential itcc = client.ClientCredentials.IssuedToken; //</snippet9> //</snippet30> //<snippet10> itcc.LocalIssuerAddress = new EndpointAddress("http://fabrikam.com/sts"); //</snippet10> //</snippet31> AddressHeader a1 = AddressHeader.CreateAddressHeader("Hello", "World", "hello"); AddressHeader[] addressHeaders = new AddressHeader[] { a1 }; //<snippet11> itcc.LocalIssuerAddress = new EndpointAddress(new Uri("http://fabrikam.com/sts"), addressHeaders); //</snippet11> //<snippet12> itcc.LocalIssuerAddress = new EndpointAddress( new Uri("http://fabrikam.com/sts"), EndpointIdentity.CreateDnsIdentity("fabrikam.com"), addressHeaders); //</snippet12> //<snippet13> itcc.LocalIssuerBinding = new WSHttpBinding("LocalIssuerBinding"); //</snippet13> //<snippet32> SynchronousReceiveBehavior myEndpointBehavior = new SynchronousReceiveBehavior(); //<snippet14> itcc.LocalIssuerChannelBehaviors.Add(myEndpointBehavior); //</snippet14> //</snippet32> //<snippet15> itcc.MaxIssuedTokenCachingTime = new TimeSpan(0, 10, 0); //</snippet15> //<snippet16> itcc.IssuedTokenRenewalThresholdPercentage = 80; //</snippet16> //<snippet17> itcc.DefaultKeyEntropyMode = SecurityKeyEntropyMode.ServerEntropy; //</snippet17> //<snippet33> //<snippet18> X509CertificateRecipientClientCredential rcc = client.ClientCredentials.ServiceCertificate; //</snippet18> X509Certificate2 cert = new X509Certificate2(); //<snippet19> rcc.ScopedCertificates.Add(new Uri("http://fabrikam.com/sts"), cert); //</snippet19> //</snippet33> }
static void Main(string[] args) { CalculatorClient client = new CalculatorClient(); int n = 1; while (n == 1) { try { Console.WriteLine("Type first argument:"); int inputFirstArgument = int.Parse(Console.ReadLine()); Console.WriteLine("Type the operation type(+, -, *, /)"); string typeOfOperation = Console.ReadLine(); Console.WriteLine("Type second argument:"); int inputSecondArgument = int.Parse(Console.ReadLine()); switch (typeOfOperation) { case "+": Console.WriteLine("Answer: {0}", client.Addition(inputFirstArgument, inputSecondArgument)); break; case "-": Console.WriteLine("Answer: {0}", client.Substraction(inputFirstArgument, inputSecondArgument)); break; case "*": Console.WriteLine("Answer: {0}", client.Multiplication(inputFirstArgument, inputSecondArgument)); break; case "/": Console.WriteLine("Answer: {0}", client.Division(inputFirstArgument, inputSecondArgument)); break; } } catch (TimeoutException e) { Console.WriteLine("The service operation timed out. " + e.Message); } catch (FaultException <CalculatorService.CustomFaultExeption> e) { Console.WriteLine("Message: {0}, Description: {1}", e.Detail.Message, e.Detail.Description); } catch (FaultException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Continue? Y or N "); string d = Console.ReadLine(); if (d == "N") { n = 0; client.Close(); } } }