首页 > 解决方案 > 如何让我的自托管 WCF 服务不尝试保留 localhost 以外的 url?

问题描述

我想在 .NET 3.5 桌面应用程序中嵌入一个 REST API。在该框架版本上,只有一个选项:自托管 WCF。

但我似乎无法让它只在 localhost 上监听。

启动时,该应用程序尝试为 进行 url 预订,但 由于我没有以管理员权限运行它http://+:8000而失败。System.ServiceModel.AddressAccessDeniedException

我希望它只保留http://localhost:8000,从而避免需要管理员权限。

使用HostNameComparisonMode.Exact似乎并没有改变任何东西。

这是我尝试过的:

using Myservice;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace consolehost1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Use exact hostname comparison mode
            WebHttpBinding binding = new WebHttpBinding();
            binding.HostNameComparisonMode = HostNameComparisonMode.Exact;

            WebServiceHost host = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8000/"));
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1), binding, "some-endpoint");

            //The host still tries to get a reservation for http://+:8000 and not just http://localhost:8000
            host.Open();
        }
    }
}

我能做些什么 ?

标签: c#wcf

解决方案


下面的命令可以解决问题吗?

  • 适用于 Windows Vista、Windows7

netsh http 添加 urlacl url= http://+:8000/MyUri user=DOMAIN\user

  • 对于 Windows XP、Server2003。

httpcfg 设置 urlacl /u { http://URL:Port/ | https://URL:Port/ } /a ACL

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/configuring-http-and-https
https://docs.microsoft.com/en-us/windows/desktop/http /add-urlacl
如果问题仍然存在,请随时告诉我。


推荐阅读