• C# WCF



    wcf模式分为单工、双工、半双工三种模式

    1. Tcp服务-双工

    服务端

    双工定义:客户端 访问 服务器方法 回调 客户端方法

    服务端接口类:IChatService

    • 服务器添加回调接口类IChatServiceCallBack ,定义回调方法

          public interface IChatServiceCallBack
          {
              [OperationContract]
              void HelloClient(string msg);
          }
      
    • 签订回调契约:该契约签订在服务器接口类上方,接口方法中可以调用已经签订签约的回调方法

          [ServiceContract(CallbackContract =typeof(IReportServiceCallback))]
          public interface IChatService
          {
              [OperationContract]
              void HelloServer(string msg);
          }
      

    服务端继承接口类:ChatService : IChatService

    • 签订服务行为:实现接口的类需要声明服务行为

      [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
      
    • 回调

      public void HelloServer(string msg)
      {
          Console.WriteLine(msg);
          OperationContext.Current.GetCallbackChannel<IChatServiceCallBack>().HelloClient("SayHello from server");
      }
      

    客户端

    • 添加Wcf服务引用

    • 继承回调接口并且实现

    public void HelloClient(string msg)
    {
        textBox1.Text = msg;
    }
    
    • 回调函数声明

      [CallbackBehavior(UseSynchronizationContext = false)]

    • 调用服务器接口

      InstanceContext instanceContext = new InstanceContext(this);
      ChatServiceClient client = new ChatServiceClient(instanceContext);
      client.HelloServer("Sayhello from Client");
      

    2. WCF解决(413) Request Entity Too Large

    传输内容过大, wcf数据传输采用的默认的大小是65535字节

    • 服务端配置
    <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <!--设置 关闭超时 接收超时 发送超时 传送最大字节数  -->
          <binding   closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
        </basicHttpBinding>
      </bindings>
    <system.serviceModel>
    
    • 客户端配置
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            </binding>
          </basicHttpBinding>
        </bindings>
    <system.serviceModel>    
    
  • 相关阅读:
    LVS集群的ipvsadm命令用法
    opencv学习HighGUI图形用户界面初步【1】
    openc下cv::Mat和IplImage的相互转换
    QT的creator中图示
    android的数据与访问(2)-delphi xe7如何存取我的app配置参数文件?
    win7下qt+opencv的环境配置
    android的数据与访问(1)-我的app配置参数文件放在哪儿?
    xe7android调用webservice
    android.permission
    在win7下,easyphp安装过程中MSVCR110.DLL没有被指定在WINDOWS上运行,或者它包含错误
  • 原文地址:https://www.cnblogs.com/tangpeng97/p/14343830.html
Copyright © 2020-2023  润新知