' This string uses a function to prepend the computer name at run time.
Dim addressTCP As String = String.Format("net.tcp://{0}:8036/Calculator", _
System.Net.Dns.GetHostEntry("").HostName)
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
' You must create an array of URI objects to have a base address.
Dim a As New Uri(addressTCP)
Dim baseAddresses() As Uri = {a}
' Create the ServiceHost. The service type (Calculator) is not
' shown here.
Dim sh As New ServiceHost(GetType(Calculator), baseAddresses)
' Add an endpoint to the service. Insert the thumbprint of an X.509
' certificate found on your computer.
Dim c As Type = GetType(ICalculator)
sh.AddServiceEndpoint(c, b, "MyCalculator")
sh.Credentials.ServiceCertificate.SetCertificate( _
StoreLocation.LocalMachine, _
StoreName.My, _
X509FindType.FindBySubjectName, _
"contoso.com")
' This next line is optional. It specifies that the client's certificate
' does not have to be issued by a trusted authority, but can be issued
' by a peer if it is in the Trusted People store. Do not use this setting
' for production code. The default is PeerTrust, which specifies that
' the certificate must originate from a trusted certifiate authority.
' sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
' X509CertificateValidationMode.PeerOrChainTrust
Try
sh.Open()
Dim address As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening @ {0}", address)
Console.WriteLine("Press enter to close the service")
Console.ReadLine()
sh.Close()
Catch ce As CommunicationException
Console.WriteLine("A commmunication error occurred: {0}", ce.Message)
Console.WriteLine()
Catch exc As System.Exception
Console.WriteLine("An unforseen error occurred: {0}", exc.Message)
Console.ReadLine()
End Try