C# 为什么我会得到WCF例外情况;HTTP无法注册URL HTTP://+;:80/MyService/“我的服务”;

C# 为什么我会得到WCF例外情况;HTTP无法注册URL HTTP://+;:80/MyService/“我的服务”;,c#,wcf,C#,Wcf,我使用需要使用的endpointaddress创建主机,如下所示: Uri endpointAddress = new Uri("http://127.0.0.1:5555/MyService"); ServiceHost host = new ServiceHost(myServiceType, endpointAddress); host.AddServiceEndpoint(implementedContract, basicHttpBinding, string.Empty); 但是当

我使用需要使用的endpointaddress创建主机,如下所示:

Uri endpointAddress = new Uri("http://127.0.0.1:5555/MyService");
ServiceHost host = new ServiceHost(myServiceType, endpointAddress);
host.AddServiceEndpoint(implementedContract, basicHttpBinding, string.Empty);
但是当我以后做
host.Open()时我得到异常“HTTP无法注册URL,因为另一个应用程序正在使用TCP端口80”。当我这样做的时候,结果是一样的

host.AddServiceEndpoint(implementedContract, basicHttpBinding, endpointAddress);
为什么它要尝试对端口80执行任何操作?我怎样才能解决这个问题

更新 在试图提供更完整的代码示例时,我找到了罪魁祸首。我添加了ServiceMetadataBehavior,如下所示:

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.ExternalMetadataLocation = new Uri(this.ExternalMetadataLocation);
smb.HttpGetEnabled = true;
smb.HttpGetUrl = this.RemovePort(this.EndpointAddress);
host.Description.Behaviors.Add(smb());
在过去,我似乎需要从URL中删除端口以使其正常工作,但现在这正是导致问题的原因

所以Pratik是对的:元数据是问题所在,当然,端口80的问题在于有一些新安装的应用程序使用该端口

谢谢,问候,


Miel.

以下对我来说很好:

class Program
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        void Test();
    }

    public class MyService : IMyService
    {
        public void Test()
        {
            throw new NotImplementedException();
        }
    }

    static void Main()
    {
        var endpointAddress = new Uri("http://127.0.0.1:5555/MyService");
        using (var host = new ServiceHost(typeof(MyService), endpointAddress))
        {
            var basicHttpBinding = new BasicHttpBinding();
            host.AddServiceEndpoint(typeof(IMyService), basicHttpBinding, string.Empty);
            host.Open();
        }
    }
}

可能您有其他代码在干扰?

这是因为端口80已被另一个应用程序使用,正如错误消息所说,很可能是本地IIS服务器使用的。尝试停止IIS并查看,或使用其他端口。 在vista和更高版本上,您可以使用命令检查已保留的端口


顺便问一下,在app.config或web.config文件中是否有http元数据或mex端点?

是否可以发布完整测试用例的代码,包括AddServiceEndpoint和host.open之间的代码?可能是因为它是另一个主机变量,或者您的某个调用出现了错误。@Paul+1,感谢您催促我写一个更好的问题。这让我找到了解决办法。我想你没有抓住重点。代码不应该试图使用端口80