[英]WCF No endpoint listening at 'localhost', incorrect address, error (404) not found
这是我在C#中的第一个WCF ReST服务。
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服务
<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
<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>
在客户端.cs文件中
WcfService1.CalcClient cal = new CalcClient();
Label.Text = cal.XMLData("xmldata123");
错误信息:
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)
在调试时,我收到以下消息:
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>
谁能解释一下哪一部分给我错误。 真的我不知道错误部分。 它可以在URL中工作,但不能在客户端应用程序中工作。
我找到了解决方案。 我在Service中使用了webHttpBinding,但在客户端中通过SOAP进行了调用。 那就是问题。 现在,我使用ReST致电客户端并解决了该问题。
原始客户端代码(SOAP模型)
WcfService1.CalcClient cal = new CalcClient();
Label.Text = cal.XMLData("xmldata123");
使用ReST模型更新了客户端代码
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.