WCF 服务 - 自托管服务不起作用 [英] WCF Service - Self Hosting Service does not work

查看:30
本文介绍了WCF 服务 - 自托管服务不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 VS 2010 中创建的 WCF 服务应用程序.当我执行它时,我在以下本地 url 中得到了服务页面

I have a WCF Service application created in VS 2010. When I execute it, I got the service page in the following local url

我创建了另一个自托管控制台应用程序,如下所示.它抛出以下异常

I created another self hosting console application as shown below. It is throwing following exception

HTTP 无法注册 URL.您的进程没有访问此命名空间的权限(请参阅 http://go.microsoft.com/fwlink/?LinkId=70353 了解详情).

HTTP could not register URL. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

好吧,我对 49609 端口号没有特别的兴趣.我只是从其他工作服务复制它.

Well, I have no particular interest in the 49609 port number. I just copied it from the other working service.

现在,我拥有运行 WCF 服务的最低权限.我需要做哪些更改才能使以下代码成功?

Now, I have bare minimum rights for running the WCF service. What change I need to do to make the following code to succeed?

注意:我没有机会在这台机器上获得管理员权限.

Note: There is no chance for me to get the administrator rights in this machine.

注意:我可以使用任何可以工作的端口号.

Note: I am fine with any port number that will work.

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string baseAddress = "http://" + Environment.MachineName + ":49609/Service";
        ServiceHost host1 = new ServiceHost(typeof(Service1), new Uri(baseAddress));
        host1.AddServiceEndpoint(typeof(ConsoleApplication1.IService1), new BasicHttpBinding(), baseAddress);
        host1.Open();

    }
    static Binding GetBinding()
    {
        BasicHttpBinding result = new BasicHttpBinding();
        return result;
    }

}

}

服务

public class Service1 : IService1
{
    public int Add(int n1, int n2)
    {
        return n1 + n2;
    }
}

[ServiceContract]
public interface IService1
{
    [OperationContract]
    int Add(int n1, int n2);
}

推荐答案

对于使用 HTTP.sys(WCF 自承载方案使用的)打开传入(侦听)TCP 端口的控制台应用程序,他们需要要么具有管理权限,或为某些具有管理权限的帐户保留命名空间"(即端口/路径对)供特定帐户(或所有帐户)使用.您提到您无法获得管理员权限,因此您需要在计算机上获得一些管理员权限,以授予您对将使用的某些命名空间的访问权限.

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.

例如,在我的一台机器上,我从来没有以管理员身份运行我的 VS(或者尽我所能,因为某些操作需要它),但是为了启动 WCF 服务,我选择了一条路径(在我的情况下,http://<my-machine-name>:8000/Service),然后我将该路径重用作我的服务的基址.所以我必须以管理员身份运行以下命令行:

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.

这篇关于WCF 服务 - 自托管服务不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆