如何为启用Ajax的WCF Web服务配置HTTPS和HTTP绑定

时间:2012-06-17 13:41:08

标签: ajax wcf sharepoint-2010 https wcf-binding

我在访问https网站中的网络服务时遇到错误。 我认为这是一个配置错误,因为它正在寻找一个https绑定。

这是Web服务的web.config:

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
  <service name="WebService.ListViewWebService" behaviorConfiguration="WebService.ListViewWebServiceBehavior">
    <endpoint address=""
              behaviorConfiguration="WebService.ListViewWebServiceAjaxBehavior"
              binding="webHttpBinding"
              contract="WebService.IListViewWebService"/>
    <endpoint address="https://win-d741qhlbivf:13241/services/DPT/_vti_bin/DPT.WebService/ListViewWebService.svc"
              behaviorConfiguration="WebService.ListViewWebServiceAjaxBehavior1"
              binding="webHttpsBinding"
              contract="WebService.IListViewWebService2"/>
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />         
  </service>
</services>
<bindings>
    <basicHttpBinding>
        <binding name="webHttpBinding">
            <security mode="None" />
        </binding>
        <binding name="webHttpsBinding">
            <security mode="Transport" />
        </binding>
    </basicHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="WebService.ListViewWebServiceAjaxBehavior">
      <webHttp/>
    </behavior>

  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="WebService.ListViewWebServiceBehavior">
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceMetadata httpGetEnabled="true" />
    </behavior> 

  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

提前感谢您的帮助。

此外,我不确定这是否与此问题有关,但是云环境中托管的Web应用程序...我是否需要特殊配置?...

再次感谢..

3 个答案:

答案 0 :(得分:1)

我的一个教程将帮助您:

http://netpl.blogspot.com/2010/05/how-to-programmatically-configure-ssl.html

本教程从声明性配置开始,然后演示如何以编程方式创建绑定/端点。

答案 1 :(得分:0)

此配置将响应HTTP和HTTPS中的请求,请注意端口不同并且必须与主机预期的匹配(在这种情况下,它恰好是iis)

<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
    <compilation debug="true" />

        <customErrors mode="Off"/>
    </system.web>



<system.serviceModel>
    <serviceHostingEnvironment minFreeMemoryPercentageToActivateService="0" />

    <services>
        <service name="KPIGetter_Library.KPIGetter">

            <endpoint address="" 
                    behaviorConfiguration="restfulBehaviour"
                    binding="webHttpBinding" 
                    bindingConfiguration="webHttpBindingWithJsonP"
                    contract="KPIGetter_Library.IKpiGetter" />


            <endpoint address="" 
                    behaviorConfiguration="restfulBehaviour"
                    binding="webHttpBinding" 
                    bindingConfiguration="webHttpsBindingWithJsonP"
                    contract="KPIGetter_Library.IKpiGetter" />

            <host>
                <baseAddresses>
                    <add baseAddress="http://hodbd05:8000/kpigetter" />
                    <add baseAddress="https://hodbd05:8001/kpigetter" />
                </baseAddresses>
            </host>
        </service>
    </services>

    <behaviors>
        <endpointBehaviors>

            <behavior name="restfulBehaviour">
                <webHttp/>
            </behavior>
        </endpointBehaviors>

        <serviceBehaviors>
            <behavior>
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <protocolMapping>
        <add binding="webHttpBinding" scheme="http" />
    </protocolMapping>

    <bindings>
        <webHttpBinding>
                <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true">
                     <security mode="None" />
                </binding>

                <binding name="webHttpsBindingWithJsonP" crossDomainScriptAccessEnabled="true">
                     <security mode="Transport" />
                </binding>
        </webHttpBinding>

    </bindings>
</system.serviceModel>

答案 2 :(得分:0)

一个重要的问题是你应该使用webHttpBinding而不是basicHttpBinding来启用启用了Ajax的WCF服务。

这里有一个简单的工作示例:Scott's blog on asp.net