Created
September 12, 2013 01:06
-
-
Save bjcull/6532034 to your computer and use it in GitHub Desktop.
Dynamically create https endpoints for configurationless WCF
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
public class HttpsServiceHostFactory : ServiceHostFactory | |
{ | |
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) | |
{ | |
ServiceHost host = new ServiceHost(serviceType, baseAddresses); | |
foreach (Uri baseAddress in baseAddresses) | |
{ | |
BasicHttpBinding binding = CreateSoapBinding(baseAddress); | |
int defaultObjectGraphSize = 100000; | |
int objectGraphSize; | |
if (!int.TryParse(ConfigurationManager.AppSettings["ObjectGraphSize"], out objectGraphSize)) | |
{ | |
objectGraphSize = defaultObjectGraphSize; | |
} | |
ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceType.GetInterfaces()[0], binding, baseAddress); | |
// This section allows you to return larger XML responses. | |
// In this case a list of type payment with quite a few records. | |
foreach (var op in endpoint.Contract.Operations) | |
{ | |
var dataContractBehaviour = op.Behaviors.Find<DataContractSerializerOperationBehavior>(); | |
if (dataContractBehaviour != null) | |
{ | |
dataContractBehaviour.MaxItemsInObjectGraph = objectGraphSize; | |
} | |
} | |
} | |
if (HasHttpEndpoint(baseAddresses)) | |
{ | |
if (host.Description.Behaviors.Contains(typeof(ServiceMetadataBehavior))) | |
{ | |
var smb = (ServiceMetadataBehavior)host.Description.Behaviors[typeof(ServiceMetadataBehavior)]; | |
smb.HttpGetEnabled = true; | |
} | |
else | |
{ | |
var smb = new ServiceMetadataBehavior(); | |
smb.HttpGetEnabled = true; | |
host.Description.Behaviors.Add(smb); | |
} | |
} | |
if (HasHttpsEndpoint(baseAddresses)) | |
{ | |
if (host.Description.Behaviors.Contains(typeof(ServiceMetadataBehavior))) | |
{ | |
var smb = (ServiceMetadataBehavior)host.Description.Behaviors[typeof(ServiceMetadataBehavior)]; | |
smb.HttpsGetEnabled = true; | |
} | |
else | |
{ | |
var smb = new ServiceMetadataBehavior(); | |
smb.HttpsGetEnabled = true; | |
host.Description.Behaviors.Add(smb); | |
} | |
} | |
return host; | |
} | |
private bool HasHttpsEndpoint(Uri[] baseAddresses) | |
{ | |
return baseAddresses.Any(b => b.Scheme == Uri.UriSchemeHttps); | |
} | |
private bool HasHttpEndpoint(Uri[] baseAddresses) | |
{ | |
return baseAddresses.Any(b => b.Scheme == Uri.UriSchemeHttp); | |
} | |
private BasicHttpBinding CreateSoapBinding(Uri baseAddress) | |
{ | |
BasicHttpBinding binding; | |
if (baseAddress.Scheme == Uri.UriSchemeHttps) | |
{ | |
binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); | |
} | |
else | |
{ | |
binding = new BasicHttpBinding(); | |
} | |
return binding; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment