【问题标题】:C# NetTcpBinding Client bindings from other config fileC# NetTcpBinding 来自其他配置文件的客户端绑定
【发布时间】:2020-03-27 18:23:18
【问题描述】:

我有一个客户端,它是一个名为 Windows.exe 的 Windows 应用程序。我有一个名为 ServiceFacade.dll 的 C# 类库,它有一个名为 ServiceFacade.dll.config 的配置文件。在 ServiceFacade.dll.config 中,我有如下客户端绑定

<system.serviceModel>
    <client>
      <endpoint address="net.tcp://localhost:5000/MyService" 
                binding="netTcpBinding" 
                contract="IMyService"
                name="NetTcpBinding_MyService"/>
    </client>
  </system.serviceModel>

在 ServiceFacade.dll 中,我有如下代码来创建代理

NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");
ChannelFactory<IMyService> chn = new ChannelFactory<IMyService>(binding);
IMyService service = chn.CreateChannel();

Windows.exe 调用 ServiceFacade.dll 来进行服务调用。

但下面一行是在 Windows.exe.config 中寻找 NetTcpBinding_MyService 而不是 ServiceFacade.dll.config

如何在 ServiceFacade.dll.config 中看到 NetTcpBinding_MyService 而不是 Windows.exe.config ?

NetTcpBinding 绑定 = new NetTcpBinding("NetTcpBinding_MyService");

【问题讨论】:

    标签: c# wcf client config nettcpbinding


    【解决方案1】:

    您绝对可以将配置从Windows.exe.config 复制到ServiceFacade.dll.config。我宁愿手动创建服务端点,也不愿在使用 ChannelFactory 调用服务时复制配置。
    客户端。

    class Program
        {
            static void Main(string[] args)
            {
                Uri uri = new Uri("https://vabqia969vm:21011/");
                NetTcpBinding binding = new NetTcpBinding();
                binding.OpenTimeout = new TimeSpan(0, 10, 0);
                binding.MaxReceivedMessageSize = 2147483647;
                binding.ReaderQuotas.MaxStringContentLength = 2147483647;
                ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, new EndpointAddress(uri));
                ITestService service = factory.CreateChannel();
                var result1 = service.GetData(34);
                Console.WriteLine(result1);
            }
    
        }
        //The service contract is shared between the client-side and the server-side.
        [ServiceContract]
        public interface ITestService
        {
            [OperationContract]
            string GetData(int id);
        }
    

    希望对你有用。

    【讨论】:

      【解决方案2】:

      我确实喜欢下面。

      我在ServiceFacade.dll.config下面添加了

      <system.serviceModel>
          <bindings>
            <netTcpBinding>
              <binding name="netTcpBindingConfiguration"
                       closeTimeout="00:10:00"
                       openTimeout="00:10:00"
                       receiveTimeout="00:10:00"
                       sendTimeout="00:10:00"
                        maxBufferSize="2147483647"
                       maxBufferPoolSize="2147483647"
                       maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                               maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                               maxNameTableCharCount="2147483647" />
              </binding>
            </netTcpBinding>
          </bindings>
      
          <client>
            <endpoint address="net.tcp://localhost:5000/MyService" 
                      binding="netTcpBinding" 
                      contract="IMyService"
                      name="NetTcpBinding_MYService"                
                      bindingConfiguration="netTcpBindingConfiguration" />
          </client>
        </system.serviceModel>
      

      在 ServiceFacade.dll 中,我有如下代码来创建代理

      string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
      Configuration config = ConfigurationManager.OpenExeConfiguration(path);
      ConfigurationChannelFactory<IMyService> chn =
          new ConfigurationChannelFactory<IMyService>(
              "NetTcpBinding_MyService",
              config,
              new EndpointAddress("net.tcp://localhost:5000/MyService"));
      IMyService iMyService = chn.CreateChannel();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        • 2023-03-21
        • 2021-11-16
        相关资源
        最近更新 更多