2011-01-21 60 views
3

我们的Silverlight应用程序可以在两个HTTPHTTPS运行(SSL,使用运输安全)模式。在我们的ServiceReferences.ClientConfig文件,我们只是配置了服务端点是这样的:Silverlight中自动选择之间的HTTP和HTTPS安全传输模式

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

    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="DefaultEndpoint" 
       maxBufferSize="2147483647" 
       maxReceivedMessageSize="2147483647"> 
      <security mode="None" /> 
      <!-- Enable for SSL: mode="Transport" --> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="/services/DefaultService.svc" 
       binding="basicHttpBinding" 
       bindingConfiguration="DefaultEndpoint" 
       contract="OurNamespace.IOurContractAsync" 
       name="DefaultEndpoint" /> 
    </client> 
    </system.serviceModel> 
</configuration> 

配置的端点可以在两种模式下进行访问。它仅取决于加载XAP文件的上下文:从http://example.com/slpage.htmlhttps://example.com/slpage.html。不幸的是,我们必须手动将“None”和“Transport”之间的设置设置为安全模式。其他一切已经按照要求工作。当安全模式是“无”,我们通过https访问,我们得到一个异常,“..提供了..https但预期http ...”,反之亦然。任何让Silverlight自动决定使用哪种安全模式的机会?这个问题最简单的解决方案是什么?

在此先感谢

托马斯

回答

5

我们终于结束了以下解决方案(不完全是瓦伦丁建议,但+1求救!):

ServiceReferences.ClientConfig同时包含绑定和端点配置是这样的:

<configuration> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="DefaultBinding" 
       maxBufferSize="2147483647" 
       maxReceivedMessageSize="2147483647"> 
      <security mode="None" /> 
     </binding> 
     </basicHttpBinding> 
     <customBinding> 
     <binding name="SecureBinding"> 
      <textMessageEncoding messageVersion="Soap12WSAddressing10" /> 
      <httpsTransport maxBufferSize="2147483647" 
          maxReceivedMessageSize="2147483647" /> 
     </binding> 
     </customBinding> 
    </bindings> 
    <client> 
     <endpoint address="/services/DefaultService.svc" 
       binding="basicHttpBinding" 
       bindingConfiguration="DefaultBinding" 
       contract="OurNamespace.IOurContractAsync" 
       name="DefaultEndpoint" /> 
     <endpoint address="/services/DefaultService.svc" 
       binding="customBinding" 
       bindingConfiguration="SecureBinding" 
       contract="OurNamespace.IOurContractAsync" 
       name="SecureEndpoint" /> 
    </client> 
    </system.serviceModel> 
</configuration> 

在初始化,我们读取App.Current.Host.Source.Scheme属性。由ChannelFactory生成服务端,代码类似这样的片段:

protected string EndpointName { 
    get { 
    return (App.Current.Host.Source.Scheme == "https") ? 
     "SecureEndpoint" : "DefaultEndpoint"; 
    } 
} 

protected IOurContractAsync CreateInterface() { 
    var channelFactory = ChannelFactory<IOurContractAsync>(EndpointName); 
    return channelFactory.CreateChannel(); 
} 

希望这有助于!

此致敬礼 托马斯

+0

耶看起来比传递https = true参数更好,如果你可以读它从计划没有问题。 – 2011-02-18 14:48:18

1

我认为可以有多种方式之一是提供initParams在以SL应用程序从网页像

PARAM NAME =“initParams在” 值= “HTTPS =真正的”

为 HTTPS页面和虚假的HTML页面。在SL内部解析它 并为端点设置安全模式 。

您可以在SL应用程序中以编程方式创建/编辑端点代理。

另一种方式可能是基于SL应用程序中没有initparams的链接设置传输行为(如果以https - > transport else开头)我相信一旦sl应用程序被下载,链接应该不是相对的,这应该是一个可行的解决方案。

您可以创建一个工厂方法来创建服务代理,并将此设置代理逻辑放入其中,这将比完全删除此serviceconfig文件更简单。

只需将调用

MyServiceClient client = Factory.MakeClient() 

,我认为它的优雅足够的解决方案。在MakeClient中,您可以决定使用哪种传输安全性及其完成。

+0

感谢您的提示。是的,我已经意识到一些程序化的解决方案,大多数我可以完全抛弃`ServiceReferences.ClientConfig`,我不喜欢这样做... – thmshd 2011-01-21 23:07:22