Created
December 27, 2011 16:50
-
-
Save JoanComasFdz/1524329 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 <YOUR_SERVICE_NAMESPACE>; | |
namespace <YOUR_APP_NAMESPACE> | |
{ | |
/// <summary> | |
/// This class receives the callbacks from the server. | |
/// </summary> | |
public class CallbackGetter : <YOUR_SERVICE_CALLBACK_INTERFACE> | |
{ | |
/// <summary> | |
/// This method will be invoke from the server. | |
/// </summary> | |
/// <param name="sms">The message sended by the server.</param> | |
public void ServerToClient(string sms) | |
{ | |
Console.WriteLine(string.Format("Callback received: {0}", sms)); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Service1Client client = null; | |
try | |
{ | |
// Create the client with callbacks | |
client = new <YOUR_SERVICE_CLIENT>(new InstanceContext(new CallbackGetter())); | |
// Test the client | |
string result = client.Test(101); | |
// Wiat for results | |
Console.WriteLine(string.Format("Result: {0}", result)); | |
Console.WriteLine("Press [ENTER] to exit."); | |
Console.ReadLine(); | |
} | |
catch (Exception ex) | |
{ | |
// Opus an error occurred | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(string.Format("Error:\r\n{0}:\r\n{1}\r\n", ex.GetType(), ex.Message)); | |
Console.ReadLine(); | |
}finally | |
{ | |
// Close allways. | |
if (client!= null&& | |
client.State != CommunicationState.Closing && | |
client.State != CommunicationState.Closing) | |
client.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment