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服務的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.example.com"/>

    </baseAddressPrefixFilters>

</serviceHostingEnvironment>


發佈了64 篇原創文章 · 獲贊 12 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章