2010-11-15 67 views

回答

1

由于SL只允许来自服务客户端的异步调用(它不一定意味着您的操作在服务器上是异步的)。

你必须做一些事情,如:

ServiceReference1.ServiceClient objService = new ServiceReference1.ServiceClient(); 
objService.GetDataCompleted += OnGetDataCompleted; 
objService.GetData(10,23); 

private void OnGetDataCompleted(object sender, GetDataCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
    string data = e.Result; 
    // Do something with data 
    } 
} 
+0

需要注意的是,SL只允许异步调用的原因是SL在UI线程上执行,非异步会阻止UI更新。您可以在SL中产生线程,但是将所有东西都作为UI应用程序的更好模式并加以实施,避免程序员不得不用繁杂的线程管理代码来代码(尽管它并不那么复杂)。 – 2011-03-24 20:02:25

1

你也可能想在ClientAccessPolicy.xml阅读起来。这里是SO上的good link。简而言之,您的服务必须允许通过ClientAccessPolicy.xml文件授予Silverlight客户端对其域的访问权来调用其自身。这通常通过创建一个服务(可以在托管服务的同一项目中实现)来确保ClientAccessPolicy.xml文件在正确的位置可用。

如果你的服务是自托管的,可以这样代码添加到您的服务启动(主):

 // This service is used to retrieve the client access policy to allow for cross-domain service calls. 
     ServiceHost ClientAccessPolicyService = null; 
     ClientAccessPolicyService = new ServiceHost(typeof(ClientAccessPolicyService)); 
     ClientAccessPolicyService.Open(); 

线这样的(从我们的项目,该项目目前正处于发展的,我敢肯定是)这些设置将我们部署的时间来改善被添加到您的服务的app.config文件:

<service name="ClientAccessPolicyService"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:8731/"/> 
     </baseAddresses> 
    </host> 
    <endpoint address="" 
       binding="webHttpBinding" 
       contract="IClientAccessPolicy" 
       behaviorConfiguration="HttpEnableBehavior"> 
    </endpoint> 
    </service> 

    <endpointBehaviors> 
    <behavior name="HttpEnableBehavior"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 

哪里ClientAccessPolicyService为您服务,提供了ClientAccessPolicy.xml文件和IClientAccessPolicy是OperationContract的。

希望在此信息与上述链接(及其嵌入式链接)中的信息之间,您可以从Silverlight获得您的服务。可能有更多的我离开了,但我真的开始自己与WCF和Silverlight,所以我感到幸运的东西运行!

祝你好运!

相关问题