public static void Main() { // Picks up configuration from the config file. SampleServiceClient wcfClient = new SampleServiceClient(); try { // Making calls. Console.WriteLine("Enter the greeting to send: "); string greeting = Console.ReadLine(); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); // Done with service. wcfClient.Close(); Console.WriteLine("Done!"); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); wcfClient.Abort(); Console.Read(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); wcfClient.Abort(); Console.Read(); } }
void Run() { // Picks up configuration from the config file. using (SampleServiceClient wcfClient = new SampleServiceClient()) { try { // Make asynchronous call. Console.WriteLine("Enter the greeting to send asynchronously: "); string greeting = Console.ReadLine(); IAsyncResult waitResult = wcfClient.BeginSampleMethod(greeting, new AsyncCallback(ProcessResponse), wcfClient); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Sent asynchronous method. Waiting on the response."); waitResult.AsyncWaitHandle.WaitOne(); Console.ResetColor(); // Make synchronous call. Console.WriteLine("Enter the greeting to send synchronously: "); greeting = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("Response: "); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(wcfClient.SampleMethod(greeting)); Console.ResetColor(); // Make synchronous call on asynchronous method. Console.WriteLine("Enter the greeting to send synchronously to async service operation: "); greeting = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("Response: "); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(wcfClient.ServiceAsyncMethod(greeting)); Console.ResetColor(); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); // Done with service. wcfClient.Close(); Console.WriteLine("Done!"); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.Read(); wcfClient.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); Console.Read(); wcfClient.Abort(); } } }
void Run() { // Picks up configuration from the config file. // <snippet4> SampleServiceClient wcfClient = new SampleServiceClient(new InstanceContext(this)); try { using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel)) { MessageHeader header = MessageHeader.CreateHeader( "Service-Bound-CustomHeader", "http://Microsoft.WCF.Documentation", "Custom Happy Value." ); OperationContext.Current.OutgoingMessageHeaders.Add(header); // Making calls. Console.WriteLine("Enter the greeting to send: "); string greeting = Console.ReadLine(); //Console.ReadLine(); header = MessageHeader.CreateHeader( "Service-Bound-OneWayHeader", "http://Microsoft.WCF.Documentation", "Different Happy Value." ); OperationContext.Current.OutgoingMessageHeaders.Add(header); // One-way wcfClient.Push(greeting); this.wait.WaitOne(); // Done with service. wcfClient.Close(); Console.WriteLine("Done!"); Console.ReadLine(); } } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.ReadLine(); wcfClient.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); Console.ReadLine(); wcfClient.Abort(); } // </snippet4> }
public static void Main() { // Picks up configuration from the configuration file. SampleServiceClient wcfClient = new SampleServiceClient(); try { // Making calls. Console.WriteLine("Enter the greeting to send: "); string greeting = Console.ReadLine(); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); wcfClient.Abort(); Console.ReadLine(); } // Catch the contractually specified SOAP fault raised here as an exception. catch (FaultException <GreetingFault> greetingFault) { Console.WriteLine(greetingFault.Detail.Message); Console.Read(); wcfClient.Abort(); } // Catch unrecognized faults. This handler receives exceptions thrown by WCF // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults // is set to true. catch (FaultException faultEx) { Console.WriteLine("An unknown exception was received. " + faultEx.Message + faultEx.StackTrace ); Console.Read(); wcfClient.Abort(); } // Standard communication fault handler. catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace); Console.Read(); wcfClient.Abort(); } }
static void Main(string[] args) { SampleServiceClient client = new SampleServiceClient(); try { for (int i = 0; i < 10; i++) { // This will post (In the service): // "Hey! Test #x" until an exception happens or it goes 10 times without an exception. string test = client.SampleMethod("Test #" + i); Console.WriteLine(test); } } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.ReadLine(); client.Abort(); } catch (FaultException<GreetingFault> greetingFault) { Console.WriteLine(greetingFault.Detail.Message); Console.ReadLine(); client.Abort(); } catch (FaultException unknownFault) { Console.WriteLine("An unknown exception was received. " + unknownFault.Message); Console.ReadLine(); client.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace); Console.ReadLine(); client.Abort(); } finally { client.Close(); } }
public string Ask(string question) { SampleServiceClient proxy = null; string response = null; int callCount = 0; bool callCompleted = false; bool shouldRetry = true; while (!callCompleted && callCount < MaxRetryCount && shouldRetry) { callCount++; try { proxy = new SampleServiceClient(); var svcResponse = proxy.AskQuestion(new SampleServiceRequest { Question = "Are we nearly there yet?" }); if (svcResponse != null) response = svcResponse.Answer; callCompleted = true; } catch (EndpointNotFoundException ex) { if (callCount >= MaxRetryCount && !callCompleted) { throw; } else { Console.WriteLine("Can't find service - going to sit here patiently for 2seconds before trying again."); Thread.Sleep(2000); // Do nothing } } catch (Exception ex) { shouldRetry = false; } finally { if (proxy != null) { try { if (proxy.State == CommunicationState.Opened) proxy.Close(); else if (proxy.State == CommunicationState.Faulted) proxy.Abort(); } catch (CommunicationException) { proxy.Abort(); } catch (TimeoutException) { proxy.Abort(); } } } } return response; }
/// <summary> /// Wrapper around our service call to ensure it is being correctly disposed /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="serviceCall"></param> /// <returns></returns> private TResult MakeSampleServiceCall <TResult>(Func <SampleService.SampleServiceClient, TResult> serviceCall) { SampleServiceClient client = null; try { client = new SampleServiceClient(); var result = serviceCall(client); client.Close(); return(result); } catch { if (client != null) { client.Abort(); } throw; } }