“ ServiceCollection”不包含带有0个参数的构造函数

时间:2020-04-26 08:04:05

标签: c# dependency-injection

我正在尝试使用C#创建Discord机器人,并且我在此处的代码会产生异常,并显示以下消息:'ServiceCollection'不包含带有0个参数的构造函数

_services = new ServiceCollection()
            .AddSingleton(_client)
            .AddSingleton(_commands)
            .BuildServiceProvider();

如果有帮助,请参考

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;
using Discord;
using Discord.Commands;
using Discord.WebSocket;

我是编程方面的初学者,因此对您的任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

您应将[{'metadata': {'gross': 26, 'payPeriod': 'Weekly', 'startDate': '2020-01-03', 'totalCost': 15}, 'name': 'Period 40'}, {'metadata': {'gross': 113, 'payPeriod': 'Weekly', 'startDate': '2020-01-03', 'totalCost': 101.5}, 'name': 'Period 41'}] 命名空间替换为System.Web.Services.Description

首先,您需要安装Microsoft.Extensions.DependencyInjection nuget软件包,然后将其包含在名称空间引用中

Microsoft.Extensions.DependencyInjection

它提供了另一个using Microsoft.Extensions.DependencyInjection; 类,因此您的代码不再抛出编译错误

ServiceCollection