【问题标题】:consuming a WCF service using simple HttpClient class使用简单的 HttpClient 类使用 WCF 服务
【发布时间】:2018-12-31 16:56:11
【问题描述】:

考虑一下微软的例子:https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-create-a-wcf-client

如何使用简单的 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;
        }
    }
}

【问题讨论】:

  • “使用简单的 HttpClient 对象来消费服务器”但是为什么呢?它破坏了 WCF 的全部目的! WCF 是关于创建代理类和执行远程功能,就好像它们是本地的一样。它非常简单和有用。现在你只想忽略所有这些并使用一些疯狂的东西......为什么?如果您想使用 HttpClient 使用它,只需创建一个 API 项目。

标签: wcf httpclient


【解决方案1】:

您可以使用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>

基于 Web 服务规范的 WCF 服务。
https://en.wikipedia.org/wiki/Web_service
他们使用soap消息通过http/https协议相互通信。 在这种情况下,我们可以使用 httpclient 来模拟请求。
另外wcf也可以通过http模式发布服务,就像Asp.net web api一样,restful风格的结构。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/wcf-and-aspnet-web-api
我做了一个demo,希望对你有用。
IService1

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet(ResponseFormat =WebMessageFormat.Json,RequestFormat =WebMessageFormat.Json)]
        Double Add(double n1, double n2);

}

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);

        }

结果。

如果有什么可以帮助的,请随时告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多