我制作了一个顶级服务,目的是向 API 发出请求。当我在控制台中运行它时,它工作正常。我安装它也没有任何问题。但是当我尝试运行它时,我立即得到:
"error 1053 the service did not respond to the start or control request in a timely fashion"
我从 this 制作了不同的“复制和粘贴”基本服务 教程,它开始没有问题。这是我的一些代码:
public class ApiService
{
public ApiService()
{
_timer = new System.Timers.Timer(10 * 1000) { AutoReset = true };
_timer.Elapsed += TimerElapsed;
}
private async void TimerElapsed(object sender, ElapsedEventArgs e)
{
await MakeRequest("projectid", "subscriptionId", true);
}
public async Task<int> MakeRequest(string projectId, string subscriptionId, bool acknowledge)
{
SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName);
int messageCount = 0;
Task startTask = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) =>
{
string text = System.Text.Encoding.UTF8.GetString(message.Data.ToArray());
Interlocked.Increment(ref messageCount);
return Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
});
await Task.Delay(5000);
await subscriber.StopAsync(CancellationToken.None);
await startTask;
numOfMessages = messageCount;
return messageCount;
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
}