一、 KeyNotFoundException,給定關(guān)鍵字不在字典中
可通過(guò)使用啟用silvlerlight功能的WCF服務(wù)。當(dāng)然,普通的WCF也是可以使用在SL中的,只不過(guò)在配置上比較繁瑣。
二、 無(wú)法序列化
1. 第一類錯(cuò)誤:無(wú)法從未標(biāo)記有 DataContractAttribute 或 SerializableAttribute 的類型繼承類型……
這類錯(cuò)誤可以通過(guò)加上如上提示的屬性解決,但更多時(shí)候需要檢查是否調(diào)用的是“啟用silvlerlight功能的WCF服務(wù)”。
同第一類。也有可能是代碼缺少必要的屬性。
3. 第三類錯(cuò)誤:格式化程序嘗試對(duì)消息反序列化時(shí)引發(fā)異常: 嘗試對(duì)參數(shù)
http://tempuri.org/ 進(jìn)行反序列化時(shí)出錯(cuò)。
同第一類。也可從修改reference.cs中的屬性的namespace解決。
4. 第四類錯(cuò)誤:“Element”命名空間“***”中的“***”并非所需元素。所需元素應(yīng)為“__identity”
同第一類。
PS: 參數(shù)類或者返回值類不能包含任何方法和構(gòu)造函數(shù)。不然也會(huì)出現(xiàn)不能序列化。
三、 MarshalByRefObject
如果調(diào)用的是普通的WCF,則極有可能在reference.cs產(chǎn)生的客戶端代碼中,有System.MarshalByRefObject。這應(yīng)該是微軟WCF的一個(gè)BUG。因?yàn)镾L中根本就沒(méi)有這個(gè)類。查看WEBSERVICE,客戶端代碼中自動(dòng)生成了MarshalByRefObject。那么參照WS,我們也可以生成一個(gè)供WCF使用的MarshalByRefObject。如下:
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
四、 SqlParameter傳值的問(wèn)題
SqlParameter這個(gè)參數(shù)比較特殊,在SL調(diào)用webservice的時(shí)候是支持的,在SL調(diào)用WCF的時(shí)候是不支持。
如果一定要傳這類參數(shù),可以自己創(chuàng)建一個(gè)SqlParameter類,在SL端和WCF端均調(diào)用即可。
六、 典型配置ServiceReferences.ClientConfig
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IYiPinPropWCF">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<extendedProtectionPolicy policyEnforcement="Never" />
</httpTransport>
</binding>
</customBinding>
</bindings>
<client>
bindingConfiguration="CustomBinding_IYiPinPropWCF" contract="ServiceReferenceYiPinProp.IYiPinPropWCF"
name="CustomBinding_IYiPinPropWCF" />
</client>
</system.serviceModel>
</configuration>
6:典型配置Web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="YiPin.QuestionsDbSL.Web.YiPinPropWCFBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<httpTransport/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="YiPin.QuestionsDbSL.Web.YiPinPropWCFBehavior"
name="YiPin.QuestionsDbSL.Web.YiPinPropWCF">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="YiPin.QuestionsDbSL.Web.IYiPinPropWCF" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>