Created
December 28, 2011 08:50
-
-
Save JoanComasFdz/1527162 to your computer and use it in GitHub Desktop.
Client for wcf service in console app basic code (using dual tcp to allow callbacks)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using Client_Service_with_callbacks_via_tcp.<YOUR_SERVICE_NAME>; | |
namespace Client_Service_with_callbacks_via_tcp | |
{ | |
class Program | |
{ | |
/// <summary> | |
/// Handles the callbacks from the server following | |
/// the contract of the service specification. | |
/// </summary> | |
class CallbackHandler : <YOUR_SERVICE_INTERFACE>Callback | |
{ | |
/// <summary> | |
/// This method is invoked from the server. | |
/// </summary> | |
/// <param name="sms">The message received from the server.</param> | |
public void Send(string sms) | |
{ | |
// Write message from server | |
Console.WriteLine(string.Format("[CALLBACK]: {0}", sms)); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Service1Client client = null; | |
try | |
{ | |
// Create the client, specifing who | |
// is going to handle the callbacks. | |
// It will use the settings in the app.config file | |
client = new Service1Client(new InstanceContext(new CallbackHandler())); | |
// Call a method. | |
string result = client.GetData(99); | |
// Show results. | |
Console.WriteLine(string.Format("Result: {0}", result)); | |
// Wait to exit | |
Console.WriteLine("Press [ENTER] to exit."); | |
Console.ReadLine(); | |
} | |
catch (Exception ex) | |
{ | |
// Oups | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(string.Format("/!\\ Error:\r\n{0}:\r\n{1}", ex.GetType(), ex.Message)); | |
Console.ReadLine(); | |
}finally | |
{ | |
// Clean memory | |
if(client !=null && | |
client.State != CommunicationState.Closing && | |
client.State != CommunicationState.Closed) | |
((IDisposable)client).Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment