marc_s
marc_s

Reputation: 754973

WCF self-hosting - dead in the water on Windows 10?

I have a really simple WCF service - this is the service contract:

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    string SayGreeting(string name);

    [OperationContract]
    string SayRudeGreeting(string name);
}

and this is the implementation:

public class HelloService : IHelloService
{
    public string SayGreeting(string name)
    {
        return "Well, hello there, " + name;
    }

    public string SayRudeGreeting(string name)
    {
        return string.Format("What do you want, {0} ?", name);
    }
}

That's not the interesting part :-) what I'm trying to do is self-host this simple service in a Windows command line app. I'm doing this with these lines of code:

Uri baseAddress = new Uri("http://localhost:9099");

using (ServiceHost host = new ServiceHost(typeof(HelloService), baseAddress))
{
    host.Open();

    Console.WriteLine("HelloService is up and running ....");
    Console.ReadKey();

    Console.WriteLine("HelloService - closing down...");
}

and I have a really simple, straightforward app.config for that host:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="DevDebug">
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="WcfService1.HelloService" 
                 behaviorConfiguration="DevDebug">
            <endpoint
                address="HelloService"
                binding="wsHttpBinding"
                contract="WcfService1.IHelloService" />
            <endpoint
                address="HelloService/mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>

Nothing fancy at all - just a regular wsHttpBinding endpoint at http://localhost:9099/HelloService and a MEX endpoint as well.

When I build this host and WCF service, and run the host within Visual Studio 2015 which I start "As Administrator", I get this odd message:

System.ServiceModel.AddressAccessDeniedException was unhandled
HResult=-2146233087
Message=HTTP could not register URL http://+:9099/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

?!?!?!?!? I'm the ADMINISTRATOR on my box - why wouldn't I be allowed to register this URL!?!?

My suspicion is that this is a new "feature" of Windows 10 (Professional - v1607) - because I'm pretty sure, this setup used to work just fine on my previous Windows 7 box.

OK - so I launch my WCF host from the command line, explicitly as administrator - and now the host comes up and everything seems fine, I can connect to it from the WCF Test Client, and my code seems to work just fine.

But if I now try to connect to it using either SoapUI or just IE 11, I keep getting another slew of error messages ("invalid page" etc.) that seem to indicate that this URL cannot be accessed.....

WHY!?!

What do I need to do in Windows 10 Professional to make WCF self-hosting work again!?

Thanks!

Upvotes: 1

Views: 986

Answers (1)

Hugh Proctor
Hugh Proctor

Reputation: 56

The reason for this is that you have to Register the URL. Basically, open up a Command Line window CMD (type CMD into Windows 10 search)

Then to Install Net Local Address use the following scripts: (copy and right click paste)

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

netsh http add urlacl url=http://+:8731/Design_Time_Addresses user=DOMAIN\user

Note: replace the +8731 with your URL and Design_Time_Addresses with your service name (or you can leave this out, but it helps to identify the URL - maybe you have more than one service from the same URL)

To Delete Net Local Address netsh http delete urlacl url=http://+:8731/Design_Time_Addresses

Then, you need to open up the port through your Windows Firewall.

In Windows Search put: Firewall On the left menu, Click 'Advance Settings' Then click the 'Inbound Settings' in the left menu.

In the right menu, click 'New Rule' .. this opens up a new window. Select 'Port' and click 'Next' In the textbox, put your URL (mine is 8731) and click 'Next' Keep the current selection of 'Allow the connection' and click 'Next' Keep all the checkboxes clicked (unless you know otherwise) and click 'Next'

Give it a good name relating to your application and add a description and click 'Finish'

You should now be able to link to the URL via a web browser.

If you want to serve an actual service out to the outside world, then you'll need to go to your router and set a Static IP address and open the Port through your Firewall also.

You can get to your router via 192.168.0.1 (depending upon your router)

Obviously, if your using a laptop then the second you take your laptop out of your flat then your service will no longer be available to the Outside World, so you really need a dedicated desktop machine to act as a server.

You can find out about Static IPs and Port Forwarding from Google searching and you can Google about your router and it's Firewall settings to allow your URL.

Hopefully, it'll all work out, if not, then just ask again.

Good luck and have fun.

This also works for RestApi's (Asp.Net WebApi), WCF services are usually used for Financial Service companies as they allow for more granular security protocols.

Upvotes: 1

Related Questions