我花了一个非常沮丧的日子来调试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