WCF RESTful HTTPS support

本文关键字:support HTTPS RESTful WCF | 更新日期: 2023-09-27 18:06:35

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

this is may method

    [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;
        }
    }

this is 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>

WCF RESTful HTTPS support

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

<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>