使用简单的HttpClient类使用WCF服务
考虑一下Microsoft在以下方面的示例:使用简单的HttpClient类使用WCF服务,wcf,httpclient,Wcf,Httpclient,考虑一下Microsoft在以下方面的示例: 如何使用简单的HttpClient对象使用服务器 在某些情况下,无法向项目添加Web引用。然后,使用HttpClient对象可能是唯一的方法。。。或者也可以选择向客户端添加服务器DLL引用 这应该是如何调用下面描述的简单double=Add(double,double)函数的简单示例: namespace GettingStartedLib { // NOTE: You can use the "Rename" command on the
如何使用简单的HttpClient对象使用服务器 在某些情况下,无法向项目添加Web引用。然后,使用HttpClient对象可能是唯一的方法。。。或者也可以选择向客户端添加服务器DLL引用 这应该是如何调用下面描述的简单double=Add(double,double)函数的简单示例:
namespace GettingStartedLib
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
}
}
服务器端如下所示:
namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}
}
}
您可以使用http客户端发送http请求,该请求引用Fiddle捕获的请求内容
POST http://10.157.18.36:12000/Service1.svc HTTP/1.1 Content-Type: text/xml; charset=utf-8 SOAPAction: "http://tempuri.org/IService1/Add" Host: 10.157.18.36:12000 Content-Length: 161 Expect: 100-continue Accept-Encoding: gzip, deflate Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Add xmlns="http://tempuri.org/"><n1>34.23</n1><n2>80.54</n2></Add></s:Body></s:Envelope>
Service1.svc.cs
public class Service1 : IService1
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
}
配置。
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="rest"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpGetUrl="mex"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
static void Main(string[] args)
{
HttpClient client = new HttpClient();
var result=client.GetStringAsync("http://localhost:9001/Service1.svc/add?n1=1.2&n2=3.2");
Console.WriteLine(result.Result);
}
结果。如果有什么我可以帮忙的,请随时告诉我。“使用简单的HttpClient对象使用服务器”,但为什么?它破坏了WCF的全部目的!WCF是关于创建代理类和执行远程函数的,就像它们是本地函数一样。这很简单,也很有用。现在你只想忽略所有这些,用一些疯狂的东西。。。为什么?如果您想使用HttpClient,只需创建一个API项目。
static void Main(string[] args)
{
HttpClient client = new HttpClient();
var result=client.GetStringAsync("http://localhost:9001/Service1.svc/add?n1=1.2&n2=3.2");
Console.WriteLine(result.Result);
}