【问题标题】:Code Only WCF Hosting has "localhost" in the link to the wsdl仅代码 WCF 托管在指向 wsdl 的链接中具有“localhost”
【发布时间】:2011-10-29 23:57:25
【问题描述】:

我有一个自己托管在控制台应用程序中的 wcf 服务。

当我运行服务并将其部署到机器(称为 MyWCFRunningMachine)时,我可以转到“您已创建服务”页面。 (http://MyWCFRunningMachine:8090/MyService)。

然后它提供了一个到 wsdl 页面的链接。该链接如下所示:http://localhost:8090/MyService?wsdl

因此,当我单击该链接时,它会尝试使用我的机器而不是 MyWCFRunningMachine 连接到服务。

如果我冷输入 wsdl 的路径 (http://MyWCFRunningMachine:8090/MyService?wsdl),那么我会在浏览器中看到一个 wsdl。但是,如果我尝试添加服务引用,则会收到此错误:

文档已被理解,但无法处理。
- WSDL 文档包含无法解析的链接。
- 下载“http://localhost:8090/MyService?xsd=xsd0”时出错。

这也是在不应该引用本地主机时。

这是我用来自托管服务的代码:

public class SelfServiceHost
{
    static string StartUpUrl {get{return "http://localhost:8090/MyService";}}
    static void Main(string[] args) { StartupService(StartUpUrl); }

    public static ServiceHost StartupService(string startUpUrl)
    {
        //+ Setup the Service
        //Create a URI to serve as the base address
        Uri httpUrl = new Uri(startUpUrl);
        //Create ServiceHost
        ServiceHost host = new ServiceHost(typeof(MyService), httpUrl);
        //Add a service endpoint
        host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");
        //Enable metadata exchange
        ServiceMetadataBehavior serviceMetadataBehavior =  
             new ServiceMetadataBehavior {HttpGetEnabled = true};
        host.Description.Behaviors.Add(serviceMetadataBehavior);

        //! Turn on Debug.  Remove for production!
        host.Description.Behaviors.Remove(typeof (ServiceDebugBehavior));
        ServiceDebugBehavior serviceDebugBehavior = 
            new ServiceDebugBehavior {IncludeExceptionDetailInFaults = true};
        host.Description.Behaviors.Add(serviceDebugBehavior);

        //Start the Service
        host.Open();
        Console.WriteLine("Service is hosted at " + httpUrl);
        Console.ReadLine();

        return host;
    }
}

我怎样才能得到这个来删除本地主机? (注意:我不能将它硬编码到 MyWCFRunningMachine。该服务将在多台不同的机器上运行。

我是否需要使用我在移动机器时更改的配置文件? (我一直远离配置文件,因为我不想为我的控制台应用程序设置一个,但如果这是唯一的方法,那么我会这样做。)

【问题讨论】:

    标签: c# visual-studio wcf wsdl service-reference


    【解决方案1】:

    我认为您使用配置文件保存机器名称是正确的

    "http://" + MACHINE_NAME + ":8090/MyService"

    安装服务后,更改配置文件中的值并重新启动服务以将其引入。

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多