// Note: run LINQPad as administrator void Main() { var baseAddress = new Uri("http://localhost:9000/hello"); try { // Create the ServiceHost using (var host = new ServiceHost(typeof(HelloWorldService), baseAddress)) { // Enable metadata publishing var serviceMetadataBehavior = new ServiceMetadataBehavior { HttpGetEnabled = false, MetadataExporter = { PolicyVersion = PolicyVersion.Policy15 } }; host.Description.Behaviors.Add(serviceMetadataBehavior); // Retrieve the ServiceDebugBehavior to include exception // details raised from the service var serviceDebugBehavior = (ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]; serviceDebugBehavior.IncludeExceptionDetailInFaults = true; // Open the ServiceHost to start listening for messages. Since // no endpoints are explicitly configured, the runtime will create // one endpoint per base address for each service contract implemented // by the service. try { host.Open(); } catch (AddressAccessDeniedException exception) { ShowHowToFixAndRethrow(exception); } Console.WriteLine("The service is ready at {0}", baseAddress); var client = new HelloWorldClient(new BasicHttpBinding(), new EndpointAddress(baseAddress)); var proxy = client.ChannelFactory.CreateChannel(new EndpointAddress(baseAddress)); Console.WriteLine(proxy.SayHello("Bill")); client.Close(); } } catch (CommunicationObjectFaultedException exception) { var message = "Run LINQPad as administrator!"; throw new CommunicationObjectFaultedException(message, exception); } } public static void ShowHowToFixAndRethrow(AddressAccessDeniedException innerException) { const string HelpLink = "http://stackoverflow.com/questions/885744/wcf-servicehost-access-rights"; var message = "See here how to solve the exception {0}{1}" + string.Empty + "{0}{0}" + "Launch the command:{0}" + "netsh http add urlacl url=http://+:9000/hello user=mylocaluser"; message = message.FormatEx(Environment.NewLine, HelpLink); var exception = new AddressAccessDeniedException(message, innerException) { HelpLink = HelpLink }; throw exception; } public class HelloWorldClient : ClientBase<IHelloWorldService>, IHelloWorldService { public HelloWorldClient(Binding binding, EndpointAddress endpointAddress) : base(binding, endpointAddress) { } public string SayHello(string name) { return this.Channel.SayHello(name); } } public class HelloWorldService : IHelloWorldService { public string SayHello(string name) { return string.Format("Hello, {0}", name); } } [ServiceContract] public interface IHelloWorldService { [OperationContract] string SayHello(string name); } public static class StringExtensions { public static string FormatEx(this string format, params object[] args) { return string.Format(format, args); } }