c# - WCF 无法通过 HTTPS 工作

标签 c# wcf

我查看了所有其他解决方案,但没有一个有效。我正在使用 IIS 托管 WCF 服务。以下是我的 web.config(我添加 WCF 服务时的默认设置):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <customErrors mode="Off" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

一切正常,我可以使用 http://www.domain.com/path/service.svc 在浏览器中访问我的 wcf 服务和 https://www.domain.com/path/service.svc - 现在我添加了对 https://www.domain.com/path/service.svc 的 Web 引用在 Windows 控制台应用程序中,以下内容在我的 app.config 中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="defaultBasicHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://www.domain.com/path/service.svc" binding="basicHttpBinding" bindingConfiguration="defaultBasicHttpBinding" contract="DomainExt.IExt" name="BasicHttpBinding_IExt" />
    </client>
  </system.serviceModel>
</configuration>

下面的代码是我用来测试服务的:

public static String DoPing()
{
     ExtClient client = new ExtClient("BasicHttpBinding_IExt", "https://www.domain.com/path/service.svc");
     return client.DoPing();
}

运行后出现以下异常:

There was no endpoint listening at https://www.domain.com/path/service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

内部异常声明:

"The remote server returned an error: (404) Not Found."

此服务在 http 上工作正常,但在 https 上失败。我怎样才能解决这个问题?请注意,我正在使用 Windows 控制台而不是通过 ASP.NET 访问此服务; ASP.NET 仅托管 WCF 服务。

最佳答案

尝试设置 httpsGetEnabled="true"而不是 httpGetEnabled

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpsGetEnabled="true" />

编辑:我也很好奇为什么在使用 SSL 时选择 BasicHttpBinding。为什么不使用 WsHttpBinding?

EDIT2:您是否缺少 <services>主机配置文件中的节点?我不认为你可以创建一个没有客户的客户。您可以右键单击您的 app.config 文件并在那里配置它...像这样:

    <services>
        <service behaviorConfiguration="MyServiceBehavior" name="MyService">
            <endpoint address="" binding="defaultBasicHttpBinding" bindingConfiguration="basicBinding" contract="IMyService" />

        </service>
    </services>
    <behaviors> 
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">

关于c# - WCF 无法通过 HTTPS 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11461613/

相关文章:

c# - 使用 pushsharp 发送存折更新

c# - ASP.net 中报告应用程序的最佳架构(带有动态源)

c# - IntelliTrace 捕获的静默异常

asp.net - 通过 HTTPS 和 HTTP 访问时 WCF 服务不工作

c# - Visual Studio 2013 编码的 UI 测试

c# - 需要使用 win32 api 在 c# 中复制文件 - 我该怎么做?

c# - 如何在与其可执行文件相同的文件夹中启动进程

c# - 更改 System.Uri 的端口

c# - Web 应用程序后端即服务?

WCF 不接收消息 - 如何调试 WCF 服务?