WCF ServiceHost access rights
The issue is that the URL is being blocked from being created by Windows.
Steps to fix:
Run command prompt as an administrator.
Add the URL to the ACL
netsh http add urlacl url=http://+:8000/ServiceModelSamples/Service user=mylocaluser
Windows Service Hosted WCF Service Getting Access Denied When Trying To Read a File
When debugging one of your service calls, what does WindowsIdentity.GetCurrent()
return to you?
I imagine you have an impersonation issue here; if it's not the service account, then you have impersonation enabled for the operation/service (note, the client side settings don't dictate impersonation, client & server have to match) in which case you have to give permission to all of the users that you are impersonating.
Or you could just indicate that you want to identify the user, but not exactly impersonate.
If the user is your service account, then you have to double-check the effective permissions on the files you are trying to open for the account your service is running under.
WCF local machine ServiceHost and Administrator Privileges
Take a look at this post. You can also look into Juval Lowy's InProc Factory (you'll have to register to download). This allows to you to automate hosting services in the same process as the client.
WCF Service Access right: No access rights to this namespace
What netsh
command did you run?
netsh http add urlacl http://+:4711/ user=DOMAIN\USER
On your machine, the built-in administrator account has implicit ownership of all HTTP namespace reservations, so you need to delegate ownership of the specific namespace reservation (like above) to the target account that will be running the HTTP endpoint. You do this by ensuring you Run as administrator
before performing the netsh
command.
To check what namespace reservations are in place, run:
netsh http show urlacl
WCF ServiceHost restricted user netsh/httpcfg
In WCF, the HTTP and HTTPS bindings use HTTP.sys under the cover to reserve a required URL for a specific WCF service, which is the same path IIS itself follows while doing the bindings for the websites it manages. This explains why the process performing the HTTP/HTTPS binding is required to run in elevated mode.
That being said, I would solve your issue in two different ways:
Option 1: use a different kind of binding. NetTcpBinding and NetNamedPipesBinding, for example, do not generally require administrative privileges: this is by far the easiest way to go.
Option 2: setup the required namespace reservation at installation time. This way you may ask your users to perform the installation in elevated mode and later allow restricted accounts to run it. While performing the initial installation/reservation you may also find out an available port to use (and perhaps save it in a configuration file for later reuse).
Manage access rights to WCF Data Service
If you need more granular control over authentication on your data, you need to authenticate your users with classic WCF authentication methods and use Query and Change Interceptors to control who can read or change your data.
More information about the Query and Change interceptors here,
http://msdn.microsoft.com/en-us/library/dd744842.aspx
Thanks
Pablo.
WCF Service - Self Hosting Service does not work
For console applications to open up incoming (listening) TCP ports using HTTP.sys (which is what the WCF self-hosted scenario uses), they need either to have administrative privileges, or for some account with an administrative privilege to reserve a "namespace" (i.e., a port/path pair) for specific accounts (or all accounts) to use. You mention that you cannot get admin privileges, so you need to get some admin on the machine to grant you access for some namespace which you'll use.
For example, in one of my machines, I never run my VS as administrator (or try my best, as some operations require it), but to start up WCF services, I chose one path (in my case, http://<my-machine-name>:8000/Service
), and I reuse that path as the base address of my services. So I had to run, as an admin, the command line below:
netsh http add urlacl url=http://+:8000/Service user=MYDOMAIN\myusername
In your case, you'll need to get an admin in the box to run a similar command for you.
Self-hosted WCF service: How to access the object(s) implementing the service contract from the hosting application?
As marc_s said, you're creating a PerCall/PerSession WCF service and a new instance is created on every request/first request of every session.
You could build some plumbing around it so that the instance can notify the service host when it receives a new request but it won't be an easy exercise and you need to be mindful of the potential for memory leak if you decide to go with using events to do this - without implementing the Weak Event Pattern your WCF service instances could be left hanging around as the event handlers still holds a reference to them UNLESS you remember to notify the host to unsubscribe when the WCF service instances are disposed.
Instead, here's two ideas which might make it easier for you to achieve your goal:
Use the Single InstanceContextMode if your service can be made a singleton, in which case you will create a new instance which implements your service contract and host it:
// instance will be your WCF service instance
private ServiceHost _host = new ServiceHost(instance);
that way you will have access to the instance that will retrieve the client requests.
Alternatively, you could have all the hosted instances be dummy 'fascades' which share a static class that actually process the requests:
[ServiceContract]
interface IMyService { ... }
interface IMyServiceFascade : IMyService { ... }
// dummy fascade
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall]
public class MyServiceFascade : IMyServiceFascade
{
private static IMyService _serviceInstance = new MyService();
public static IMyService ServiceInstance { get { return _serviceInstance; } }
public int MyMethod()
{
return _serviceInstance.MyMethod();
}
...
}
// the logic class that does the work
public class MyService : IMyService { ... }
// then host the fascade
var host = new ServiceHost(typeof(MyServiceFascade));
// but you can still access the actual service class
var serviceInstance = MyServiceFascade.ServiceInstance;
I'd say you should go with the first approach if possible, makes life that bit easier!
Hosting WCF service via ServiceHost in Console/WinForms
You need to make sure you WCF web config is set up correctly
You will need to enable metat data for http gets, check you web config in the
system.serviceModel -> behaviors -> serviceBehaviors -> behavior -> serviceMetadata
and make sure you have:
<serviceMetadata httpGetEnabled="true"/>
For Part 2, you can get the data, you can do something like
public MathResult GetResult(int a, int b) {
var status = new MathResult();
try {
var myBinding = new WSHttpBinding();
var myEndpoint =
new EndpointAddress(
new Uri("http://localhost:3980/"));
var myChannelFactory = new ChannelFactory<ICalculator>(myBinding, myEndpoint);
ICalculator client = myChannelFactory.CreateChannel();
status = client.DoMathJson(a,b);
} catch (Exception e) {
//do something proper here
}
return status;
}
Related Topics
Entity Framework 6: Audit/Track Changes
C# Iterating Through an Enum? (Indexing a System.Array)
Populate Treeview with File System Directory Structure
Why Does Wcf Return Myobject[] Instead of List<T> Like I Was Expecting
Am I Misunderstanding Linq to SQL .Asenumerable()
Self-Referencing Many-To-Many Recursive Relationship Code First Entity Framework
.Net Framework 3.5 and Tls 1.2
Calling Oracle Stored Procedure from C#
Differencebetween Manualresetevent and Autoresetevent in .Net
Write to Windows Application Event Log Without Event Source Registration
Should I Always Return Ienumerable<T> Instead of Ilist<T>
Variables Within App.Config/Web.Config
Why Is It Considered Bad to Expose List<T>
If Int32 Is Just an Alias for Int, How Can the Int32 Class Use an Int