C# 未能调用该服务。可能原因:调用时服务脱机或不可访问

C# 未能调用该服务。可能原因:调用时服务脱机或不可访问,c#,.net,wcf,rest,C#,.net,Wcf,Rest,我的服务配置如下: <system.serviceModel> <services> <service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors"> <host> <baseAddresses> <add b

我的服务配置如下:

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://127.0.0.1:808/service" />
          </baseAddresses>
        </host>
        <endpoint address="net.tcp://127.0.0.1:808/service/"
                  binding="netTcpBinding"
                  contract="WcfService1.IService1"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"   />

      </service>
    </services>
    <protocolMapping>
      <add scheme="net.tcp" binding="netTcpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="MyEndpointBehaviour">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户为:

 <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService1" sendTimeout="00:05:00" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://127.0.0.1/service/" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IService1" contract="IService1"
                name="NetTcpBinding_IService1">
                <identity>
                    <servicePrincipalName value="host/MachineName" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

使用WCFTestClient或SVCutil时,我能够发现和访问服务并生成代理或存根

但当我想调用任何方法时,会出现以下错误:

未能调用该服务。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复到默认配置或刷新服务来进行恢复

套接字连接被中止。这可能是由于处理消息时出错、远程主机超过接收超时或基础网络资源问题造成的。本地套接字超时为“00:04:59.9980468”


也许不是你想要的答案,但是对于在本地机器上的开发,我总是使用HTTP端点。我发现Net.TCP只有在IIS中的服务器上托管时才能发挥作用

一旦部署到IIS服务器,添加NET.TCP端点就很容易了。注意:基址不再有任何影响,因为它是在IIS中设置的。要测试新端点,最好在浏览器中打开服务,同时将其远程插入服务器,这样可以获得最佳的错误消息

如果我理解正确,您在创建服务引用时正在尝试选择端点?这就是它的工作原理。 如果您使用
127.0.0.1/service/Service1.svc

您应该在配置文件中看到如下内容。(当我创建我的服务端点时,我总是用协议作为后缀来命名它们

        <endpoint address="http://servername:8090/servername/servername.svc/Business"
              binding="basicHttpBinding" bindingConfiguration="BusinessHTTP"
              contract="servername.IService" name="BusinessHTTP" />
       <endpoint address="net.tcp://servername:8030/Service/Service.svc/Business"
              binding="netTcpBinding" bindingConfiguration="BusinessTCP"
              contract="servername.IService" name="BusinessTCP" />

也许不是你想要的答案,但是为了在本地机器上进行开发,我总是使用HTTP端点。我发现Net.TCP只有在IIS中的服务器上托管时才能发挥作用

一旦部署到IIS服务器,添加NET.TCP终结点就很容易了。注意:基址不再像在IIS中设置的那样有任何影响。要测试新的终结点,最好在浏览器中打开服务,同时将其远程连接到服务器,这样可以获得最佳的错误消息

如果我理解正确,那么在创建服务引用时您正在尝试选择端点?这就是它现在的工作方式。 如果您使用
127.0.0.1/service/Service1.svc

您应该在配置文件中看到如下内容(当我创建服务端点时,总是使用协议作为后缀来命名它们)

        <endpoint address="http://servername:8090/servername/servername.svc/Business"
              binding="basicHttpBinding" bindingConfiguration="BusinessHTTP"
              contract="servername.IService" name="BusinessHTTP" />
       <endpoint address="net.tcp://servername:8030/Service/Service.svc/Business"
              binding="netTcpBinding" bindingConfiguration="BusinessTCP"
              contract="servername.IService" name="BusinessTCP" />

在端点块内设置以下值

<identity>
   <dns value ="localhost"/>
</identity>

在端点块内设置以下值

<identity>
   <dns value ="localhost"/>
</identity>

默认情况下,NetTcp绑定是安全的。在客户端和服务之间的安全身份验证过程中,WCF确保服务的标识与此元素的值匹配。因此,服务需要配置为:

<identity>
   <dns value ="localhost"/>
</identity>

默认情况下,NetTcp绑定是安全的。在客户端和服务之间的安全身份验证过程中,WCF确保服务的标识与此元素的值匹配。因此,服务需要配置为:

<identity>
   <dns value ="localhost"/>
</identity>

我也遇到了同样的问题。这是EndpointAddress的端口地址问题。在Visual studio中,您文件的端口地址(例如Service1.svc)和wcf项目的端口地址必须与您在EndpointAddress中给出的端口地址相同。让我详细描述一下这个为我工作的解决方案

检查端口地址有两个步骤

  • 在您的WCF项目中,右键单击您的服务文件(例如Service1.svc)->然后选择在浏览器中查看现在在您的浏览器中有类似的url,因此现在将端口地址记为61122

  • 右键单击您的wcf项目->然后选择属性->转到Web选项卡->现在在服务器部分->选择使用Visual Studio Development Server->选择特定端口,并给出我们先前从Service1.svc服务中找到的端口地址。即(61122)

  • 之前,我有一个不同的端口地址。在正确指定端口地址后(从第一步,EndpointAddress 61122获得),我的问题得到了解决


    我希望这可以解决您的问题。

    我也遇到了同样的问题。这是文件(例如Service1.svc)的Visual studio端口地址中的EndpointAddress的端口地址问题您的wcf项目的端口地址必须与您提供给EndpointAddress的端口地址相同。让我详细描述一下这个对我有效的解决方案

    检查端口地址有两个步骤

  • 在您的WCF项目中,右键单击您的服务文件(例如Service1.svc)->然后选择在浏览器中查看现在在您的浏览器中有类似的url,因此现在将端口地址记为61122

  • 右键单击您的wcf项目->然后选择属性->转到Web选项卡->现在在服务器部分->选择使用Visual Studio开发