这是我在C#中的第一个WCF ReST服务。
Calc.svc.cs
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
$("#google-review").click(function() {
window.gapi.load('surveyoptin', function () {
window.gapi.surveyoptin.render({
"merchant_id": number,
"order_id": order_id,
"email": customer_email,
"delivery_country": delivery_country,
"estimated_delivery_date": estimated_delivery_date,
"opt_in_style": "BOTTOM_TRAY"
});
});
});
</script>
ICalc.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;
}
}
}
服务中的Web.Config
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>
在客户端.cs文件中
<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>
错误讯息:
WcfService1.CalcClient cal = new CalcClient();
Label.Text = cal.XMLData("xmldata123");
的InnerException:
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)
http://localhost:7510/Calc.svc?wsdl
Page URL: http://localhost:7510/Calc.svc
svcutil.exe http://localhost:7510/Calc.svc?wsdl
http://localhost:7510/Calc.svc?singleWsdl
有人可以解释一下我哪个部分给了我错误。真的我不知道错误部分。它在URL中工作,但不在客户端应用程序中。
答案 0 :(得分:0)
我找到了解决方案。我在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;