已解决-C#服务HttpClient响应为空

问题描述 投票:0回答:1

我已经阅读了所有可能的文章,但没有找到适合我的解决方案(没有更多的东西可以看到导入过程中的混乱情况,请提供帮助。 :(

我正在用C#构建Windows服务,我需要每X次调用一次Web服务,并检查它是否必须执行某些操作。 (收集说明)

我附上完整的代码:

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Reflection;
using System.ServiceProcess;
using System.Timers;
using System.Text;

namespace TestServiceCreateFile
{

public partial class Service1 : ServiceBase
{
    /* Config values */
    int CheckRobotStatusTimerMills = 10000;
    static string MachineKey = "";

    private static readonly HttpClient client = new HttpClient();
    EventLog EventLog = new System.Diagnostics.EventLog();

    public Service1()
    {
        this.ServiceName = "BotBrainExecutor";
        this.EventLog.Source = this.ServiceName;
        this.EventLog.Log = "Application";

        ((ISupportInitialize)(this.EventLog)).BeginInit();
        if (!EventLog.SourceExists(this.EventLog.Source))
        {
            EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);
        }
        ((ISupportInitialize)(this.EventLog)).EndInit();

    }

    protected override void OnStart(string[] args)
    {
        LoadConfig();

        Timer timer = new Timer();
        timer.Interval = CheckRobotStatusTimerMills;
        timer.Elapsed += (s, e) => MainFunctionAsync(s, e);
        timer.Start();


        base.OnStart(args);
    }

    private string URLGetOrdersOrch = "http://localhost:8080/testing?machinekey=" + MachineKey;
    public async Task MainFunctionAsync(object sender, ElapsedEventArgs args)
    {
        LogLine("Start" + " " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss"));

        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.GetAsync(URLGetOrdersOrch);

        if (response.IsSuccessStatusCode)
        {
            LogLine("Response OK");

            LogLine(await response.Content.ReadAsStringAsync());
        }
        else
        {
            LogLine((int)response.StatusCode + " " + response.ReasonPhrase);
        }

    }

问题是Http的结果正确,并且它在日志'Response OK'中注册,因此它正在输入'if',但它永远无法收集结果:

log image

谢谢!

c# windows rest service ws
1个回答
0
投票

这完全是我的错。代码很好,但是它有一种方法可以注册具有字符串格式的日志。因此,当我收到“ {”时,该方法将其解释为传递给它的参数。我将保留该帖子,以供其他人查看。

© www.soinside.com 2019 - 2024. All rights reserved.