在http//上没有端点可以接受WCF中的消息

问题描述:

我正在尝试开发 webservice .在我的应用程序中,我需要无需任何引用即可连接到 webservice ,因此我使用以下代码:

I am trying to develop a webservice. In my application I need to connect to my webservice without any referencing, so I use this code:

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)我收到此错误:

But in this line channel.GetCategoryName(1) I get this error :

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.

这是我的服务网络配置:

<?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>

注意:当我添加引用时,它会起作用,但是当我不添加引用时,它就不会起作用.

note:when I add the reference it works, it doesn't work when I don't add that to reference .

错误堆栈跟踪:

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

您说在添加引用时它可以工作.因此,我的猜测是您的端点存在问题.

You said it worked when you added references. So my guess is that you have an issue in your endpoint.

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

According to your code you are expecting the endpoint to be http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc. But your actual endpoint is http://confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc/confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc.

在您的wsdl中,该网址显示为

In your wsdl it shows the url as

<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的这一部分不正确.

I think this part this your web.config is not correct.

<端点地址="confdemo.spadsystem.com/WcfServiceLibrary1.Service1.svc" binding ="basicHttpBinding" contract ="WcfServiceLibrary1.IService1">