2016-10-25 53 views
2

我使用asp.net VB創建一個REST API,我試圖通過安全連接來調用API(HTTPS),但我有一個錯誤資源不能WCF服務被發現SSL

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

你你的機器上運行通過HTTPS/HTTP服務在本地? – Mark

+0

HTTP本地計算機上,並在服務器HTTPS(在服務器上測試) – Diyaa

回答

3

你需要在你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> 

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

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

這將有效地在切換服務,HTTPS,但如果你想有HTTP和HTTPS工作,你需要定義2種綁定配置,然後讓每個服務,其中一個使用http bindingConfiguration和其他2個相同的端點使用https bindingConfiguration像這樣:

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

的最佳解決方案! – netosone

+0

<安全模式=「運輸」>已存我! –