• 编写WsHttpBinding的WCF通信方式


    这个通信方式本人实验了好久,需要一个重要的条件是服务端和客户端的发送内容方式都是相同的声明,需要在配置文件写入,客户端:

     <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="wsHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"  />
              <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"  />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None" />
                <message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
      </system.serviceModel>

    客户端代码:

      try
                {
                    var myBinding = new WSHttpBinding("wsHttpBinding");//根据后台的binding名字选中后台的binding
                    var myEndpoint =
                        new EndpointAddress(
                            new Uri("http://localhost:12857/UserService.svc"));
                    var myChannelFactory = new ChannelFactory<IUserBussiness>(myBinding, myEndpoint);
                    IUserBussiness client = myChannelFactory.CreateChannel();
                    var res = client.DoWork("1111");
                    myChannelFactory.Close();
                }
                catch (Exception ex)
                {
                    //do something proper here 
                }

    服务端的配置:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="WCFTestImp.UserBussiness" behaviorConfiguration="metadataBehavior">
            <!--这里指定要绑定的binding,contract指定的是WCF的接口-->
            <endpoint bindingConfiguration="wsHttpBindings" address="" name="WCFTestImp.UserBussiness" binding="wsHttpBinding" contract="WCFTestImp.IUserBussiness" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="metadataBehavior">
               <!--为避免泄漏元数据信息,请在部署前将以下值设置为 false--> 
              <serviceMetadata httpGetEnabled="true"/>
               <!--要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> 
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceSecurityAudit auditLogLocation="Default" suppressAuditFailure="True" serviceAuthorizationAuditLevel="SuccessOrFailure" messageAuthenticationAuditLevel="Success" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
        
        <bindings>
          <!--这里声明服务端的binding也是wsHttpBinding的方式-->
          <wsHttpBinding>
            <binding name="wsHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
              <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None" />
                <message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false" />
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
       
      </system.serviceModel>
      <system.webServer>
        
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
            在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
          -->
        <directoryBrowse enabled="true"/>
        
      </system.webServer>
    </configuration>

     不管是BasicHttpBinding还是WSHttpBinding又或者其他的绑定方式,服务端的接口一定要带有标记写法如下:

      [ServiceContract]
        public interface IUserBussiness
        {
            [OperationContract]
            string DoWork(string name);
        }

    实现上面也要有标记,如下:

        [ServiceBehavior]
        public class UserBussiness:IUserBussiness
        {
            public string DoWork(string name)
            {
                return string.Format("hello Word by {0}", name);
            }
        }
  • 相关阅读:
    jpa summary
    web service 参考文档
    jap 事务总结
    jap 事务
    PriorityQueue优先队列深入解析(含源码分析,方法使用)
    【LeetCode】703.数据流中的第K大元素(二叉搜索树解法,java实现)
    做移动端的你竟然不知道Flex布局?快来看看!
    【LeetCode】450.删除二叉搜索树中的节点(java实现,详细图解)
    #linux vscode 保存总提示“Retry as sudo”
    java 面向对象(十八):包装类的使用
  • 原文地址:https://www.cnblogs.com/llcdbk/p/6378179.html
Copyright © 2020-2023  润新知