在 http // 上没有侦听可以接受 WCF 中的消息的终结点

本文关键字:WCF 消息 结点 http | 更新日期: 2023-09-27 18:32:55

我正在尝试开发一个webservice。在我的应用程序中,我需要在没有任何引用的情况下连接到我的webservice,所以我使用以下代码:

static void Main(string[] args)
{            
    BasicHttpBinding binding = new BasicHttpBinding();
    EndpointAddress address = new EndpointAddress("http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc");
    ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, address);
    IService1 channel = factory.CreateChannel();
    Console.WriteLine(channel.GetCategoryName(1));
    Console.ReadLine();
}

但是在这一行中channel.GetCategoryName(1)我收到此错误:

There was no endpoint listening at http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

这是我service webconfig

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
注意:当我添加引用时,

它可以工作,当我不将其添加到引用时它不起作用。

错误堆栈跟踪:

System.ServiceModel.EndpointNotFoundException was unhandled by user code
  HResult=-2146233087
  Message=There was no endpoint listening at http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(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 WcfServiceLibrary1.IService1.GetCategoryName(Int32 productID)
       at WebApplication1.WebForm1.Page_Load(Object sender, EventArgs e) in c:'Users'ehsan'Documents'Visual Studio 2012'Projects'WcfService1'WebApplication1'WebForm1.aspx.cs:line 20
       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)
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The remote server returned an error: (404) Not Found.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       InnerException

在 http // 上没有侦听可以接受 WCF 中的消息的终结点

你说当你添加引用时它有效。所以我的猜测是您的端点有问题。

根据您的代码,您希望端点http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc。但您的实际端点是 http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc/confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc .

在您的 wsdl 中,它将 url 显示为

<wsdl:service name="Service1">
<wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1"> 
<soap:address location="http://confdemo.spadsystem.com
/WcfServiceLibrary1.Service1.svc/confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc"/>
</wsdl:port>
</wsdl:service>

我认为这部分你的 web.config 不正确。

<endpoint address="confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">