我花了一個非常沮喪的日子來調試Hello World Silverlight Web應用程序。該應用程序通過Windows Communication Foundation(WCF)與託管在遠程服務器上的Hello World Web服務進行通信。C#中WCF服務需要STAThread嗎?
起初,應用不斷給我下面的錯誤:
An error occurred while trying to make a request to URI
'http://remoteServer/service'. This could be due to attempting to
access a service in a cross-domain way without a proper cross-domain policy
in place, or a policy that is unsuitable for SOAP services. You may need to
contact the owner of the service to publish a cross-domain policy file and
to ensure it allows SOAP-related HTTP headers to be sent.
對谷歌一陣搜索後,我以爲我遇到的常見跨域問題就來了。所以我嘗試將跨站點策略文件添加到Web根文件夾。但是這並沒有消除錯誤。
幸運的是,我無意間看到其在Program.cs中的Web服務的的主要功能前面的「[STAThread]」關鍵字WCF服務在線的代碼。所以我做了同樣的事情,即在主要方法之前添加[STAThread]。這個簡單的技巧神奇地解決了這個問題。但我不知道這背後的機制。任何人都可以向我解釋?
順便說一句:另一個問題是,當我在localhost上託管服務時,上述錯誤又回來了,即Silverlight應用程序無法與服務通信。但是控制檯應用程序可以成功與服務通信。我想知道是否這是因爲本地主機禁用Silverlight應用程序所需的某些系統服務,以便在服務器提供WCF時使用它。
WCF服務使用多少線程或什麼類型的線程完全獨立於跨域訪問。我看不出爲什麼這個「固定」問題的原因,我認爲這只是一個巧合。 – 2012-08-07 16:53:44
@PeterRitchie我有同樣的推理,因此變得非常困惑 – sma 2012-08-09 14:32:35