This question already has answers here:
HTTP could not register URL http://+:10001/. Your process does not have access rights to this namespace
(3 answers)
Closed 8 years ago.
I have written a simple WCF code. and want to host it . but when I run it shows me an exception
HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace
I have a class library project, in which I have written my WCF code. then I added App.config file and write some codes
App.config code :
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<system.serviceModel>
<services>
<service name="DEMO1.HelloService" behaviorConfiguration="mexBehavior">
<endpoint address="HelloService" binding="basicHttpBinding" contract="DEMO1.IHelloService"></endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="DEMO1.IHelloService"></endpoint>
<endpoint address="Mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080"/>
<add baseAddress="net.tcp://localhost:8090"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
this is my Interface
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string GetMessage(string name);
}
and i implemented this
public class HelloService : IHelloService
{
public string GetMessage(string name)
{
return "Name : " + name;
}
}
then I added another console project to that solution and
class Program
{
static void Main()
{
using(ServiceHost host=new ServiceHost(typeof(DEMO1.HelloService)))
{
try
{
host.Open();
Console.WriteLine("Started");
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
My hierarchy
This problem come when you can change manually baseaddress. So wcf service doesn't find the service location where service is located so if you can change your service baseaddress port then you can change only port number and shouldn't change all address. If you changed all address then address doesn't find service location and service has been giving error.
Any service which is hosted via HTTP.SYS (which includes WCF) requires permission to the namespace you are attempting to use (http://localhost:8080, in your case).
Either run as administrator, or add an urlacl which allows your process access to the namespace.
In an administrator command prompt:
netsh http add urlacl url=http://+:8080/ user=BUILTIN\Users
Related
i was trying to create basic WCF service and host it on a console application.
Here's my code of WCF project.
ISampleService.cs
using System.ServiceModel;
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleService" in both code and config file together.
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string DoWork();
}
}
SampleService.cs
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleService" in both code and config file together.
public class SampleService : ISampleService
{
public string DoWork()
{
return "Message from WCFservice";
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">
<endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/> <!--For metadata exchange-->
<add baseAddress="net.tcp://localhost:8737/" /> <!--Endpoint, netTCP binding, For data exchange-->
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
WCF Hosting on console appl - Program.cs
using System;
using System.ServiceModel;
namespace ConsumeWCFApplicationAppl
{
class Program
{
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(MultipleSeviceContractAppl.SampleService)))
{
host.Open();
Console.WriteLine("Host started #" + DateTime.Now.ToString());
Console.ReadKey();
}
}
}
}
in console application at line host.Open();, following exception was thrown.
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.ServiceModel.dll Additional information: Service
'MultipleSeviceContractAppl.SampleService' has zero application
(non-infrastructure) endpoints. This might be because no configuration
file was found for your application, or because no service element
matching the service name could be found in the configuration file, or
because no endpoints were defined in the service element. Help me to
figure out my mistake. Thanks
You need to replicate the config in your console app and add reference to DLL of the service model assemblies to this project...
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">
<endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/> <!--For metadata exchange-->
<add baseAddress="net.tcp://localhost:8737/" /> <!--Endpoint, netTCP binding, For data exchange-->
</baseAddresses>
</host>
</service>
</services>
I'm learning WCF, I started creating a very basic host app which defines a class with one method as follows:
[ServiceContract()]
public interface IMath
{
[OperationContract]
int Add(int a, int b);
}
public class MathCalcs : IMath
{
public MathCalcs()
{
Console.WriteLine("Service await two numbers...");
}
public int Add(int a, int b)
{
return a + b;
}
}
and that is how I configured the App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ConsoleHost.MathCalcs" behaviorConfiguration="MathServiceMEXBehavior">
<endpoint address="http://localhost:8080/MathCalcs"
binding="basicHttpBinding"
contract="ConsoleHost.IMath"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/MathCalcs"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MathServiceMEXBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Then I called the service from Main
using (ServiceHost host = new ServiceHost(typeof(MathCalcs)))
{
host.Open();
Console.WriteLine("***The service is ready***");
}
Console.ReadLine();
But it fails to view the metadata of the service through the URI http://localhost:8080/MathCalcs, I'm sure I'm following the steps right as the book I'm reading from and as a preceding example works fine, the only difference is that I didn't separate the service logic (the interface and the class) in a stand alone class library.
What am I missing?
The following line of code
Console.ReadLine();
must be inside the braces of the using clause!
When that is done, retry finding the WSDL metadata.
Here is a related post that I think may lead you in the right direction.
WCF service showing blank in browser
I have a simple WCF service:
namespace Vert.Host.VertService
{
[ServiceContract]
public interface IRSVP
{
[OperationContract]
bool Attending();
[OperationContract]
bool NotAttending();
}
public class RSVPService : IRSVP
{
public RSVPService()
{
}
public bool Attending()
{
return true;
}
public bool NotAttending()
{
return true;
}
}
}
I'd like to self-host in a console application like so:
class Program
{
public static void Main()
{
// Create a ServiceHost
using (ServiceHost serviceHost = new ServiceHost(typeof(RSVPService)))
{
// Open the ServiceHost to create listeners
// and start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
}
}
So I'm using this app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<system.serviceModel>
<services>
<service name="Vert.Host.VertService.RSVPService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/Vert" />
</baseAddresses>
</host>
<endpoint
address="/RSVP"
binding="basicHttpBinding"
contract="Vert.Host.VertService.IRSVP" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
As I understand it, this setup would leave me with http://localhost:8080/Vert/RSVP/Attending as a valid REST URI to call from an arbitrary HTTPClient, but the call is hanging indefinitely or coming back with a 0 No Response (I'm using Advanced REST client)
What am I missing?
You are RIGHT in all of your setup...right up to the point where you stopped typing code and started telling me what you have. :)
What you've created is a std WCF service and you can get to it using a Service proxy or a ChannelFactory, but it will communicate with you as-is using SOAP.
You need this tutorial to turn this webservice into a RESTFUL service giving back Json/pox.
I have created my first self-hosted WCF service. I hosted it in a C# console app but it throws an error:
System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http: 8080
When I run Visual Studio 2013 as administrator, then it works well, but not if I don't. So any way to get it done automatically instead of starting VS as an ADMIN?
So far I created a HelloService class library in which I added a WCF service which consists of an interface IHelloService and HelloService.
IHelloService:
namespace HelloService
{
[ServiceContract]
public interface IHelloService
{
[OperationContract]
String GetMsg();
}
}
HelloService:
namespace HelloService
{
public class HelloService : IHelloService
{
public String GetMsg()
{
return "Service Accessed";
}
}
}
Then I created a C# console app HelloServiceHost which has an app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="MexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloService.HelloService"
behaviorConfiguration="MexBehaviour" >
<endpoint
address="HelloService"
binding="basicHttpBinding"
contract="HelloService.IHelloService"></endpoint>
<endpoint
address="HelloService"
binding="netTcpBinding"
contract="HelloService.IHelloService"></endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/"/>
<add baseAddress="net.tcp://localhost:8081/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
and program.cs file:
using HelloService;
using System.ServiceModel;
namespace HelloServiceHost
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost sh = new ServiceHost(typeof(HelloService.HelloService)))
{
sh.Open();
Console.WriteLine("Host Started #"+ System.DateTime.UtcNow.ToShortDateString());
sh.Close();
}
}
}
}
I followed a video tutorial exactly but it's not working why ?
I am using VS 2013, .net 4
Start the cmd as Administrator and enter:
netsh http add urlacl url=http://+:8080/MyUri user=DOMAIN\user
this worked for me.
I ran into the same problem on a different project.
The problem is that binding to a tcp port requires administrative privileges. There's a couple ways to deal with this.
Keep an administrative command prompt open. Then you can just run the console app directly.
(As you suggested) run VS as admin. This is absolutely necessary only when debugging your app.
Create an application manifiest file, specifying requestedExecutionLevel level="requireAdministrator". See How do I force my .NET application to run as administrator? for more details.
Root path URL (http://+:8080/) permissions are needed in your case:
netsh http add urlacl url=http://+:8080/ user=%USERDOMAIN%\%USERNAME%
show all URL reservations:
netsh http show urlacl | FIND "URL"
If you ran into this error while starting the service on Windows Service, you can check the account on your process installer.
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCFWindowsServiceSample";
Installers.Add(process);
Installers.Add(service);
}
}
I have WCF service how run on 1 machine and simple comsole application client who run on another machine.
the server do something very simple: one method that return the value of 2 number that the client sent:
[ServiceContract]
public interface IMySampleWCFService
{
[OperationContract]
int Add(int num1, int num2);
[OperationContract]
void CreateDirectory(string directory);
[OperationContract]
string GetVendorToRun(string path);
}
public class MySampleWCFService : IMySampleWCFService
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
What i want to do and find it hard to implement because i am a new developer is add Discovery to my WCF service, suppose i have several services that installed on several machines, what the client application is open i want to now which services is alive\running and all this data that i can received from Discovery.
I try to read several articles but as i mention didn't understand how to do it and I would love some help.
Using WCF Discovery is a bit convoluted and few people actually use it in my experience but it does work. This MSDN article has all the detail needed for adding Discovery to both the service & client configuration files.
The premise behind WCF Discovery is that you a expose a new discovery endpoint in similar way to the default MEX endpoint. The MEX endpoint allows the service to provide WSDL to clients. A WCF Discovery endpoint exposes a configured service to clients by means of UDP based responses to client UDP based requests. The overview link above provides much more detail.
Here is how your service configuration would look like:
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService"
behaviorConfiguration="endpointDiscoveryBehavior">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<!-- Discovery Endpoint: -->
<endpoint kind="udpDiscoveryEndpoint" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
<!-- Announcement Listener Configuration -->
<service name="AnnouncementListener">
<endpoint kind="udpAnnouncementEndpoint" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceDiscovery>
<announcementEndpoints>
<endpoint kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="endpointDiscoveryBehavior">
<endpointDiscovery enabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
I think the most easy way to do this is to try to connect your client to addresses that you calculate at the runtime. For example:
static void Main(string[] args)
{
var addresses = new List<string>
{
#"http://192.168.1.1:8730/MySampleWCFService/",
#"http://localhost:8731/MySampleWCFService/",
#"http://localhost:8732/MySampleWCFService/",
#"http://localhost:8733/MySampleWCFService/",
};
foreach (var address in addresses)
{
var client = new MySampleWCFServiceClient(new BasicHttpBinding(), new EndpointAddress(address));
try
{
client.Open();
client.Add(0, 1);
Console.WriteLine("Connected to {0}", address);
}
catch (EndpointNotFoundException)
{
Console.WriteLine("Service at {0} is unreachable", address);
}
}
Console.ReadLine();
}
In my case I create a list with addresses but in your case you may build addresses with some predefined rules. For example you know that services use http binding with some name and port. Also you know that your cluster is in 192.0.16.xxx LAN so you may use formula:
address = "http://" + NextLanAddress() + ":" + port + "/" + serviceName + "/";