2012-07-19 164 views
0

我試圖在Visual Studio 2010中創建一個WCF服務,它似乎工作,因爲當我調試VS使用WCF測試客戶端時,我可以連接到該服務並運行命令。但是,當我嘗試從我的Silverlight應用程序運行命令時,出現錯誤,說我的跨域不起作用。我已經包含了我的Silverlight應用程序和wcf客戶端的代碼。Silverlight + WCF服務

服務

clientaccesspolicy.xml

`<?xml version="1.0" encoding="utf-8"?> 
<access-policy> 
    <cross-domain-access> 
    <policy> 
     <allow-from http-request-headers="SOAPAction"> 
     <domain uri="*"/> 
     </allow-from> 
     <grant-to> 
     <resource path="/" include-subpaths="true"/> 
     </grant-to> 
    </policy> 
    </cross-domain-access> 
</access-policy>` 

的app.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="SqlServerServiceBinding" maxBufferPoolSize="2147483647" 
       maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

    <!--<behaviors> 
     <serviceBehaviors> 
     <behavior name="SqlServerServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors>--> 

    <services> 
     <service name="SqlServerService.Service1"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost:8085/" /> 
      </baseAddresses> 
     </host> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address ="" binding="basicHttpBinding" contract="SqlServerService.IService1"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
      <!--<identity> 
      <dns value="localhost"/> 
      </identity>--> 
     </endpoint> 
     <!-- Metadata Endpoints --> 
     <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
     <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="True"/> 
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true. Set to false before deployment 
      to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 

service1.c小號

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace SqlServerService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
    public class Service1 : IService1 
    { 
     public CompositeType executeScriptWithReturn(String command_string) 
     { 
      CompositeType cs = new CompositeType(); 
      Userful_Commands.sql_server_commands sql = new Userful_Commands.sql_server_commands("Server=localhost;Database=kenticoTest;Integrated Security=SSPI;"); 

      cs.DtValue = command_string;//sql.executeScriptWithReturn("SELECT * FROM tblBugs"); 
      return cs; 
     } 
    } 
} 

IService1.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace SqlServerService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     CompositeType executeScriptWithReturn(String command_string); 

     // TODO: Add your service operations here 
    } 

    // Use a data contract as illustrated in the sample below to add composite types to service operations 
    [DataContract] 
    public class CompositeType 
    { 
     public String dt; 

     [DataMember] 
     public String DtValue 
     { 
      get { return dt; } 
      set { dt = value; } 
     } 
    } 
} 

SILVERLIGHT

ServicesReferences.ClientConfig

<configuration> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" 
      maxReceivedMessageSize="2147483647"> 
      <security mode="None" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:8085/" binding="basicHttpBinding" 
     bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" 
     name="BasicHttpBinding_IService1" /> 
    </client> 
    </system.serviceModel> 
</configuration> 

MainPage.xaml.cs中

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Silverlight; 
using SilverlightSQLServer; 

namespace SilverlightApplication1 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void loaded_up(object sender, RoutedEventArgs e) 
     { 
      //SilverlightApplication1.ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client(); 
      //sc.executeScriptWithReturnCompleted += completed; 
      //sc.executeScriptWithReturnAsync("TEST COMMAND"); 
      ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client(); 

      ws.executeScriptWithReturnCompleted += completed; 
      ws.executeScriptWithReturnAsync("TESTCOMMAND"); 
     } 

     private void completed(object sender, SilverlightApplication1.ServiceReference1.executeScriptWithReturnCompletedEventArgs e) 
     { 
      SilverlightApplication1.ServiceReference1.CompositeType serviceResp = e.Result; 
      e = e; 

     } 
    } 
} 

預先感謝您。

+0

你得到的實際錯誤是什麼?您的客戶端訪問策略是否位於託管服務的根目錄?另外,如何使'allow-from http-request-headers =「*」'代替SOAPAction – Thelonias 2012-07-19 18:29:24

回答

0

我已經與自己搏鬥了。我找到的最簡單的解決方案是將WCF服務放在託管Silverlight應用程序的解決方案中。右鍵單擊該項目(具有ClientBin文件夾的.Web文件)選擇添加項並選擇WCF服務。將您的代碼放入該服務中,您的跨域問題就會消失。當然,如果你想在其他地方託管服務,那麼這不會對你有所幫助。