使用 SSL 找不到资源 wcf 服务
Posted
技术标签:
【中文标题】使用 SSL 找不到资源 wcf 服务【英文标题】:the resource cannot be found wcf service with SSL 【发布时间】:2017-03-07 21:26:31 【问题描述】:我使用 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
【问题讨论】:
您是否在本地机器上通过 HTTPS/HTTP 运行服务? 在本地机器 HTTP 和服务器 HTTPS 上(在服务器上测试) 【参考方案1】:您需要在 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>
【讨论】:
很好的解决方案!以上是关于使用 SSL 找不到资源 wcf 服务的主要内容,如果未能解决你的问题,请参考以下文章