WCF 多重绑定 - 错误:没有端点监听

Posted

技术标签:

【中文标题】WCF 多重绑定 - 错误:没有端点监听【英文标题】:WCF multiple binding - Error: There was no endpoint listening 【发布时间】:2015-01-28 19:41:27 【问题描述】:

我正在尝试在 asp WCF 项目中设置服务器和客户端以使用各种 WCF 绑定模式。

在 Visual Studio 2012 中运行时出现此错误:

There was no endpoint listening at http://localhost:9000/BasicHttp that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.  
Unable to connect to the remote server

Server stack trace:     at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream() at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)    at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)    at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)    at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)    at WCF_ASMX.IWCF_Service.add(Int32[] list)    at WCF_ASMX._Default.NetPipeClient() in c:\xxx\Programming\c#\win_dotnet_sample_apps\WCF_ASMX_64bit_NamedPipe_F45\Default.aspx.cs:line 81    at WCF_ASMX._Default.Page_Load(Object sender, EventArgs e) in c:\xxx\Programming\c#\win_dotnet_sample_apps\WCF_ASMX_64bit_NamedPipe_F45\Default.aspx.cs:line 32    at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)    at System.Web.UI.Control.OnLoad(EventArgs e)    at System.Web.UI.Control.LoadRecursive()    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

这是我web.config的相关部分:

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttpBinding_behaviour">
                <enableWebScript />
            </behavior>
            <behavior name="basicHttpBinding_behaviour">
            </behavior>
            <behavior name="netNamedPipeBinding_behaviour">
                <enableWebScript />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service name="WCF_Sample.WCF_Service">
            <endpoint 
                address="http://localhost:9000/BasicHttp" 
                behaviorConfiguration="basicHttpBinding_behaviour"
                binding="basicHttpBinding" 
                contract="WCF_Sample.WCF_Service" />
            <endpoint 
                address="net.pipe://localhost/NetNamedPipe" 
                behaviorConfiguration="netNamedPipeBinding_behaviour"
                binding="netNamedPipeBinding" 
                contract="WCF_Sample.WCF_Service" />
        </service>
    </services>
</system.serviceModel>

这是我的服务:

[ServiceContract]
public interface IWCF_Service

    [OperationContract]
    int add(int[] list);


[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class WCF_Service : IWCF_Service

    [OperationContract]
    [WebInvoke(Method = "GET")]
    public int add(int[] list)
    
        int sum = 0;
        foreach(int val in list)
        
            sum += val;
        
        return sum;
    

这是我的客户:

private void Client()

    ChannelFactory<IWCF_Service> httpFactory =
          new ChannelFactory<IWCF_Service>(
            new BasicHttpBinding(),
            new EndpointAddress("http://localhost:9000/BasicHttp"));

    ChannelFactory<IWCF_Service> pipeFactory =
          new ChannelFactory<IWCF_Service>(
            new NetNamedPipeBinding(),
            new EndpointAddress("net.pipe://localhost/NetNamedPipe"));

    IWCF_Service httpProxy = httpFactory.CreateChannel();
    IWCF_Service pipeProxy = pipeFactory.CreateChannel();

    string str;
    str = "http: " + httpProxy.add(new int[]  1, 2 );
    Console.WriteLine(str);

    str = "pipe: " + pipeProxy.add(new int[]  1, 2 );
    Console.WriteLine(str);

有谁知道我做错了什么?


更新以下代码: 虽然我仍然遇到类似的错误:

The pipe endpoint 'net.pipe://wcf_sample/' could not be found on your local machine.
[PipeException: The pipe endpoint 'net.pipe://wcf_sample/' could not be found on your local machine. ]

[EndpointNotFoundException: There was no endpoint listening at net.pipe://wcf_sample/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.]

新的 web.config:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="netNamedPipeBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="netNamedPipeBehavior" name="WCF_Sample.WCF_Service">
        <endpoint address="" binding="netNamedPipeBinding" bindingConfiguration=""
          name="netNamedPipeEndPt" contract="WCF_Sample.IWCF_Service" />
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://WCF_Sample" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

新客户端代码:

private void Client()

    ChannelFactory<IWCF_Service> pipeFactory =
      new ChannelFactory<IWCF_Service>(
        new NetNamedPipeBinding(),
        new EndpointAddress(
          "net.pipe://WCF_Sample"));

    IWCF_Service pipeProxy = pipeFactory.CreateChannel();
    string str;
    str = "pipe: " + pipeProxy.add(new int[]  1, 2 );
    Console.WriteLine(str);

当我在浏览器中打开服务时,我得到这个错误:

The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute.  Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements.
[InvalidOperationException: The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute.  Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements.]
   System.ServiceModel.Description.ServiceReflector.GetInterfaces(Type service) +12922331
   System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) +248
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +146
   System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) +46
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +146
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +30
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +494
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1434
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +52
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +598

[ServiceActivationException: The service '/WCF_Service.svc' cannot be activated due to an exception during compilation.  The exception message is: The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute.  Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +489276
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +178
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +350382
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9691825

【问题讨论】:

您如何托管您的服务?你在使用 IIS 吗?还是自托管方式? 目前我在 Visual Studio 中同时托管服务器和客户端。服务器是我的 wcf 服务,客户端是我的 asp 应用程序;所以我想这就是你所说的自托管。 两个问题:(1)客户端和服务器是在同一台机器上吗?如果没有,那么命名管道将不起作用。 (2) 你能提供服务器堆栈跟踪吗? (如果 Q1 是肯定的)。 是的,是的。当我在浏览器中打开服务时,我注意到一个奇怪的错误。我认为这是关键......所以我要发布它。 我是否正确地说因为我在 WCF_Service 中使用了 [ServiceContract],所以我不应该指定接口 IWCF_Service,因为 wcf 已经为我创建了一个不可见的接口? 【参考方案1】:

问题在于您没有正确指定服务合同。

contract="WCF_Sample.WCF_Service" 更改为contract="WCF_Sample.IWCF_Service" /&gt;

每条评论更新:

拥有一个接口并实现它被认为是最佳实践。我将从服务库中删除属性并将它们放在服务合同中。这应该可以解决您的问题。

但是,如果您希望跳过该界面,则需要从您的项目中完全删除 IWCF_Service 并将您的 web.config 更改为:

<service behaviorConfiguration="netNamedPipeBehavior" name="WCF_Sample.WCF_Service">
  <endpoint address="" binding="netNamedPipeBinding" bindingConfiguration=""
          name="netNamedPipeEndPt" contract="WCF_Sample.WCF_Service" />

【讨论】:

谢谢。我还注意到...修复该问题后,我仍然收到该错误。我还尝试从头开始创建配置文件并更新上面的代码。

以上是关于WCF 多重绑定 - 错误:没有端点监听的主要内容,如果未能解决你的问题,请参考以下文章

net.pipe 没有端点监听

WCF 错误“找不到与具有绑定 NetTcpBinding 的端点的方案 net.tcp 匹配的基地址”

WCF 身份验证错误

WCF 多端点

WCF命名管道另一个端点错误但没有其他端点?

测试 WCF - 没有端点监听