I want to implement a simple self-hosted REST API in my WPF application, that listens on a specific port on localhost. This API will be consumed by a website in order to communicate with the WPF application, if it's running.
It didn't take long before I encountered the System.ServiceModel.AddressAccessDeniedException error. The URL must be reserved and this can only be done if the process is running with elevated privileges. The problem is that this application is in use at thousands of companies with varying IT policies, which makes it virtually impossible for the application to require administrative privileges each time it's started.
This is my test code:
_task = Task.Factory.StartNew(() =>
{
var uri = new Uri("http://localhost:5000/test");
var type = typeof (TestService);
WebServiceHost host = new WebServiceHost(type, uri);
WebHttpBinding binding = new WebHttpBinding();
binding.CrossDomainScriptAccessEnabled = true;
host.AddServiceEndpoint(type, binding, uri);
host.Open();
});
Are there any ways around this? Any 3rd party packages that I can use? Or could I reserve the URL during installation of the application, since the installation requires elevated privileges? Or this is a dead end?
Just run this during installation:
netsh http add urlacl url=http://+:5000/Test/ user=Everyone
Note you can restrict the user from Everyone to the logged in user e.g. user=MyDomain\John
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With