簡體   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