Skip to content

Instantly share code, notes, and snippets.

@llCorvinSll
Created October 31, 2013 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llCorvinSll/7245042 to your computer and use it in GitHub Desktop.
Save llCorvinSll/7245042 to your computer and use it in GitHub Desktop.
Create Lazy instance of object which requires setup after creating
class Client
{
public static TClient CreateInst<TClient, TInterface>(string url, string domain, string username, string password)
where TClient : System.ServiceModel.ClientBase<TInterface>
where TInterface : class
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
EndpointAddress endpoint = new EndpointAddress(url);
TClient client = (TClient)Activator.CreateInstance(typeof(TClient), new object[] { binding, endpoint });
client.ClientCredentials.Windows.ClientCredential.Domain = domain;
client.ClientCredentials.Windows.ClientCredential.UserName = username;
client.ClientCredentials.Windows.ClientCredential.Password = password;
client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;
return client;
}
public static Lazy<TClient> CreateLazyInst<TClient, TInterface>(string url, string domain, string username, string password)
where TClient : System.ServiceModel.ClientBase<TInterface>
where TInterface : class
{
return new Lazy<TClient>(() =>
{
return CreateClient<TClient, TInterface>(url, domain, username, password);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment