Created
December 28, 2011 09:05
-
-
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.
This file contains hidden or 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
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