【问题标题】:Changing ServiceHost EndPoint Address at Runtime C#在运行时 C# 更改 ServiceHost 端点地址
【发布时间】:2015-02-06 11:18:46
【问题描述】:

我正忙于编写一个文件服务器/客户端工具,它基本上使用托管服务向服务器发送和接收数据。由于此解决方案将被许多不同的人使用,因此不建议让他们去编辑 App.Config 文件以进行设置。我想做的是在运行时更改它,以便用户可以完全控制要使用的设置。所以,这是我的 App.Config 文件:

<system.serviceModel>
        <services>
            <service name="FI.ProBooks.FileSystem.FileRepositoryService">
                <endpoint name="" binding="netTcpBinding"
                    address="net.tcp://localhost:5000"
                    contract="FI.ProBooks.FileSystem.IFileRepositoryService"
                    bindingConfiguration="customTcpBinding" />
            </service>
        </services>
        <bindings>
            <netTcpBinding>
                <binding name="customTcpBinding" transferMode="Streamed" maxReceivedMessageSize="20480000" />
            </netTcpBinding>
        </bindings>
    </system.serviceModel>

我想做的是在执行应用程序时只更改地址(在本例中为 net.tcp://localhost:5000)。所以我必须能够读取当前值并将其显示给用户,然后接受他们的输入并将其保存回该字段。

【问题讨论】:

    标签: c# wcf service servicehost


    【解决方案1】:

    下面的测试可能会对您有所帮助。基本上这些步骤是

    • 实例化一个从 .config 文件读取配置的主机实例;
    • 使用与旧配置相同的配置创建 EndpointAddress 的新实例,但更改 uri 并将其分配给 ServiceEndpointAddress 属性。

      [TestMethod]
      public void ChangeEndpointAddressAtRuntime()
      {
          var host = new ServiceHost(typeof(FileRepositoryService));
      
          var serviceEndpoint = host.Description.Endpoints.First(e => e.Contract.ContractType == typeof (IFileRepositoryService));
          var oldAddress = serviceEndpoint.Address;
          Console.WriteLine("Curent Address: {0}", oldAddress.Uri);
      
          var newAddress = "net.tcp://localhost:5001";
          Console.WriteLine("New Address: {0}", newAddress);
          serviceEndpoint.Address = new EndpointAddress(new Uri(newAddress), oldAddress.Identity, oldAddress.Headers);
          Task.Factory.StartNew(() => host.Open());
      
          var channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), new EndpointAddress(newAddress));
          var channel = channelFactory.CreateChannel();
      
          channel.Method();
      
          (channel as ICommunicationObject).Close();
      
      
          channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), oldAddress);
          channel = channelFactory.CreateChannel();
      
          bool failedWithOldAddress = false;
          try
          {
              channel.Method();
          }
          catch (Exception e)
          {
              failedWithOldAddress = true;
          }
      
          (channel as ICommunicationObject).Close();
      
          Assert.IsTrue(failedWithOldAddress);  
      }
      

    【讨论】:

    • 不喜欢所有的变量,但你的回答总体上很有帮助,谢谢。
    【解决方案2】:

    您可以创建提供配置名称和端点的服务实例。所以你可以使用;

    EndpointAddress endpoint = new EndpointAddress(serviceUri);
    var client= new MyServiceClient(endpointConfigurationName,endpoint )
    

    查看msdn 文章。

    【讨论】:

    • 问题是关于 ServiceHost 而不是客户端
    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2015-03-03
    相关资源
    最近更新 更多