最近在學習WCF技術,在學習到WCF拓展時,發現一個奇怪的問題。
就是在使用自訂拓展元素時,如果按照下面的方式在設定檔註冊拓展元素(設定檔手寫):
<add name="ContextPropagationExtension" type="HXW.WCF.Extension.ContextPropagationBehaviorElement, HXW.WCF.Extension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
程式運行時會拋出一個異常:
未處理 System.Configuration.ConfigurationErrorsException
Message="建立 system.serviceModel/behaviors 的配置節處理常式時出錯: 無法將擴充元素“ContextPropagationExtension”添加到此元素中。請驗證該擴充是否已在 system.serviceModel/extensions/behaviorExtensions 中的擴充集合中註冊。\r\n參數名: element (F:\\hxw\\wcf\\HXW.WCF\\HXW.WCF.Host\\bin\\Debug\\HXW.WCF.Host.vshost.exe.Config line 10)"
研究了兩天都解決不了。
最後google了才發現是微軟的BUG。具體看下面的連結:
https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431
具體解決方式如下:
把上面那段配置改為:
<add name="ContextPropagationExtension" type="HXW.WCF.Extension.ContextPropagationBehaviorElement, HXW.WCF.Extension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
(就是在,後面加上一個空格)
就正常了。所以大家手寫設定檔一定要注意。