[英]How to publish and connect to my own WCF endpoint without Admin rights
I have a WCF REST API implemented in a C# class library. 我在C#类库中实现了WCF REST API。 Ultimately I would like the service to be able to be hosted in my own process, or as a Windows service, depending on the user's choice.
最终,我希望能够根据自己的选择将服务托管在自己的进程中,或者作为Windows服务托管。 So it needs to support both inproc and HTTP bindings.
因此,它需要同时支持inproc和HTTP绑定。
The following works as admin, but not as a regular user (says it doesn't have rights to open up the HTTP port). 以下内容以管理员身份工作,但不以普通用户身份工作(例如,它无权打开HTTP端口)。
What's my next step? 我下一步是什么? Should I be using a different protocol for in-proc?
我应该对进程使用其他协议吗? So long as I retain the ability to have it cross-machine on the HTTP binding, I'm satisfied.
只要我保留在HTTP绑定中跨机器运行的能力,我就会感到满意。
WebServiceHost host = new WebServiceHost(typeof(SapphireServiceLib.SapphireService), new Uri("http://localhost:8080/") );
try
{
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(SapphireServiceLib.ISapphireService), new WebHttpBinding(), "");
host.Open();
}
catch (Exception ex)
{
MessageBox.Show("Unable to start Sapphire Service Host: " + ex.Message);
return;
}
// Next, connect to the endpoint for our own use
try
{
// Set the max msg large, otherwise max size would be 64K and we couldn't fetch all the logs.
// REVIEW I suppose this might expose it to a Denial of Service attack if public
var binding = new WebHttpBinding() { MaxReceivedMessageSize = Int32.MaxValue };
ChannelFactory<ISapphireService> cf = new ChannelFactory<ISapphireService>(binding, "http://localhost:8080");
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
SapphireService = cf.CreateChannel();
}
catch (Exception ex)
{
MessageBox.Show("Unable to connect to service endpoint: " + ex.Message);
return;
}
If you want to keep the http binding you have to use the netsh http add urlacl url=http://+80/YourService
command to allow a process without admin rights to use it. 如果要保留http绑定,则必须使用
netsh http add urlacl url=http://+80/YourService
命令来允许没有管理员权限的进程使用它。
Another option is to switch to netTcpBinding for instance. 另一个选择是例如切换到netTcpBinding。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.