Created
October 31, 2013 06:19
-
-
Save llCorvinSll/7245042 to your computer and use it in GitHub Desktop.
Create Lazy instance of object which requires setup after creating
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
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