zoukankan      html  css  js  c++  java
  • 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>    
    
  • 相关阅读:
    第11条:谨慎地覆盖clone
    第10条:始终要覆盖toString
    第9条:覆盖equals时总是覆盖hashCode
    第8条:覆盖equals时请遵守通用约定
    第7条:避免使用终结方法
    第6条:消除过期的对象引用
    第5条:避免创建不必要的对象
    第4条:通过私有构造器来强化不可实例化能力
    第3条:用私有构造器或者枚举类型强化Singleton属性
    第2条:遇到多个构造器参数时要考虑用构建器
  • 原文地址:https://www.cnblogs.com/tangpeng97/p/14343830.html
Copyright © 2011-2022 走看看