这是我在C#中的第一个WCF ReST服务。
计算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);
}
}
网状物服务时配置
<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>
网状物客户端配置
<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.-
内部异常:
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中工作,但不在客户端应用程序中。