简体   繁体   English

在 asp.net core 2.2 中找不到类型或命名空间名称“Worker”(您是否缺少 using 指令或程序集引用?)?

[英]The type or namespace name 'Worker' could not be found (are you missing a using directive or an assembly reference?) in asp.net core 2.2?

I have asp.net core 2.2 project in which i have created a service named Worker.cs which looks like this我有一个asp.net core 2.2项目,我在其中创建了一个名为Worker.cs的服务,它看起来像这样

 using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace sqs_processor { public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; private readonly IAmazonSQS _sqs; private readonly string _exampleQueueUrl = "https://sqs.ap-southeast-2.amazonaws.com/637294848563/example-queue"; private readonly string _processedMessageQueueUrl = "https://sqs.ap-southeast-2.amazonaws.com/637294848563/processed-messages"; public Worker(ILogger<Worker> logger, IAmazonSQS sqs) { _logger = logger; _sqs = sqs; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { var request = new ReceiveMessageRequest { QueueUrl = _exampleQueueUrl, MaxNumberOfMessages = 10, WaitTimeSeconds = 5 }; var result = await _sqs.ReceiveMessageAsync(request); if (result.Messages.Any()) { foreach (var message in result.Messages) { // Some Processing code would live here _logger.LogInformation("Processing Message: {message} | {time}", message.Body, DateTimeOffset.Now); var processedMessage = new ProcessedMessage(message.Body); var sendRequest = new SendMessageRequest(_processedMessageQueueUrl, JsonConvert.SerializeObject(processedMessage)); var sendResult = await _sqs.SendMessageAsync(sendRequest, stoppingToken); if (sendResult.HttpStatusCode == System.Net.HttpStatusCode.OK) { var deleteResult = await _sqs.DeleteMessageAsync(_exampleQueueUrl, message.ReceiptHandle); } } } } catch (Exception e) { _logger.LogError(e.InnerException.ToString()); } _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); } } } public class ProcessedMessage { public ProcessedMessage(string message, bool hasErrors = false) { TimeStamp = DateTime.UtcNow; Message = message; HasErrors = hasErrors; } public DateTime TimeStamp { get; set; } public string Message { get; set; } public bool HasErrors { get; set; } } }

Now when i am adding this service in my Startup.cs like this现在,当我像这样在Startup.cs添加此服务时

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddCors(c =>
        {
            c.AddPolicy("AllowOrigin", options =>
            {
                options.AllowAnyOrigin();
            });
        });


         // AWS Configuration
                var awsoptions = Configuration.GetAWSOptions();
                services.AddDefaultAWSOptions(awsoptions);
                services.AddAWSService<IAmazonSQS>();

                // Worker Service
                services.AddHostedService<Worker>();
    }

So on registering Worker.cs in my Startup.cs it throws an error like this因此,在我的Startup.cs注册Worker.cs ,它会引发这样的错误

Startup.cs(46,47): error CS0246: The type or namespace name 'Worker' could not be found (are you missing a using directive or an assembly reference?) [D:\OfficeProjects\beelinksanalytics\analytics.csproj]

So what i am doing wrong in this?那么我做错了什么? I just simple want to add that Worker.cs in my Startup.cs .我只是想在我的Startup.cs添加Worker.cs But curreny it is throwing an error.但是curry它抛出了一个错误。

You need to add the namespace of your Worker class on the usings of your startup class.您需要在启动类的使用中添加 Worker 类的命名空间。

in your case it should be like this:在你的情况下,它应该是这样的:

using sqs_processor;

When you get this kind of error, you can make visual studio resolve it put your cursor on the class name and pressing Ctrl + .当您遇到此类错误时,您可以让 Visual Studio 解决它,将光标放在类名上并按Ctrl + then Visual Studio will give you some options to solve it.然后 Visual Studio 会给你一些解决它的选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 2.0找不到类型或名称空间名称“ dll名称”(您是否缺少using指令或程序集引用?) - the type or namespace name 'dll Name' could not be found (are you missing a using directive or an assembly reference?) asp.net 2.0 C#asp.net-找不到类型或名称空间名称“ Helper”(您是否缺少using指令或程序集引用?) - C# asp.net - The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称(缺少使用指令或程序集引用吗?) - Type or namespace name could not be found (missing using directive or assembly reference?) 找不到类型或名称空间名称“ My_Interface_Name”(您是否缺少using指令或程序集引用?) - The type or namespace name 'My_Interface_Name' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ FacebookSessionClient”(您是否缺少using指令或程序集引用?) - The type or namespace name 'FacebookSessionClient' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或命名空间名称&#39;SignInManager&#39;(您是否缺少using指令或程序集引用?) - The type or namespace name 'SignInManager' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ PrintDocument”(是否缺少using指令或程序集引用?) - The type or namespace name 'PrintDocument' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或命名空间名称“OleDbConnection”(您是否缺少 using 指令或程序集引用?) - The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?) 为什么会出现这个错误? “找不到类型或名称空间名称&#39;c&#39;(您是否缺少using指令或程序集引用?)” - Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM