zoukankan      html  css  js  c++  java
  • WCF常见问题(转)

    WCF常见问题

     

    一、创建时,WCF Service中HttpContext.Current为null的解决办法

    1. 在hosting WCF的web.config中加入:

    <system.serviceModel>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    </system.serviceModel>

    2. 在Service的类定义上加上下面Attribute:

    [AspNetCompatibilityrequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

     

    二、错误提示:调用方未由服务进行身份验证。

    修改WCF服务的web.config文件system.serviceModel下添加配置:

    <bindings>

    <wsHttpBinding>

        <binding name="bindingConfiguration1">

          <security mode="None">

            <transport clientCredentialType="None"/>

            <message clientCredentialType="None"/>

          </security>

        </binding>

    </wsHttpBinding>

    </bindings>

        </system.serviceModel>

    </configuration>

    并修改behaviors下的endpoint,添加bindingConfiguration="bindingConfiguration1"

     

    三、已超过传入消息(65536)的最大消息大小配额

    解决方案1:调用方的代码里加入:

    (client.Endpoint.Binding as WSHttpBinding).MaxReceivedMessageSize = int.MaxValue;

    解决方案2:修改客户端调用方的Config配置文件:

    <system.serviceModel>

        <bindings>

            <wsHttpBinding>

                <binding maxReceivedMessageSize="65536000"

     

    四、调用时服务端返回400 Bad Request错误

    解决方案:修改服务端的Config配置文件,增加maxReceivedMessageSize设置:

    <system.serviceModel>

        <bindings>

            <wsHttpBinding>

                <binding name="bindingConfiguration1" maxReceivedMessageSize="2147483647"

     

    五、 无法打开安全通道,因为与远程终结点的安全协商已失败。

    这可能是由于用于创建通道的 EndpointAddress 中不存在 EndpointIdentity 或错误指定了 EndpointIdentity。

    请确认由 EndpointAddress 指定或暗示的 EndpointIdentity 正确标识了远程终结点。

    这个错误通常是服务端相关配置修改了,删除引用,重新添加引用即可

    六、接收对 http://xxx.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参阅服务器日志。

    1、没有重新生成代理文件

    2、这是因为WCF返回值无法序列化造成的

    WCF的方法,不能返回ObjectICollectionIList之类的不明确的数据类型,但是IList<string>这样的类型可以返回,如果返回IList<SimpleSoft>这样的自定义类型,需要在接口上增加KnownType,如:

    [ServiceContract]

    [ServiceKnownType(typeof(SimpleSoft))]

    public interface ISearchService

     

    七、格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: 方法名。InnerException 消息是“在行 1、位置 1485 出现错误。 元素“http://schemas.datacontract.org /2004/07/父类”含有“http://schemas.datacontract.org/2004/07/子类”数据协定的数据。反序列化程序 不知道映射到此协定的类型。请将与“子类”对应的类型添加到已知类型的列表中,例如,通过使用 KnownTypeAttribute 属性或通过将其添加到传递给 DataContractSerializer 的已知类型的列表等方法。”。有关详细信息,请参阅 InnerException。

    接口返回父类,但是实际返回的是子类,就会出现这个错误,解决方法,在父类定义上添加属性,如:

    [KnownType(typeof(FullSoft))]

    public class SimpleSoft

     

    八、读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。

    解决方案:修改客户端的Config配置文件,增加maxStringContentLength设置:

    <system.serviceModel>

        <bindings><wsHttpBinding><binding name=""....>

    <readerQuotas maxStringContentLength="2147483647"

     

    九、无 法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性。请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompatibilityRequirements 属性添加到服务类型且同时将 RequirementsMode 设置为“Allowed”或“Required”。

    解决办法:

    修改相应   服务.svc.cs

    using System.ServiceModel.Activation ;

    [AspNetCompatibilityRequirements (RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]

     

    十、WCF接口的参数,如果是枚举值,则必须是已经定义的枚举

    比如枚举定义: enum aaa{aa=1, bb=2} 如果参数有aaa类型,传递3就会出错,因为枚举定义里没有3

    如果枚举定义加上Flags属性,就可以传递3了(等于是aaa.aa | aaa.bb

    [Flags]

    enum aaa{aa=1, bb=2}

     

    十一、作为WCF接口的参数,其成员必须有public的set属性,否则不会传递,比如下面的a参数可以在wcf中使用,下面的b参数无法使用:

    public aaa{

        public int a{get;set;}

        public int b{get;private set;}

    }

     

    十二、System.ArgumentException: 此集合已经包含方案 http 的地址。此集合中每个方案中最多只能包含一个地址。 

    WCF 针对每个schema只支持一个绑定,所以站点不能绑定多个主机头来使用WCF,如果绑定多个主机头,可以在Web.config里配置,但是如下配置后,其它主机头无法使用这个wcf

    <serviceHostingEnvironment>

        <baseAddressPrefixFilters>

            <add prefix="http://www.cnblogs.com/"/>

        </baseAddressPrefixFilters>

    </serviceHostingEnvironment>

    十三、在wcf项目下,找到web.config 文件修改如下(蓝色部分)

      <behaviors>
         <serviceBehaviors>
            <behavior>
              <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
      </behaviors>

  • 相关阅读:
    【两周一本书】大话设计模式
    如何将在AWS上的网站快速从http转换为https
    java中error和exception的区别
    Java IO : NIO与IO的区别
    TCP/TP:DNS区域(Zone)
    Liferay 7:Liferay DXP解决方案
    Eclipse:Eclipse插件开发全套教程
    Liferay 7:Liferay DXP全套教程内附源码
    Liferay 7:Liferay内部博客地址
    Gradle:gradle下载插件
  • 原文地址:https://www.cnblogs.com/zpc870921/p/3044939.html
Copyright © 2011-2022 走看看