0

To do the above using the config file I would do:

<endpoint
  address="...."
  binding="netTcpBinding"
  bindingConfiguration="MyBinding"
  contract="IService1">
  <identity>
    <servicePrincipalName value="name"/>
  </identity>
</endpoint>

But how do I add it to the below code?

Uri uri = new Uri("http://example.com/service");
ServiceHost host = new ServiceHost(typeof(Service1), uri);

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
host.AddServiceEndpoint(typeof(IService1), binding, uri);
host.Open();
abatishchev
  • 98,240
  • 88
  • 296
  • 433

1 Answers1

6

It's a bit cumbersome, you need to use the return value of the AddServiceEndpoint method and set it there:

ServiceEndpoint serviceEndpoint = host.AddServiceEndpoint(typeof(IService1), binding, uri);
EndpointAddress myEndpointAddress = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity("YourIdentity"));
serviceEndpoint.Address = myEndpointAddress;
Marc Wittke
  • 2,991
  • 2
  • 30
  • 45