无法在silverlight应用程序中访问自托管的wcf服务

时间:2012-09-11 16:37:47

标签: c# wcf silverlight service

我正在尝试从我的Silverlight应用程序中获取自托管的WCF服务,但它无法正常工作。该服务配置为使用SSL,我可以使用WCFTestClient工具,一个Web浏览器点击它,我可以引用该服务并从silverlight项目中更新它。问题是当silverlight应用程序试图调用该服务时,它会发生以下错误:

  

服务器未提供有意义的回复;这可能是由于合同不匹配,过早的会话关闭或内部服务器错误造成的。

当我使用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>

2 个答案:

答案 0 :(得分: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);
        }

答案 1 :(得分: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提供策略,否则帖子中的代码应该适合您。