1

我有一个客户端,它是一个名为 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");

4

2 回答 2

1

您绝对可以将配置复制ServiceFacade.dll.configWindows.exe.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);
    }

希望对你有用。

于 2020-04-01T06:36:37.793 回答
0

我确实喜欢下面。

我在下面添加了 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();
于 2020-03-27T21:40:59.307 回答