Windows Service通过HTTPS托管WCF

Rob*_*Rob 19 c# wcf windows-services

我已根据MSDN的这些说明创建并配置了SSL证书.我收到此问题列出的错误消息,但不知道如何将该问题中接受的答案映射到我的App.config文件.配置文件的内容和服务本身在http上正常工作,只是在https上发生了问题.

我的App.config文件目前是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="LookupServiceHost" behaviorConfiguration="serviceBehaviour">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:54321/MyService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" contract="ILookupService" bindingConfiguration="TransportSecurity" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehaviour">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Windows事件日志中返回的错误异常:

服务无法启动.System.ServiceModel.AddressAlreadyInUseException:HTTP无法注册URL https:// +:54321/MyService /.另一个应用程序已经使用HTTP.SYS注册了此URL.---> System.Net.HttpListenerException:无法侦听前缀' https:// +:54321/MyService / ',因为它与计算机上的现有注册冲突.

有人可以给我一个指针,说明如何启用它?

Lad*_*nka 28

我认为你正在连接两个不同的设置.Netsh可用于为SSL添加证书,但也允许应用程序在给定端口上侦听,而无需在管理员帐户下运行.例外目标是第二次设置.我之前没见过,但我认为你已经为HTTP注册了这个端口,所以让我们尝试在另一个端口上使用(和注册)HTTPS或者替换以前的注册.

编辑:

使用提升的权限打开命令提示符(作为管理员).首先检查SSL证书是否分配给正确的端口:

netsh http show sslcert
Run Code Online (Sandbox Code Playgroud)

通过调用以下方法检查是否在该端口上注册了HTTP侦听:

netsh http show urlacl 
Run Code Online (Sandbox Code Playgroud)

如果是这样,请使用以下命令删除该注册:

netsh http delete urlacl url=http://+:54321/MyService
Run Code Online (Sandbox Code Playgroud)

再次添加注册以支持监听HTTPS:

netsh http add urlacl url=https://+:54321/MyService user=domain\userName
Run Code Online (Sandbox Code Playgroud)

用户是用于运行Windows服务的帐户.如果它是本地帐户,则仅使用userName.

注意:在https下,似乎必须在urlacl中使用通配符.我们无法编写https://localhost:8733/...匹配Visual Studios默认的urlacl for http.这可能是有意义的,因为请求的主机名在解密之后才可用.