C# 调用服务失败。可能原因:调用时服务离线或无法访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18610041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to invoke the service. Possible causes: The service is offline or inaccessible while Invoking
提问by Simsons
I have my service configured as below:
我的服务配置如下:
<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>
and the client as:
和客户为:
<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>
When using WCFTestClient or SVCutil, I am able to discover and access the servie and generate proxy or stubs.
使用 WCFTestClient 或 SVCutil 时,我能够发现和访问服务并生成代理或存根。
But when I want to invoke any of the methods getting following error:
但是当我想调用任何得到以下错误的方法时:
Failed to invoke the service. Possible causes: The service is offlineor inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.
The socket connection was aborted.This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:04:59.9980468'.
调用服务失败。可能原因:服务离线或无法访问;所述客户端配置不匹配的代理; 现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复到默认配置或刷新服务来恢复。
套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超过接收超时,或底层网络资源问题引起的。本地套接字超时为“00:04:59.9980468”。
回答by PatFromCanada
Maybe not the answer you are looking for, but for development on the local machine I always use an HTTP endpoint. I find Net.TCP will only play nice when it is hosted on a server in IIS.
也许不是您正在寻找的答案,但对于本地机器上的开发,我总是使用 HTTP 端点。我发现 Net.TCP 只有在 IIS 中的服务器上托管时才能很好地发挥作用。
It is easy enough to add the NET.TCP endpoints once you deploy to an IIS server. Note: that the base address no longer has any affect as that is set in IIS. To test the new endpoints it is best to start by opening the service in a browser while remoted into the server, that way you get the best error messages.
部署到 IIS 服务器后,添加 NET.TCP 端点就很容易了。注意:基地址不再有任何影响,因为它在 IIS 中设置。要测试新端点,最好先在浏览器中打开服务,同时远程访问服务器,这样您就可以获得最好的错误消息。
If i understand correctly you are trying to select the endpoint when creating the service reference? Thats now how it works.
If you create your service reference using 127.0.0.1/service/Service1.svc
如果我理解正确,您是在创建服务引用时尝试选择端点吗?这就是它现在的工作方式。如果您使用创建服务引用 127.0.0.1/service/Service1.svc
You should see in your config file something like the following. (When I create my service endpoints I always name them with the protocol as a suffix.
您应该在配置文件中看到如下内容。(当我创建我的服务端点时,我总是用协议作为后缀来命名它们。
<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" />
then in your code you can choose which endpoint to use.
然后在您的代码中您可以选择要使用的端点。
Dim svc as New SeviceReferenceName.BusinessClient("BusinessTCP")"
回答by Gehan Fernando
Set the following value inside the endpoint block
在端点块内设置以下值
<identity>
<dns value ="localhost"/>
</identity>
回答by Nitin
NetTcp binding is secured by default.In the security authentication process between client and service,WCF ensures that the identity of service matches the values of this element. Therefore service need to be configured with :
NetTcp 绑定默认是安全的。在客户端和服务之间的安全认证过程中,WCF 确保服务的身份与该元素的值匹配。因此服务需要配置:
<identity>
<dns value ="localhost"/>
</identity>
回答by Dilip0165
I have faced the same issue. It was the issue of port address of EndpointAddress. In Visual studio port address of your file (e.g. Service1.svc) and port address of your wcf project must be the same which you gives into EndpointAddress. Let me describe you this solution in detail which worked for me.
我遇到了同样的问题。这是EndpointAddress的端口地址问题。在 Visual Studio 中,您的文件(例如 Service1.svc)的端口地址和 wcf 项目的端口地址必须与您提供给EndpointAddress的相同。让我详细描述一下这个对我有用的解决方案。
There are two steps to check the port addresses.
检查端口地址有两个步骤。
In your WCF Project right click to your Service file (e.g. Service1.svc) -> than select View in browsernow in your browser you have url like http://localhost:61122/Service1.svcso now note down your port address as a 61122
Right click your wcf project -> than select Properties-> go to the Web Tab-> Now in Servers section-> select Use Visual Studio Development Server-> select Specific Portand give the port address which we have earlier find from our Service1.svc service. That is (61122).
在您的WCF项目右键点击你的服务文件(如Service1.svc) - >选择比在浏览器中查看,现在在你的浏览器,你有喜欢的网址的http://本地主机:61122 / Service1.svc所以现在记下你的端口地址为一个 61122
右键单击您的 wcf 项目 -> 然后选择属性-> 转到Web 选项卡-> 现在在服务器部分-> 选择使用 Visual Studio 开发服务器-> 选择特定端口并提供我们之前从 Service1 中找到的端口地址。 svc 服务。即(61122)。
Earlier, I had a different port address. After specifying the port address properly, which I got from the first step, EndpointAddress 61122, my problem was solved.
早些时候,我有一个不同的端口地址。正确指定端口地址后,我从第一步得到的EndpointAddress 61122,我的问题就解决了。
I hope this might be solved your issue.
我希望这可以解决您的问题。