首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF -如何以编程方式设置bindingconfiguration?

WCF -如何以编程方式设置bindingconfiguration?
EN

Stack Overflow用户
提问于 2021-05-20 14:15:26
回答 2查看 55关注 0票数 0

现在在client app.config中:

代码语言:javascript
复制
<system.serviceModel>
    <client>
      <endpoint name="FileEndPoint" address="net.p2p://ChangingName123/FileServer"
                binding="netPeerTcpBinding" bindingConfiguration="PeerTcpConfig"
                contract="FileClient.IFileService"></endpoint>
     
      
   </client>

    <bindings>
      <netPeerTcpBinding>
        <binding name="PeerTcpConfig" port="0">
          <security mode="None"></security>
          <resolver mode="Custom">
            <custom address="net.tcp://191.14.3.11/FileServer" binding="netTcpBinding"
                    bindingConfiguration="TcpConfig"></custom>
          </resolver>
        </binding>
        
      </netPeerTcpBinding>
      <netTcpBinding>
        <binding name="TcpConfig">
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

在客户端代码中:

代码语言:javascript
复制
InstanceContext context = new InstanceContext(
                        new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
                        new DuplexChannelFactory<IFileChannel>(context, "FileEndPoint");
IFileChannel channel = factory.CreateChannel();
                                        
channel.Open();

我试着这样做:

代码语言:javascript
复制
NetPeerTcpBinding binding = new NetPeerTcpBinding();
            
EndpointAddress endpoint = new EndpointAddress("net.p2p://ChangingName123/FileServer");

InstanceContext context = new InstanceContext(
                        new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
                        new DuplexChannelFactory<IFileChannel>(context, binding, endpoint);

但是"PeerTcpConfig“有custom address="net.tcp://191.14.3.11/FileServer"bindingConfiguration="TcpConfig"

如何在代码中设置绑定自定义地址,并为NetPeerTcpBinding绑定设置多个绑定TcpConfig

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-24 19:12:35

完成工作代码如下:

代码语言:javascript
复制
InstanceContext context = new InstanceContext(new ChatClient(numberclient));
NetPeerTcpBinding binding = new NetPeerTcpBinding();

EndpointAddress endpoint = new EndpointAddress("net.p2p://ChangingName123/FileServer");
binding.Resolver.Custom.Address = new EndpointAddress("net.tcp://191.14.3.11/FileServer");
binding.Security.Mode = SecurityMode.None;
NetTcpBinding ntcp = new NetTcpBinding();
ntcp.Security.Mode = SecurityMode.None;
binding.Resolver.Custom.Binding = ntcp;
factory = new DuplexChannelFactory<IChatChannel>(context, binding, endpoint);
channel = factory.CreateChannel();
票数 0
EN

Stack Overflow用户

发布于 2021-05-24 15:16:19

代码语言:javascript
复制
  private NetTcpBinding binding = new NetTcpBinding();
  private EndpointAddress endPoint;

  private void initial_binding()
    {
        binding.Security.Mode = SecurityMode.None;
        
        binding.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
       
        binding.ReliableSession.Enabled = false;
    }

initial_binding();
endPoint= new EndpointAddress(@"net.tcp://" + 191.14.3.11+ @":4000/FileServer");


 var IAChannelFactory = new ChannelFactory<FileClient.IFileService>(binding, 
 endpoint);
            IFileService Client = new FileService();
            try
             {
                client.anyFunction();
             }
            catch (Exception ex)
             {
                Logger.Log.Error(ex.Message);
             }

第一步,您应该定义netTcpBinding和endPoint,然后分配您的自定义绑定和EndPoint (您应该使用端口连接到您的端点),然后只需创建ChannelFactory对象并连接到您的服务即可。这段代码在我的项目中工作了一年,我只是根据你的代码对它进行了定制。如果你有任何问题,尽管问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67614888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档