繁体   English   中英

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

[英]WCF multiple binding - Error: There was no endpoint listening

我正在尝试在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

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

contract="WCF_Sample.WCF_Service"更改为contract="WCF_Sample.IWCF_Service" />

每条评论更新:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM