【问题标题】:Silverlight 4 - Configure Self-Hosted WCF Service to use SSLSilverlight 4 - 配置自托管 WCF 服务以使用 SSL
【发布时间】:2011-05-19 18:36:35
【问题描述】:

我有一个 Silverlight 4 应用程序,它在同一台服务器(自托管)上使用 WCF 服务。一切正常,但现在我想将我的 WCF 服务转换为使用 SSL。我正在使用 CustomBindings 并且找不到完成此操作的组合。我在客户端使用相对 URL,希望这不会引起问题。以下是我的 Web.config 文件的重要部分:

    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"  />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="6553600"/>
          <serviceTimeouts transactionTimeout="00:10:00"/>
        </behavior>
        </serviceBehaviors>
      </behaviors>

    <bindings>
      <customBinding>
        <binding name="MyApp.Web.Services.ProjectService.customBinding0"
          receiveTimeout="00:10:00" sendTimeout="00:10:00">
          <binaryMessageEncoding />
          <httpsTransport maxReceivedMessageSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
  <services>
    <service name="MyApp.Web.Services.ProjectService">
        <endpoint address="" binding="customBinding" bindingConfiguration="MyApp.Web.Services.ProjectService.customBinding0"
          contract="MyApp.Web.Services.ProjectService" />
      </service>

我的 ClientConfig 如下所示:

    <configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_ProjectService">
                    <binaryMessageEncoding />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
           </customBinding>
       </bindings>
       <client>
            <endpoint address="../Services/ProjectService.svc" binding="customBinding"
                bindingConfiguration="CustomBinding_ProjectService" contract="SearchProxy.ProjectService"
                name="CustomBinding_ProjectService" />
     </client>
  </system.serviceModel>
</configuration>

我只是不明白绑定在服务器和客户端中是如何工作的。我希望有人能指出我正确的方向。

【问题讨论】:

  • 你说服务是“自托管的”,但是你发布了一个web.config;服务是否托管在 IIS 中,或者您是否有一个带有您实际打开的 ServiceHost 实例的程序?
  • 我的意思是我的 WCF 服务托管在我的 Silverlight 项目的 Web 项目中。我只想将它们转换为 SSL,但我还需要能够在我的本地机器上调试/开发。我会看看你的答案,看看我能找到什么。谢谢!
  • 当我在我的服务中设置 httpsTransport(并在行为中设置 httpsGetEnabled=true),然后尝试从我的 silverlight 应用程序更新它时,我收到以下错误:“下载时出错地址中的元数据。请验证您输入的地址是否有效”。有什么想法吗?
  • 您更改了服务地址(从 http 到 https),因此您需要“配置服务引用”来更新客户端中的地址。
  • 我提到的错误是我尝试添加新服务引用时遇到的错误。在我的 web.config 中进行上述更改后,我将无法再通过 Visual Studio 访问该服务。

标签: silverlight wcf ssl


【解决方案1】:

一些事情:

如果您想在 localhost 上使用 SSL,则需要使用 IIS Express 7.5(如果您在服务器上进行开发,则需要使用完整的 IIS - 不太可能)。

您需要一个存储在 Web 应用程序根目录中的 clientaccesspolicy.xml 文件:

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

服务器端 Web.config 示例:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="SecureBasicHttpBinding">
        <security mode="Transport">
          <transport clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>

  <behaviors>
    <serviceBehaviors>
      <behavior name="SomeBehavior" >
        <serviceMetadata httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <useRequestHeadersForMetadataAddress>
          <defaultPorts>
            <add scheme="https" port="443" />
          </defaultPorts>
        </useRequestHeadersForMetadataAddress>
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <serviceHostingEnvironment>
    <serviceActivations>
      <add relativeAddress="SomeService.svc" service="MySilverlight.Web.SomeService"/>
    </serviceActivations>
  </serviceHostingEnvironment>

  <services>
    <service name="MySilverlight.Web.SomeService"
             behaviorConfiguration="SomeBehavior">

      <endpoint address="SomeService"
                binding="basicHttpBinding"
                bindingConfiguration="SecureBasicHttpBinding"
                bindingNamespace="https://MySilverlight.Web.SomeService"
                contract="MySilverlight.Web.ISomeService">
      </endpoint>

      <endpoint address="mex"
                binding="mexHttpsBinding"
                contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>

客户端示例:

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
      <system.serviceModel>
          <bindings>
              <basicHttpBinding>
                  <binding name="BasicHttpBinding_ISomeService" maxBufferSize="2147483647"
                       maxReceivedMessageSize="2147483647">
                      <security mode="Transport" />
                  </binding>
              </basicHttpBinding>
          </bindings>
          <client>
              <endpoint address="https://localhost/SomeService.svc/SomeService"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISomeService"
            contract="MySilverlight.Web.SomeServiceReference.ISomeService"
            name="BasicHttpBinding_ISomeService" />
         </client>
    <extensions />
    </system.serviceModel>
</configuration>

IIS 7.5 将自动设置您的本地主机证书。

【讨论】:

  • 一旦我安装了 IIS Express,一切就开始工作了。我不知道VS中内置的Web服务器不支持https。谢谢!
【解决方案2】:

您能否更新客户端项目的服务引用?那应该使用正确的绑定更新 clientconfig 文件。我现在注意到的一件事是您在客户端绑定上使用 并在服务上使用 。尝试将客户端更改为也使用

此外,如果您的 SL 应用程序是从 HTTP:// 地址下载的,那么在 HTTPS 中对服务的调用被视为跨域调用,因此您还需要一个跨域策略文件.

【讨论】:

  • 在进行 web.config 更改后尝试更新或重新配置服务会出现我在其他 cmets 中描述的错误。我还没有找到解决办法。
  • 您能否尝试浏览 .svc 文件以查看它是否显示任何激活错误?
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多