Skip to content

Instantly share code, notes, and snippets.

@JoanComasFdz
Created December 28, 2011 09:05
Show Gist options
  • Save JoanComasFdz/1527200 to your computer and use it in GitHub Desktop.
Save JoanComasFdz/1527200 to your computer and use it in GitHub Desktop.
Basic console program to acces a WCF service via using username and password.
static void Main(string[] args)
{
Service1Client client = null;
try
{
// Create the client
// It will use the settings in the app.config file
client = new Service1Client();
// Set credentials
client.ClientCredentials.UserName.UserName = "USER_PASSWORD";
client.ClientCredentials.UserName.Password = "USER_PASSWORD";
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
// Call a method.
string result = client.Test(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));
if (ex.InnerException != null)
{
Console.WriteLine(string.Format("Inner Error:\r\n{0}:\r\n{1}", ex.InnerException.GetType(), ex.InnerException.Message));
}
Console.ReadLine();
}
finally
{
// Clean memory
if (client != null &&
client.State != CommunicationState.Closing &&
client.State != CommunicationState.Closed &&
client.State != CommunicationState.Faulted)
((IDisposable)client).Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment