使用简单的HttpClient类使用WCF服务 [英] consuming a WCF service using simple HttpClient class

查看:141
本文介绍了使用简单的HttpClient类使用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑Microsoft提供的示例:

Consider the example Microsoft has on: https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-create-a-wcf-client

如何使用一个简单的HttpClient对象使用服务器?

How can one consume the server using a simple HttpClient object?

在某些情况下,无法向您的项目添加Web引用.然后,使用HttpClient对象可能是唯一的方法……或者将Server DLL引用添加到客户端也是一种选择.

There are instances where adding a Web Reference to your project is not possible. Then, using an HttpClient object might be the only way ... or adding a Server DLL reference to the Client side could also be an option.

这应该是一个简单的示例,说明如何调用以下所述的简单double = Add(double,double)函数:

This should be a simple example of how to call the simple double = Add(double, double) function described under:

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客户端发送一个引用Fiddle捕获的请求内容的http请求.

You could use http client to send a http request refer to the request content captured by 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
他们使用肥皂消息通过http/https协议相互通信. 在这种情况下,我们可以使用httpclient模仿请求.
除此之外,wcf还可以通过http-mode发布服务,就像Asp.net Web API一样,风格样式也很宁静.
https://docs.microsoft. com/en-us/dotnet/framework/wcf/wcf-and-aspnet-web-api
我做了一个演示,希望它对您有用.
IService1

WCF Service based on the web service specification.
https://en.wikipedia.org/wiki/Web_service
They use soap message to communicate with each other via http/https protocol. In that cases, we could use the httpclient to imitate the request.
Beside, wcf also could publish the service via http-mode, just like Asp.net web api, restful style structure.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/wcf-and-aspnet-web-api
I have made a demo, wish it is useful to you.
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);

        }

结果.

随时让我知道是否有什么可以帮助您的.

Result.

Feel free to let me know if there is anything I can help with.

这篇关于使用简单的HttpClient类使用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆