简体   繁体   中英

WCF No endpoint listening at 'localhost', incorrect address, error (404) not found

This is my first WCF ReST Service in C#.

Calc.svc.cs

namespace WcfService1
{
    public class Calc : ICalc
    {
        public string XMLData(string id)
        {
            return "XML product " + id;
        }

        public string JSONData(string id)
        {
            return "JSON product " + id;
        }
    }
}

ICalc.cs

namespace WcfService1
{
    [ServiceContract]
    public interface ICalc
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONData(string id);
    }
}

Web.Config at Service

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding_IService1" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="WcfService1.Calc" behaviorConfiguration="ServiceBehaviour" >
        <endpoint address="" contract="WcfService1.ICalc" 
                  binding="webHttpBinding" behaviorConfiguration="webHttp" bindingConfiguration="webHttpBinding_IService1" />
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" />
      </service>
    </services>    

  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Web.Config at Client

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding_IService1" />
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webhttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost:7510/Calc.svc" contract="WcfService1.ICalc" behaviorConfiguration="webhttp" 
                binding="webHttpBinding" bindingConfiguration="webHttpBinding_IService1" />
    </client>
  </system.serviceModel>

In Client .cs file

WcfService1.CalcClient cal = new CalcClient();
Label.Text = cal.XMLData("xmldata123");

Error Message:

There was no endpoint listening at http://localhost:7510/Calc.svc/XMLData that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.-

InnerException:

System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

While debugging I got this message:

Page URL: http://localhost:7510/Calc.svc
svcutil.exe http://localhost:7510/Calc.svc?wsdl
http://localhost:7510/Calc.svc?singleWsdl

http://localhost:7510/Calc.svc?wsdl

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="Calc" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://localhost:7510/Calc.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://localhost:7510/Calc.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ICalc_XMLData_InputMessage">
<wsdl:part name="parameters" element="tns:XMLData"/>
</wsdl:message>
<wsdl:message name="ICalc_XMLData_OutputMessage">
<wsdl:part name="parameters" element="tns:XMLDataResponse"/>
</wsdl:message>
<wsdl:message name="ICalc_JSONData_InputMessage">
<wsdl:part name="parameters" element="tns:JSONData"/>
</wsdl:message>
<wsdl:message name="ICalc_JSONData_OutputMessage">
<wsdl:part name="parameters" element="tns:JSONDataResponse"/>
</wsdl:message>
<wsdl:portType name="ICalc">
<wsdl:operation name="XMLData">
<wsdl:input wsaw:Action="http://tempuri.org/ICalc/XMLData" message="tns:ICalc_XMLData_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/ICalc/XMLDataResponse" message="tns:ICalc_XMLData_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="JSONData">
<wsdl:input wsaw:Action="http://tempuri.org/ICalc/JSONData" message="tns:ICalc_JSONData_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/ICalc/JSONDataResponse" message="tns:ICalc_JSONData_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:service name="Calc"/>
</wsdl:definitions>

Could anyone please explain me which part gives me error. Really I am not aware of the error part. Its working in the URL but not inside the client application.

I found out the solution. I used webHttpBinding in Service but called over SOAP in client. Thats the issue. Now I m calling the client using ReST and solved it.

Original Client code (SOAP model)

WcfService1.CalcClient cal = new CalcClient();
Label.Text = cal.XMLData("xmldata123");

Updated Client code using ReST model

        HttpClient client = new HttpClient();
        HttpResponseMessage wcfResponse = client.GetAsync(string.Format("http://localhost:7510/Calc.svc/xmlData/{0}", "hello")).Result;
        HttpContent stream = wcfResponse.Content;
        var data = stream.ReadAsStringAsync();
        Label.Text = data.Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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