簡體   English   中英

來自其他配置文件的 C# NetTcpBinding 客戶端綁定

[英]C# NetTcpBinding Client bindings from other config file

我有一個客戶端,它是一個名為 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 而不是 ServiceFacade.dll.config 中尋找 NetTcpBinding_MyService

如何使下面的行看到 ServiceFacade.dll.config 中的 NetTcpBinding_MyService 而不是 Windows.exe.config ?

NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");

我確實喜歡下面。

我在 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();

您絕對可以將配置從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);
    }

希望對你有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM