小编str*_*ood的帖子

使用 HttpClient 调用 WCF 服务

我必须调用 WCF 服务。WCF 服务已开启,我可以编辑其配置。

我想创建一个调用服务的客户端。我无法将服务引用添加到我的客户端,因此我尝试使用 HttpClient 调用它。

客户端代码:

    using (var client = new HttpClient())
    {
        //soapString is my input class serialized
        var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
        using (var postResponse = client.PostAsync("http://localhost:52937/Attempts.svc/", content).Result)
        {
            string postResult = postResponse.Content.ReadAsStringAsync().Result;

        }
    }
Run Code Online (Sandbox Code Playgroud)

服务器端代码:

    [ServiceContract]
    public interface IAttempts
    {
        [OperationContract]
        void ReceiveAttemptResult(ReceiveAttemptResultInput result);
    }

    public class Attempts : IAttempts
    {
        string _backendUrl;

        public void ReceiveAttemptResult(ReceiveAttemptResultInput result)
        {
           //...
        }
    }
Run Code Online (Sandbox Code Playgroud)

最后是 web.config 服务器端:

    <system.serviceModel>
      <services>
        <service name="it.MC.Listeners.Attempts">
          <endpoint address="" contract="it.MC.Listeners.IAttempts" binding="basicHttpBinding"/>
          <endpoint …
Run Code Online (Sandbox Code Playgroud)

c# wcf httpclient

6
推荐指数
1
解决办法
8680
查看次数

标签 统计

c# ×1

httpclient ×1

wcf ×1