Asp.net

使用 SSL 找不到資源 wcf 服務

  • February 10, 2017

我使用 asp.net vb 創建了一個 REST api,我試圖通過安全連接 (https) 呼叫 api,但出現錯誤

The resource cannot be found

我可以使用 (http) 呼叫任何方法,但使用 (https) 我不能。我可以使用(https)訪問api(service.svc)的首頁,但是功能有問題!下面是我的配置和函式頭。

   <system.serviceModel>


  <services>
 <service name="RESTAPI" behaviorConfiguration="MyServiceTypeBehaviors">

   <endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="RESTAPI"/>

<endpoint address="" behaviorConfiguration="HerbalAPIAspNetAjaxBehavior"
     binding="webHttpBinding" contract="HerbalAPI"  />

   <endpoint contract="RESTAPI" binding="mexHttpBinding" address="mex" />


    </service>

</services>

 <!-- **** Services ****-->


<behaviors>

 <serviceBehaviors>
   <behavior name="MyServiceTypeBehaviors">
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
     <serviceDebug includeExceptionDetailInFaults="true"/>
   </behavior>
 </serviceBehaviors>


 <endpointBehaviors>
   <behavior name="HerbalAPIAspNetAjaxBehavior">
     <webHttp helpEnabled="true" />
   </behavior>
 </endpointBehaviors>

</behaviors>
<bindings>
 <customBinding>
   <binding name="basicConfig">
     <binaryMessageEncoding/>
     <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
   </binding>
 </customBinding>

</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
 multipleSiteBindingsEnabled="true" />

API 類

<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class RESTAPI
<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)>
Public Function test(ByVal st As String) As JSONResultString
//any code
End Function
End Class

您需要在web.config文件中定義特殊的綁定配置,以允許 SVC 服務正確綁定 HTTPS 請求。

請看這篇博文:https ://weblogs.asp.net/srkirkland/wcf-bindings-needed-for-https

您的服務已經在 中定義web.config,只需添加bindingConfiguration屬性:

<services>
 <service name="TestService">
   <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
     binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
 </service>
</services>

然後為 asso 定義特殊綁定設置webHttpBinding,修復 HTTPS 請求的神奇部分是<security mode="Transport" />

<bindings>
 <webHttpBinding>
   <binding name="webBindingHttps">
    <security mode="Transport">
    </security>
   </binding>
 </webHttpBinding>
</bindings>

這將有效地將服務切換到 HTTPS,但是如果您希望 HTTP 和 HTTPS 都可以工作,您需要定義 2 個綁定配置,然後每個服務有 2 個相同的端點,其中一個使用 http bindingConfiguration,另一個使用 httpsbindingConfiguration像這樣:

<bindings>
 <webHttpBinding>
   <binding name="webBindingHttps">
    <security mode="Transport">
    </security>
   </binding>
   <binding name="webBindingHttp">
     <!-- Nothing special here -->
   </binding>
 </webHttpBinding>
</bindings>

<services>
 <service name="TestService">
   <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
     binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
   <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
     binding="webHttpBinding" bindingConfiguration="webBindingHttp" contract="TestService" />
 </service>
</services>

引用自:https://stackoverflow.com/questions/40246585