static void Main() { //<snippet1> // 3rd Procedure: // Creating a binding and using it in a service // To run using config, comment the following lines, and uncomment out the code // following this section WSHttpBinding b = new WSHttpBinding(SecurityMode.Message); b.Security.Message.ClientCredentialType = MessageCredentialType.Windows; EndpointAddress ea = new EndpointAddress("Http://localhost:8036/SecuritySamples/secureCalculator"); CalculatorClient cc = new CalculatorClient(b, ea); cc.Open(); // Now call the service and display the results // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = cc.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Closing the client gracefully closes the connection and cleans up resources. cc.Close(); //</snippet1> }
static void Main2() { //<snippet2> // 4th Procedure: // Using config instead of the binding-related code // In this case, use a binding name from a configuration file generated by the // SvcUtil.exe tool to create the client. Omit the binding and endpoint address // because that information is provided by the configuration file. CalculatorClient cc = new CalculatorClient("ICalculator_Binding"); cc.Open(); // Now call the service and display the results // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = cc.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Closing the client gracefully closes the connection and cleans up resources. cc.Close(); //</snippet2> }
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 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(); }