WCF - 如何以编程方式设置绑定配置?
WCF - how programmatically set bindingconfiguration?
现在在客户端 app.config:
<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>
在客户端代码中:
InstanceContext context = new InstanceContext(
new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
new DuplexChannelFactory<IFileChannel>(context, "FileEndPoint");
IFileChannel channel = factory.CreateChannel();
channel.Open();
我试着这样做:
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
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 和一个端点然后分配你的自定义绑定和你的端点(你应该使用一个端口来连接到你的端点)在这些之后简单地创建 ChannelFactory 对象并连接到你的服务。这段代码在我的项目中工作了一年,我只是根据您的代码对其进行了自定义。如果您有任何问题,请直接提问。
完成工作代码如下:
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();
现在在客户端 app.config:
<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>
在客户端代码中:
InstanceContext context = new InstanceContext(
new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
new DuplexChannelFactory<IFileChannel>(context, "FileEndPoint");
IFileChannel channel = factory.CreateChannel();
channel.Open();
我试着这样做:
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
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 和一个端点然后分配你的自定义绑定和你的端点(你应该使用一个端口来连接到你的端点)在这些之后简单地创建 ChannelFactory 对象并连接到你的服务。这段代码在我的项目中工作了一年,我只是根据您的代码对其进行了自定义。如果您有任何问题,请直接提问。
完成工作代码如下:
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();