为什么我无法访问Windows服务中托管的WCF?

时间:2020-01-02 13:31:27

标签: wcf windows-services

我在Windows服务中托管了WCF服务。

当我尝试访问该服务时,出现以下错误消息。

无法建立连接,因为目标计算机主动拒绝了127.0.0.1:9002

内部异常

http://localhost:9002/MainService/Service上没有端点可以接受该消息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参见InnerException(如果存在)。

但是端点已在WCF的App配置中定义如下。

<system.serviceModel>
    <services>
      <service name="MainService.CalculatorService">
        <endpoint address="CalculatorService" binding="basicHttpBinding" contract="MainService.ICalculator">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9002/MainService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端的Web配置

<client>
    <endpoint address="http://localhost:9002/MainService/CalculatorService"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INoiseCalculator"
    contract="Service.ICalculator" name="BasicHttpBinding_INoiseCalculator" />
</client>

在Controller中,我正在按以下方式访问服务

MainService.CalculatorClient proxy = new MainService.CalculatorClient();
proxy.getDetails();

我也打开了防火墙中的端口。

我无法弄清楚出了什么问题,因为当服务在WccSvcHost中进行自我托管时,它可以正常工作,但是在部署之后,它将无法正常工作。

1 个答案:

答案 0 :(得分:1)

服务运行状态似乎有问题。为了验证这一点,我们首先可以讨论客户端端点。

<client>
    <endpoint address="http://localhost:9002/MainService/CalculatorService"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INoiseCalculator"
    contract="Service.ICalculator" name="BasicHttpBinding_INoiseCalculator" />
</client>

合同是Service.ICalculator,而用于实例化客户端代理的名称空间是MainService

MainService.CalculatorClient proxy = new MainService.CalculatorClient();
proxy.getDetails();

是否通过添加服务引用自动生成了客户端服务端点?为什么命名空间不一致?
我建议您通过在客户端添加服务引用来再次生成客户端端点,这样我们就可以检查服务是否运行良好。
enter image description here
关于通过添加服务引用来调用服务。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
随时让我知道问题是否仍然存在。