Skip to content

Instantly share code, notes, and snippets.

@DavidForster
Last active March 1, 2016 21:19
Show Gist options
  • Save DavidForster/a35f4824a8d9f59f0cba to your computer and use it in GitHub Desktop.
Save DavidForster/a35f4824a8d9f59f0cba to your computer and use it in GitHub Desktop.
Configuration-less Core Service Client
static ICoreService GetClient(string hostname, string username, string password)
{
var binding = new BasicHttpBinding()
{
MaxBufferSize = int.MaxValue,
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxStringContentLength = int.MaxValue,
MaxArrayLength = int.MaxValue,
},
Security = new BasicHttpSecurity()
{
Mode = BasicHttpSecurityMode.TransportCredentialOnly,
Transport = new HttpTransportSecurity()
{
ClientCredentialType = HttpClientCredentialType.Windows,
}
}
};
hostname = string.Format("{0}{1}{2}", hostname.StartsWith("http") ? "" : "http://", hostname, hostname.EndsWith("/") ? "" : "/");
var endpoint = new EndpointAddress(hostname + "/webservices/CoreService2012.svc/basicHttp");
var factory = new ChannelFactory<ICoreService>(binding, endpoint);
factory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password);
return factory.CreateChannel();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment