c# - netpeertcpbinding 是否支持请求/回复

标签 c# wcf wcf-binding

我有一个 WCF p2p 网状网络,它运行良好,适合单向对话。我正在研究是否可以调用一种方法来添加两个数字并返回和总和。

但是我在尝试连接时遇到错误:

契约(Contract)需要请求/回复,但绑定(bind)“NetPeerTcpBinding”不支持它或未正确配置以支持它。

C# 连接

private void button1_Click(object sender, EventArgs e)
{
   try
   {
      // Construct InstanceContext to handle messages on callback interface. 
      // An instance of ChatApp is created and passed to the InstanceContext.
      InstanceContext instanceContext = new InstanceContext(new ChatApp(radTextBoxusername.Text, this));

      // Create the participant with the given endpoint configuration
      // Each participant opens a duplex channel to the mesh
      // participant is an instance of the chat application that has opened a channel to the mesh
      factory = new DuplexChannelFactory<IChatChannel>(instanceContext, "ChatEndpoint");
      participant = factory.CreateChannel();

      // Retrieve the PeerNode associated with the participant and register for online/offline events
      // PeerNode represents a node in the mesh. Mesh is the named collection of connected nodes.

      IOnlineStatus ostat = participant.GetProperty<IOnlineStatus>();
      ostat.Online += new EventHandler(OnOnline);
      ostat.Offline += new EventHandler(OnOffline);

      try
      {
         participant.Open();
      }

      catch (CommunicationException)
      {
         radListViewChats.Text = radListViewChats.Text + ("Could not find resolver.  If you are using a custom resolver, please ensure");
         radListViewChats.Text = radListViewChats.Text + ("that the service is running before executing this sample.  Refer to the readme");
         radListViewChats.Text = radListViewChats.Text + ("for more details.");
         return;
      }

      radListViewChats.Text = radListViewChats.Text +("You are connected: " + radTextBoxusername.Text);

      // Announce self to other participants
      participant.Join(radTextBoxusername.Text);

      }

      catch (Exception ex)
      {
         radListViewChats.Text = radListViewChats.Text + ex.Message.ToString();
         MessageBox.Show(ex.Message.ToString());
      }
    }

将 2 个数字相加的 C# 代码

public int Add(int number1, int number2)
{
   try
   {
      return number1 + number2;
   }

   catch (Exception ex)
   {
      form.SetLogText(ex.Message.ToString());
      return -1;
   }
}

IChat

namespace Client
{
    // Chat service contract
    // Applying [PeerBehavior] attribute on the service contract enables retrieval of PeerNode from IClientChannel.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", CallbackContract = typeof(IChat))]
    public interface IChat
    {
        [OperationContract(IsOneWay = true)]
        void Join(string member);

        [OperationContract(IsOneWay = true)]
        void Chat(string member, string msg);

        [OperationContract(IsOneWay = true)]
        void Leave(string member);


        [OperationContract(IsOneWay = false)]
        int Add(int number1, int number2);
    }

    public interface IChatChannel : IChat, IClientChannel
    { }
}

ChapApp

public class ChatApp : IChat
{    
    // member id for this instance
    string member;
    Form1 form;

    public ChatApp(string member, Form1 form)
    {
        this.member = member;
        this.form = form;
    }

    //IChat implementation
    public void Join(string member)
    {
        form.SetLogText(member + " joined");
    }

    public void Chat(string member, string msg)
    {
        try
        {               
           //Comment out this if statement if you wish to echo chats from the same member
           if (member != this.member)
           {
               form.SetLogText(msg);
           }
        }

        catch (Exception ex)
        {
           form.SetLogText(ex.Message.ToString());
        }
    }

    public int Add(int number1, int number2)
    {
       try
       {
            return number1 + number2;
       }

       catch (Exception ex)
       {
          form.SetLogText(ex.Message.ToString());
          return -1;
       }

    }

    public void Leave(string member)
    {
        form.SetLogText(member + " left the chat");
    }
}

App.config

<?xml version="1.0"?>
<configuration>

  <system.serviceModel>

    <client>
      <!-- chat instance participating in the mesh -->
      <endpoint name="ChatEndpoint" address="net.p2p://chatMesh/ServiceModelSamples/Chattest" binding="netPeerTcpBinding" bindingConfiguration="BindingCustomResolver" contract="Client.IChat">
      </endpoint>

    </client>

    <bindings>
      <netPeerTcpBinding>
        <!-- Refer to Peer channel security samples on how to configure netPeerTcpBinding for security -->
        <binding name="BindingCustomResolver" port="0">
          <security mode="None"/>
          <resolver mode="Custom">
            <custom address="net.tcp://localhost/servicemodelsamples/peerResolverService" binding="netTcpBinding" bindingConfiguration="Binding3"/>
          </resolver>
        </binding>
      </netPeerTcpBinding>

      <netTcpBinding>
        <!-- You can change security mode to enable security -->
        <binding name="Binding3">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>

  </system.serviceModel>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

最佳答案

看起来这篇文章的答案是否定的

WCF

关于c# - netpeertcpbinding 是否支持请求/回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300893/

相关文章:

c# - 如何在 C# 中将 byte[] 转换为日期时间?

c# - Datagridview 删除所有列

c# - 无法分配 RepeatedField 类型的属性或索引器 — 它是只读的

c# - SOAP 是简单对象访问协议(protocol)还是面向服务的应用程序平台?

c# - 在没有代理的情况下调用网络服务

wcf - 指定与 WCF 客户端一起使用的传出 IP 地址

C# 转换与解析

WCF net.tcp SSL、证书和用户名+密码认证

c# - 从 webHttpBinding 端点获取元数据

wcf - 使用 TcpClientCredentialType.Windows 时的加密强度是多少?