WCF自托管-在Windows 10上死在水中?

WCF自托管-在Windows 10上死在水中?,wcf,windows-10,self-hosting,Wcf,Windows 10,Self Hosting,我有一个非常简单的WCF服务-这是服务合同: [ServiceContract] public interface IHelloService { [OperationContract] string SayGreeting(string name); [OperationContract] string SayRudeGreeting(string name); } 这就是实现: public class HelloService : IHelloServi

我有一个非常简单的WCF服务-这是服务合同:

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    string SayGreeting(string name);

    [OperationContract]
    string SayRudeGreeting(string name);
}
这就是实现:

public class HelloService : IHelloService
{
    public string SayGreeting(string name)
    {
        return "Well, hello there, " + name;
    }

    public string SayRudeGreeting(string name)
    {
        return string.Format("What do you want, {0} ?", name);
    }
}
这不是有趣的部分:-)我想做的是在Windows命令行应用程序中使用这个简单的服务。我用以下几行代码来实现这一点:

Uri baseAddress = new Uri("http://localhost:9099");

using (ServiceHost host = new ServiceHost(typeof(HelloService), baseAddress))
{
    host.Open();

    Console.WriteLine("HelloService is up and running ....");
    Console.ReadKey();

    Console.WriteLine("HelloService - closing down...");
}
对于该主机,我有一个非常简单、直接的
app.config

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="DevDebug">
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="WcfService1.HelloService" 
                 behaviorConfiguration="DevDebug">
            <endpoint
                address="HelloService"
                binding="wsHttpBinding"
                contract="WcfService1.IHelloService" />
            <endpoint
                address="HelloService/mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>

一点也不奇怪-只是一个常规的
wsHttpBinding
端点在
http://localhost:9099/HelloService
以及一个MEX端点

当我构建此主机和WCF服务,并在Visual Studio 2015中运行主机时(我以“管理员”身份启动),我收到以下奇怪的消息:

System.ServiceModel.AddressAccessDeniedException未处理
HResult=-2146233087
Message=HTTP无法注册URL。您的进程没有对此命名空间的访问权限(有关详细信息,请参阅)

我是邮箱上的管理员-为什么不允许我注册此URL

我怀疑这是Windows10(Professional-v1607)的一个新“功能”——因为我很确定,这个设置在我以前的Windows7上运行得很好

好的-所以我从命令行启动我的WCF主机,明确地作为管理员-现在主机启动了,一切看起来都很好,我可以从WCF测试客户端连接到它,我的代码似乎工作得很好

但是,如果我现在尝试使用SoapUI或IE 11连接到它,我会不断收到另一系列错误消息(“无效页面”等),这些消息似乎表明无法访问此URL

为什么

我需要在Windows 10 Professional中执行什么操作才能使WCF自托管再次工作


谢谢

原因是您必须注册URL。 基本上,打开命令行窗口CMD(在Windows10搜索中键入CMD)

然后使用以下脚本安装网络本地地址:(复制并右键单击粘贴)

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

netsh http add urlacl url=user=DOMAIN\user

注意:用URL替换+8731,用服务名称替换Design_Time_地址(也可以省略此项,但这有助于识别URL-可能同一URL中有多个服务)

删除网络本地地址 netsh http删除urlacl url=

然后,您需要通过Windows防火墙打开端口

在Windows搜索中放置:防火墙 在左侧菜单上,单击“高级设置” 然后单击左侧菜单中的“入站设置”

在右菜单中,单击“新建规则”。。这将打开一个新窗口。 选择“端口”并单击“下一步” 在文本框中,输入您的URL(我的是8731),然后单击“下一步” 保留当前选择的“允许连接”并单击“下一步” 单击所有复选框(除非您另行知道),然后单击“下一步”

给它一个与您的应用程序相关的好名字,并添加描述,然后单击“完成”

您现在应该能够通过web浏览器链接到URL

如果你想向外界提供实际的服务,那么你需要去你的路由器,设置一个静态IP地址,并通过防火墙打开端口

您可以通过192.168.0.1访问路由器(取决于您的路由器)

显然,如果你使用笔记本电脑,那么当你把笔记本电脑从公寓里拿出来的那一刻,你的服务将不再对外开放,因此你真的需要一台专用的台式机来充当服务器

你可以通过谷歌搜索找到关于静态IP和端口转发的信息,你也可以通过谷歌搜索你的路由器及其防火墙设置来允许你的URL

希望一切都能解决,如果不行,那就再问一次

祝你好运,玩得开心

这也适用于RestApi(Asp.Net WebApi),WCF服务通常用于金融服务公司,因为它们允许更细粒度的安全协议