2012-09-11 95 views
0

我想從我的Silverlight應用程序中創建自託管的WCF服務,但它不起作用。該服務配置爲使用SSL,我可以使用WCFTestClient工具(一個Web瀏覽器)打開它,並且我可以引用該服務並在Silverlight項目中對其進行更新。當Silverlight應用程序試圖撥打電話到服務的問題是,它的炸彈,出現以下錯誤:無法在Silverlight應用程序中訪問自託管的wcf服務

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

當我打它使用WCF測試客戶端工具,它返回沒有問題的數據(預期) 。

任何想法?

下面是我的應用程序配置:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="basicHttp" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="6553600" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> 
      <security mode="Transport"> 
      </security> 
     </binding> 
     </basicHttpBinding> 
     <webHttpBinding> 
     <binding name="webHttp" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="6553600" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> 
      <security mode="Transport"> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <services> 
     <service name="Application.ServiceModel.ApplicationService" behaviorConfiguration="serviceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="https://192.168.1.171:8000/ApplicationServiceModel/service"/> 
      </baseAddresses> 
     </host> 
     <endpoint address="" 
        binding="basicHttpBinding" 
        bindingConfiguration="basicHttp" 
        contract="Application.ServiceModel.IApplicationService" /> 
     <endpoint address="mex" 
        binding="mexHttpsBinding" 
        contract="IMetadataExchange"/> 
     <endpoint address="http://localhost:8000/" 
        binding="webHttpBinding" 
        contract="Application.ServiceModel.IApplicationService" 
        behaviorConfiguration="webHttpBehavior" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="serviceBehavior"> 
      <serviceMetadata httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="webHttpBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 

回答

1

Silverlight客戶端無法在默認情況下,讓網絡請求不到哪裏被加載的.xap文件之一(即跨域請求)等領域。如果您想訪問自託管WCF服務(這意味着一個X域請求),則該服務必須向Silverlight運行時提供一個策略文件,表示可以進行此類請求。

您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2008/03/07/enabling-cross-domain-calls-for-silverlight-apps-on-self-hosted-web-services.aspx的帖子中找到如何爲自助服務啓用跨域調用的示例。由於您的服務使用HTTPS,因此您還需要使用HTTPS提供策略,否則該帖子中的代碼應該適合您。

+0

謝謝!非常感謝!!! – spyter

+0

問題,有沒有一種方法可以在使用xml的clientconfig文件中定義這個? – spyter

+0

不,它是*服務器*決定它是否可以跨域方式調用,而不是客戶端調用(我假設您正在討論ServiceReferences.clientconfig文件)。這需要在服務器端完成。 – carlosfigueira

3

@carlos:你的代碼對於http很有用。但是,我無法讓它爲https工作。我只是得到「clientaccesspolicy.xml」找不到......

我做了這些修改主要功能:

   public static void Main() 
     { 
      string baseAddress = "https://" + Environment.MachineName + ":8000"; 
      ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
      BasicHttpBinding basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); 
      host.AddServiceEndpoint(typeof(ITest), basicHttpBinding, "basic"); 
      WebHttpBinding webHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport); 
      HttpTransportSecurity httpTransportSecurity = webHttpBinding.Security.Transport; 
      httpTransportSecurity.ClientCredentialType = HttpClientCredentialType.None; 
      httpTransportSecurity.ProxyCredentialType = HttpProxyCredentialType.None; 
      WebHttpBehavior webHttpBehavior = new WebHttpBehavior(); 

      host.AddServiceEndpoint(typeof(IPolicyRetriever), webHttpBinding, "").Behaviors.Add(webHttpBehavior); 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpsGetEnabled = true; 
      host.Description.Behaviors.Add(smb); 
      host.Open(); 
      Console.WriteLine("Host opened"); 
     } 

此外,我做了這些修改對GetSilverlightPolicy()函數:

     public Stream GetSilverlightPolicy() 
     { 
      string result = 
       @"<?xml version=""1.0"" encoding=""utf-8""?> 
        <access-policy> 
         <cross-domain-access> 
          <policy> 
           <allow-from http-request-headers=""SOAPAction""> 
            <domain uri=""https://""/> 
            <domain uri=""http://""/> 
           </allow-from> 
           <grant-to> 
            <resource path=""/"" include-subpaths=""true""/> 
           </grant-to> 
          </policy> 
         </cross-domain-access> 
        </access-policy>"; 
      return StringToStream(result); 
     } 
相關問題