gpt4 book ai didi

asp.net - 无法使用 SSL 找到资源 wcf 服务

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:36 25 4
gpt4 key购买 nike

我使用 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 中定义一个特殊的绑定(bind)配置文件以允许 SVC 服务正确绑定(bind) 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 定义特殊绑定(bind)设置因此,修复 HTTPS 请求的神奇部分是 <security mode="Transport" /> :

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

这将有效地将服务切换到 HTTPS,但是如果您希望同时使用 HTTP 和 HTTPS,您需要定义 2 个绑定(bind)配置,然后每个服务有 2 个相同的端点,其中一个使用 http bindingConfiguration另一个使用 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>

关于asp.net - 无法使用 SSL 找到资源 wcf 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com