使用简单的HttpClient类使用WCF服务

时间:2018-12-28 16:12:44

标签: wcf httpclient

考虑Microsoft提供的示例:https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-create-a-wcf-client

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

在某些情况下,无法向您的项目添加Web引用。然后,使用HttpClient对象可能是唯一的方法……或者将Server 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用http客户端发送参考Fiddle捕获的请求内容的http请求。

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

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

        }

结果。
enter image description here
随时让我知道是否有什么可以帮助您的。