WCF RESTful HTTPS支持

时间:2014-06-04 14:27:29

标签: c# asp.net .net web-services wcf

我有[WebInvoke]方法用于ajax调用以获取数据,但在某些情况下,ajax调用应该通过HTTPS协议进行。 如何配置我的方法以使用HTTP和HTTPS连接

这可能是方法

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ChartService
    {           
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json )]
        public GetDataRes GetData(GetDataReq req)
        { 
            res=DB.GetRes(req);
            return res;
        }
    }

这是web.config

  <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <behaviors>
          <endpointBehaviors>
            <behavior name="Won.ICom.Code.Services.ChartServiceAspNetAjaxBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
          <service name="Won.ICom.Code.Services.ChartService">
            <endpoint address="http://localhost:12345/services/ChartService.svc" behaviorConfiguration="Won.ICom.Code.Services.ChartServiceAspNetAjaxBehavior"
             binding="webHttpBinding" contract="Won.ICom.Code.Services.ChartService" />
          </service>
        </services>
    </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

创建第二个终点 在第二个端点将mexHttpBinding更改为mexHttpsBinding。 在serviceMetadata中,我们还需要将httpGetEnabled更改为httpsGetEnabled。

<endpoint address="http://localhost:12345/services/ChartService.svc"      bindingConfiguration="TransportSecurity" binding="wsHttpBinding" contract="Won.ICom.Code.Services.ChartService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>

<serviceBehaviors>
<serviceMetadata httpsGetEnabled="true"/>
</serviceBehaviors>
<bindings>
<webHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>