204

I'm a beginner in WCF, but trying to improve my experience. And on the first step I faced the problem. I created the simplest WCF service. The listing of code: (all the code in one file)

using System;
using System.ServiceModel;

namespace EssentialWCF
{
    [ServiceContract]
    public interface IStockService
    {
        [OperationContract]
        double GetPrice(string ticker);
    }

    public class StockService : IStockService
    {
        public double GetPrice(string ticker)
        {
            return 94.85;
        }
    }

    class Service
    {
        static void Main(string[] args)
        {
            ServiceHost serviceHost = new ServiceHost(typeof(StockService),
                                                        new Uri("http://localhost:8000/HelloWCF"));

            serviceHost.AddServiceEndpoint(typeof(IStockService), new BasicHttpBinding());
            serviceHost.Open();

            Console.WriteLine("To continue press ENTER");

            serviceHost.Close();
        }
    }
}

That would be the service that give me a number via console. But debug give me the exception: (instead of number :) )

HTTP could not register URL http://+:8000/HelloWCF/. Your process does not have access rights to this namespace.

Have you ever faced the same situation? I will be glad to see every advice.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
valecoder
  • 2,055
  • 2
  • 13
  • 7
  • I'm confused. Is that HTML-escaped text supposed to be part of the code? –  Jan 05 '12 at 04:10
  • 1
    look here for other option [http://stackoverflow.com/questions/885744/wcf-servicehost-access-rights] – Devesh Jun 13 '14 at 08:12

12 Answers12

264

Unfortunately the link in the exception text, http://go.microsoft.com/fwlink/?LinkId=70353, is broken. However, it used to lead to http://msdn.microsoft.com/en-us/library/ms733768.aspx which explains how to set the permissions.

It basically informs you to use the following command:

netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user

You can get more help on the details using the help of netsh

For example: netsh http add ?

Gives help on the http add command.

Technobabble
  • 996
  • 9
  • 25
Bryan
  • 2,661
  • 1
  • 12
  • 2
  • 3
    This method is also useful for setting up the required permissions on an automated testing server. – Technobabble Jul 30 '13 at 21:26
  • 9
    Make sure to run the command from an Administrator Command prompt. – mbonness Jul 17 '16 at 16:00
  • 5
    If you're not on a domain, just use `COMPUTER_NAME\USER_NAME` in the above command. – dotNET Oct 13 '16 at 05:15
  • 2
    you can run this `netsh http add urlacl url=http://+:80/ user=DOMAIN\user` if you want to give the account access rights to any namespaces on the port. – Hamid Heydarian Jun 09 '17 at 03:54
  • should this be don on the client machine or an the server or both ? andwhat should be used in http:// ? the ip of the client or the server ? As usual the help is useless – GuidoG Jul 10 '17 at 11:22
  • What in the world is stopping WCF from opening a socket and self-hosting an HTTP server without a magic incantation to `netsh`? The linked page talks about IIS but I have no desire to use it. What's with the `+` sign? I assume this problem doesn't happen on all machines, since the WCF [tutorial](https://learn.microsoft.com/en-us/dotnet/framework/wcf/how-to-host-a-wcf-service-in-a-managed-application) doesn't mention the issue, but running the example code causes this exception on my machine. – Qwertie May 30 '19 at 23:01
  • Remember that the OP wanted to use port 8000; I've heard that ports over 1023 don't normally require any special permissions. – Qwertie Oct 16 '19 at 23:19
205

I closed Visual studio IDE and reopened it by right clicking on the Visual Studio icon and saying "Run as Administrator", Then when I ran the host , It worked!!!

John Royceton Arthur
  • 2,368
  • 1
  • 12
  • 8
  • 17
    First, this is bad practice although Visual Studio leaves little choice here. Second, the problem remains at other steps (e.g. deployed). See Bryan's answer or http://blogs.msdn.com/b/paulwh/archive/2007/05/04/addressaccessdeniedexception-http-could-not-register-url-http-8080.aspx – Stéphane Gourichon Aug 30 '13 at 10:30
  • 5
    Good grief! Friggin MS, making different functionality that fights itself. So much for 'least privilege'. – Dan Csharpster Sep 12 '13 at 20:56
  • 1
    Best practise or not, this is on MS. Why is it WCF/.NET is so special that it needs one more hoop to jump through? If I write a service in Java running on Windows, bang it works locally. If I want to make it public, I adjust firewall, switcher, router etc. – treehouse Dec 19 '15 at 19:56
  • 1
    -1. This is not the correct thing to do. You should grant explicit permission to a restricted user and use that. @Bryan's answer below is better. – Daniel James Bryars Dec 31 '15 at 12:26
  • 2
    -1. It's the same as reformatting hard drive when there is problem and saying that problem is solved. The answer is, as it was pointed out, to run netssh – Stan Bashtavenko Feb 17 '16 at 19:14
  • Thanks, that saved me a lot of time! – Lachezar Lalov Jun 25 '18 at 13:59
28

Right Click on Visual Studio > Run as Administrator > Open your project and run the service. This is a privilege related issue.

16

The simple thing you need to do is to close your Visual Studio environment and open it again by using 'Run as administrator'. It should now run successfully.

01F0
  • 1,228
  • 2
  • 19
  • 32
Mahesh.P
  • 271
  • 3
  • 4
15

You need some Administrator privilege to your account if your machine in local area network then you apply some administrator privilege to your User else you should start ide as Administrator...

Pramod Lawate
  • 167
  • 1
  • 4
  • 2
    This is what I needed to do for my WCF windows service. I was using a new domain account to run the service on a build machine. My service would start up and then throw that exception. Once I gave it administrator rights, it worked perfectly. – thehelix Dec 10 '14 at 20:27
  • This solved it for me. Just right-click the service, and in tab "log on" choose "Local System account" – Alexander Derck Nov 09 '15 at 15:10
11

You must give permission to your app for listening http requests. You can use this command in cmd for this purpose (open cmd Run As Administrator mode)

netsh http add urlacl url=http://+:8000/ user=Everyone

If your app is working other port, for example 9095, this command must be like as below:

netsh http add urlacl url=http://+:9095/ user=Everyone

And re-run your app, it should work. This way working for me.

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
6

In Windows Vista and later the HTTP WCF service stuff would cause the exception you mentioned because a restricted account does not have right for that. That is the reason why it worked when you ran it as administrator.

Every sensible developer must use a RESTRICTED account rather than as an Administrator, yet many people go the wrong way and that is precisely why there are so many applications out there that DEMAND admin permissions when they are not really required. Working the lazy way results in lazy solutions. I hope you still work in a restricted account (my congratulations).

There is a tool out there (from 2008 or so) called NamespaceManagerTool if I remember correctly that is supposed to grant the restricted user permissions on these service URLs that you define for WCF. I haven't used that though...

Lord of Scripts
  • 3,579
  • 5
  • 41
  • 62
  • The tool you're thinking of is called Http Namespace Manager. It's unsupported by MS but it's available here: http://blogs.msdn.com/b/paulwh/archive/2007/05/04/addressaccessdeniedexception-http-could-not-register-url-http-8080.aspx – Gareth Nov 27 '14 at 16:24
1

While I was able to solve this problem in one computer following the other users solutions, the command netsh didn't solve the issue in one of my machines and even though the current user had administrator rights I was still getting the "HTTP could not register URL.... Your process does not have access rights to this namespace". So I'm sharing my solution in case you still don't get it to work with the other solutions too.

After also trying to give write permissions to the user in the physical directory of my website and getting no success, I finally decided trying to change IIS settings.

As the images below show, I configured the Physical Path Credentials of my website to connect as an specifc user, which was an admin account with DOMAIN\username and password, and this was enough to make the error disapear.

enter image description here

enter image description here

enter image description here

Ulysses Alves
  • 2,297
  • 3
  • 24
  • 34
1

Your sample code won't work as shown because you forgot to include a Console.ReadLine() before the serviceHost.Close() line. That means the host is opened and then immediately closed.

Other than that, it seems you have a permission problem on your machine. Ensure you are logged-in as an administrator account on your machine. If you are an administrator then it may be that you don't have the World Wide Web Publishing Service (W3SVC) running to handle HTTP requests.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • I've already fixed this problem and that's not because of my cmd account and (of course) not because of ReadLine() - cause the point is in the exception (not the cmd result). – valecoder Jan 04 '12 at 15:00
  • I solved that with http://stackoverflow.com/questions/885744/wcf-servicehost-access-rights. – valecoder Jan 04 '12 at 15:01
0

Close iis express and all the browsers (if the url was opened in any of the browser). Also open the visual studio IDE in admin mode. This has resolved my issue.

Lokesh
  • 129
  • 3
  • It did not solve the issue. Using an IDE in Administrator mode is another issue and probably more serious. – Preza8 May 15 '19 at 12:05
0

I was getting the same error in a for a domain using a different port (2130). The process was a service. I was able to fix the issue by changin the user running the service from NetworkSystem to Local System Account.

Sergio Prats
  • 1,043
  • 1
  • 14
  • 19
0

Another thing I might add: If this just for local testing purposes you could try to change the port from 8000 to something else such as 8080 and not get this error. That worked for me at least.

tomasat
  • 578
  • 8
  • 11