【问题标题】:Dynamically switch WCF Web Service Reference URL path through config file通过配置文件动态切换 WCF Web 服务引用 URL 路径
【发布时间】:2011-06-29 12:12:59
【问题描述】:

如何通过配置文件动态切换WCF Web Service Reference URL路径?

【问题讨论】:

    标签: c# asp.net wcf


    【解决方案1】:

    您是否只想将配置中的 URL 覆盖为不同的 URL。假设您有一个测试服务和一个实时服务。你可以这样做。

    client.Endpoint.Address = new EndpointAddress(Server.IsLiveServer() ?
        @"LiveUrl" : @"TestURl"); 
    

    这些网址来自哪里,无论你想要什么

    【讨论】:

    • 什么是 Server.IsLiveServer() :)?
    • Server.IsLiveServer() 只是一个自定义方法(随便调用它),它会检查以确定要使用的 url。
    • Server.IsLiveServer() 只是用来根据使用 API 的环境切换 URL
    【解决方案2】:

    只是为了扩展 Erin 的答案:-

    MyClient client = new MyService.MyClient();
    client.Endpoint.Address = new EndpointAddress(new Uri("insert new url here"),
        client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
    client.Open();
    

    HTH!

    【讨论】:

      【解决方案3】:

      没有动态切换。每次您想使用另一个 URL 时,您必须创建服务代理(客户端)的新实例并将 EndpointAddress 或 enpoint 配置名称传递给构造函数。

      【讨论】:

        【解决方案4】:

        当然可以,请看这里:How to config clients for a wcf service?

        在开发中指向 localhost 并在 web.config 中更改生产中的地址(url)是绝对正常的

        【讨论】:

        • 我在 7 个文件中看到了开发 URL 的引用:reference.svcmap、.xsd、.wsdl、.disco、.svcinfo 和 web.config。这是我需要的所有配置 web.config 吗?
        • 啊你的意思是你想改变所有这些,有点覆盖触及 web.config 的硬编码值?
        • @Martin:我敢打赌那是一个命名空间,而不是你的开发 URL。
        【解决方案5】:

        在任何调用后,您都无法获得端点 url。

        例如

        在这种情况下,您将从 NEWURL 获得答案:

        MyClient client = new MyService.MyClient();
        client.Endpoint.Address = new EndpointAddress("NEWURL"); 
        client.Hello(); //return is hello response from NEWURL
        

        但如果您在更改 url 之前调用任何方法,则会使用 app.config 中的 url,如下一个示例:

        MyClient client = new MyService.MyClient();
        client.Endpoint.Address = new EndpointAddress("NEWURL"); 
        client.Hello(); //return is hello response from BASEURL
        

        【讨论】:

          【解决方案6】:

          我一直在尝试做同样的事情,但各种帖子中大多数接受的答案只是更改了地址。目前在 .net 4.7 下,简单地更改地址本身是行不通的。如果您有两台不同的服务器并希望它从一台切换到另一台,您必须这样做:

          var client = new MyService.Service1Client();
          var newAdrEndpoint = new EndpointAddress(new Uri("second server address"));
          client = new MyService.Service1Client(client.Endpoint.Binding, newAdrEndpoint);
          

          基本上,您需要使用与第一台服务器相同的绑定并传入新地址来创建新服务。这是我找到的最简单的方法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多