a·gen·tic a·gil·i·ty

Creating WCF Service Host Programmatically

TL;DR; Explains how to programmatically create and configure a WCF Service Host in .NET, including base addresses, endpoints, bindings, and service behaviours for secure hosting.

Published on
2 minute read
Image
https://nkdagility.com/resources/z78UlmtJAzV
Subscribe

If you want to create a Windows Communication Foundation Service Host on the fly then you will need to first create a base address. I would recommend using the DNS host entry instead of the My.Computer.Name as I had many problems on the corporate network with [computername] not working with our proxy settings.

1Dim baseAddresses() As Uri = {New Uri(String.Format("http://{0}:{1}/TFSEventHandler/Queuer", System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).HostName, Port))}

Then you need to create the service host itself.

1Dim sh As New System.ServiceModel.ServiceHost(GetType(Services.QueuerService), baseAddresses)

Then set the service meta and debug behaviors so that you can both enable the MEX and remote exception handling.

 1' Set service meta behavior
 2Dim smb As ServiceMetadataBehavior = sh.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
 3If smb Is Nothing Then
 4  smb = New ServiceMetadataBehavior()
 5  smb.HttpGetEnabled = True
 6  sh.Description.Behaviors.Add(smb)
 7Else
 8  smb.HttpGetEnabled = True
 9End If
10' Set Service Debug Behavior
11Dim sdb As ServiceDebugBehavior = sh.Description.Behaviors.Find(Of ServiceDebugBehavior)()
12If sdb Is Nothing Then
13  sdb = New ServiceDebugBehavior()
14  sdb.IncludeExceptionDetailInFaults = True
15  sh.Description.Behaviors.Add(sdb)
16Else
17  sdb.IncludeExceptionDetailInFaults = True
18End If

Then comes the easy bit, adding the Endpoints. I have chosen to use a Secure wsHttpBinding as I am using Active Directory authentication and I want another level of security. Here I am creating a number of static end points, but also an endpoint for each of the Team Foundation Server SOAP Events, which uses the same code to handle each one, but you can determine the incoming URL for the event type.

1sh.Description.Endpoints.Clear()
2For Each EventType As Events.EventTypes In [Enum].GetValues(GetType(Events.EventTypes))
3    sh.AddServiceEndpoint(GetType(Services.Contracts.INotification), GetSecureWSHttpBinding, "Notification/" & EventType.ToString)
4Next
5sh.AddServiceEndpoint(GetType(Services.Contracts.ISubscriptions), GetSecureWSDualHttpBinding, "Subscriptions")
6sh.AddServiceEndpoint(GetType(Services.Contracts.ITeamServers), GetSecureWSDualHttpBinding, "TeamServers")
7sh.AddServiceEndpoint(GetType(Description.IMetadataExchange), GetSecureWSHttpBinding, "mex")

You will need to create the binding programmatically as well (see the GetSecureDualWSHttpBinding method referenced above) and you may need to set some specialist options. I needed to increase the size of some of the payloads to implement my service. I have chosen to use the same method to create the service on both the client and the server so I have included the ClientBaseAddress property to get around the problem on Windows of a “http://+:80 error if you have IIS installed.

1Dim Binding As New WSDualHttpBinding(WSDualHttpSecurityMode.Message)
2Binding.MaxReceivedMessageSize = 655360
3Binding.ReaderQuotas.MaxStringContentLength = 655360
4Binding.ReaderQuotas.MaxArrayLength = 655360
5Binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows
6Binding.Security.Message.NegotiateServiceCredential = True
7Binding.ClientBaseAddress = New System.Uri("http://" & System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).HostName & ":660")
8Binding.BypassProxyOnLocal = True

Using this in conjunction with the custom proxy creation will allow you to build versatile integrated services on the .NET platform.

You can find all of the code listed above @ http://www.codeplex.com/TFSEventHandler

Smart Classifications

Each classification [Concepts, Categories, & Tags] was assigned using AI-powered semantic analysis and scored across relevance, depth, and alignment. Final decisions? Still human. Always traceable. Hover to see how it applies.

Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

YearUp.org Logo

YearUp.org

Boeing Logo

Boeing

Milliman Logo

Milliman

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Philips Logo

Philips

Schlumberger Logo

Schlumberger

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Workday Logo

Workday

Akaditi Logo

Akaditi

Deliotte Logo

Deliotte

Alignment Healthcare Logo

Alignment Healthcare

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Genus Breeding Ltd Logo

Genus Breeding Ltd

Sage Logo

Sage

Ericson Logo

Ericson

Healthgrades Logo

Healthgrades

Trayport Logo

Trayport

Kongsberg Maritime Logo

Kongsberg Maritime

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Washington Department of Transport Logo

Washington Department of Transport

Royal Air Force Logo

Royal Air Force

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

Nottingham County Council Logo

Nottingham County Council

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

New Signature Logo

New Signature

Workday Logo

Workday

Freadom Logo

Freadom

Hubtel Ghana Logo

Hubtel Ghana

Slicedbread Logo

Slicedbread