加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:50:33 所属栏目:asp.Net 来源:网络整理
导读:我使用asp.net vb创建了一个REST api,我试图通过安全连接(https)调用api,但是我遇到了错误 The resource cannot be found 我可以使用(http)调用任何方法,但使用(https)我不能.我可以使用(https)访问api(service.svc)的主页但功能问题!!下面是我的配置和功能
我使用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>

然后定义webHttpBinding的特殊绑定设置,因此修复HTTPS请求的神奇部分是< security mode =“Transport”/>:

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

这将有效地将服务切换到HTTPS,但是如果要同时使用HTTP和HTTPS,则需要定义2个绑定配置,然后每个服务有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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读