简体   繁体   中英

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

 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

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(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 . But curreny it is throwing an error.

You need to add the namespace of your Worker class on the usings of your startup class.

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 + . then Visual Studio will give you some options to solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM