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(); } }
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(); wcfClient.Open(); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); // Done with service. wcfClient.Close(); Console.WriteLine("Done!"); SampleServiceClient newclient = new SampleServiceClient(); newclient.Open(); Console.WriteLine("The service responded: " + newclient.SampleMethod(greeting)); Console.WriteLine("The service responded: " + newclient.SampleMethod(greeting)); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); // Done with service. newclient.Close(); ChannelFactory <ISampleServiceChannel> chFactory = new ChannelFactory <ISampleServiceChannel>("WSHttpBinding_ISampleService"); ISampleServiceChannel clientChannel = chFactory.CreateChannel(); clientChannel.Open(); Console.Read(); clientChannel.SampleMethod(greeting); clientChannel.SampleMethod(greeting); clientChannel.SampleMethod(greeting); clientChannel.Close(); Console.WriteLine("Done!"); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.Read(); } catch (FaultException <SampleFault> fault) { Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage); Console.Read(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); 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> }
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; } }
public static void Main() { // Picks up configuration from the config file. SampleServiceClient wcfClient = new SampleServiceClient(); try { // Add the client side behavior programmatically. wcfClient.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector()); // 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); Console.Read(); } catch (FaultException <SampleFault> fault) { Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage); Console.Read(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); Console.Read(); } }
static void Main() { // Get a client for the service SampleServiceClient wcfClient = new SampleServiceClient(); // Call the GetData method on the server string retstr = wcfClient.GetData(123); Console.WriteLine("GetData returns: " + retstr); // Create the CompositeType object from the service and execute // the GetDataUsingDataContract method on the server CompositeType obj = new CompositeType(); obj.BoolValue = true; obj.StringValue = "Hello WCF client"; CompositeType objret = wcfClient.GetDataUsingDataContract(obj); Console.WriteLine("GetDataUsingDataContract returns: " + objret.StringValue); Console.ReadLine(); // Close the connection to the server wcfClient.Close(); }
public static void Main() { // Picks up configuration from the config file. using (SampleServiceClient wcfClient = new SampleServiceClient()) { try { // Making calls. Console.Write("Enter the first name to send: "); string first = Console.ReadLine(); Console.Write("Enter the last name to send: "); string last = Console.ReadLine(); Console.Write("Enter a message: "); string msg = Console.ReadLine(); OriginalPerson person = new OriginalPerson(); person.lastName = last; person.firstName = first; person.Message = msg; person.additionalData = "extra data the service does not have."; person.Blob = new ArgumentException(); OriginalPerson replyPerson = wcfClient.SampleMethod(person); Console.WriteLine("The service responded: " + replyPerson.Message); Console.WriteLine("From \"{0}, {1}\".", replyPerson.firstName, replyPerson.lastName); if (replyPerson.additionalData == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The return additional data is null"); Console.ResetColor(); } else { Console.WriteLine("Any extra data: {0}.", replyPerson.additionalData.ToString()); } if (replyPerson.Blob != null) { Console.WriteLine("And the added object is: " + replyPerson.Blob.ToString()); } else { Console.WriteLine("There is no added data in the extra object property."); } 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); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); Console.Read(); } } }
public static void Main() { try { SampleServiceClient wcfClient; // Client has an endpoint for metadata EndpointAddress metaAddress = new EndpointAddress(new Uri("http://localhost:8080/SampleService/mex")); EndpointAddress httpGetMetaAddress = new EndpointAddress(new Uri("http://localhost:8080/SampleService?wsdl")); // <snippet1> // Get the endpoints for such a service ServiceEndpointCollection endpoints = MetadataResolver.Resolve(typeof(SampleServiceClient), metaAddress); Console.WriteLine("Trying all available WS-Transfer metadata endpoints..."); foreach (ServiceEndpoint point in endpoints) { if (point != null) { // Create a new wcfClient using retrieved endpoints. wcfClient = new SampleServiceClient(point.Binding, point.Address); Console.WriteLine( wcfClient.SampleMethod("Client used the " + point.Address.ToString() + " address.") ); wcfClient.Close(); } } // </snippet1> // <snippet2> // Get the endpoints for such a service using Http/Get request endpoints = MetadataResolver.Resolve(typeof(SampleServiceClient), httpGetMetaAddress.Uri, MetadataExchangeClientMode.HttpGet); Client.WriteParameters(endpoints); ISampleService serviceChannel; Console.WriteLine( "\r\nTrying all endpoints from HTTP/Get and with direct service channels..."); foreach (ServiceEndpoint point in endpoints) { if (point != null) { ChannelFactory <ISampleService> factory = new ChannelFactory <ISampleService>(point.Binding); factory.Endpoint.Address = point.Address; serviceChannel = factory.CreateChannel(); Console.WriteLine("Client used the " + point.Address.ToString() + " address."); Console.WriteLine( serviceChannel.SampleMethod( "Client used the " + point.Address.ToString() + " address." ) ); factory.Close(); } } // </snippet2> // <snippet3> // Get metadata documents. Console.WriteLine("URI of the metadata documents retreived:"); MetadataExchangeClient metaTransfer = new MetadataExchangeClient(httpGetMetaAddress.Uri, MetadataExchangeClientMode.HttpGet); metaTransfer.ResolveMetadataReferences = true; MetadataSet otherDocs = metaTransfer.GetMetadata(); foreach (MetadataSection doc in otherDocs.MetadataSections) { Console.WriteLine(doc.Dialect + " : " + doc.Identifier); } // </snippet3> Console.WriteLine("Press ENTER to exit..."); Console.ReadLine(); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); } catch (FaultException unkException) { Console.WriteLine(unkException.Message); Console.ReadLine(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); } }
public void Dispose() { _service?.Close(); (_service as IDisposable)?.Dispose(); }