Asp .net Core使用服务实例在Startup.cs中运行的另一个方法中调用方法 [英] Asp .net Core using service instance to call method in another method which runs in Startup.cs

查看:376
本文介绍了Asp .net Core使用服务实例在Startup.cs中运行的另一个方法中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带事件方法的类,它获取需要存储到数据库的数据.

I have class with method which is event and its gets data which i need to store to database.

public class DataParser
{
SmsService smsService = new SmsService(..require context...);

 public void ReceiveSms()
 {
     //ParserLogic
     smsService.SaveMessage(...Values...);
 }
}

由于服务借助上下文保存数据,因此我需要传递数据并在构造函数中进行初始化.在我这样做之后,当我创建要在Startup ir上运行的解析器对象时,需要在该处传递上下文.

As service saves data with help of context I need to pass it and initialize in constructor. After i do that when I'm creating my parser object to run on Startup ir requires to pass context there.

public class Startup
{
DataParser data = new DataParser(...requires db context...)
   public void ConfigureServices(IServiceCollection services)
    {
        //Opens port for runtime
        InnerComPortSettings.OpenPort();
        //Runtime sms receiver
        data.ReceiveSms();
    }
}

那我怎样才能正确地将数据保存到数据库?

So how can I properly save data to db?

推荐答案

您需要重构代码.

1)您不必在解析器内部创建服务.将其作为依赖项传递

1) You dont have to create service inside parser. Pass it as dependency

public class DataParser
{   
    public DataParser(SmsService smsService)
    {
        SmsService _smsService = smsService;
    }

    public void ReceiveSms( )
    {
        //ParserLogic
        smsService.SaveMessage(...Values...);
    }
}

2)现在您需要注册上下文以及解析器和服务

2) Now you need to register your context and parser and service

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options =>... your options here); // register your context
    services.AddSingleton<SmsService, SmsService>(); // register your sms servcice which is required data context
    services.AddSingleton<DataParser, DataParser>(); // register your parser
}

5)现在是时候重构您的短信服务了

5) now its time to refactor your sms service

public class SmsService
{
    private readonly IServiceScopeFactory _scopeFactory;

    public SmsService(IServiceScopeFactory scopeFactory)
    {
        _scopeFactory = scopeFactory;
    }

    public async Task SaveMessage(....)
    {
        using (var scope = _scopeFactory.CreateScope())
        {
            using (var ctx = scope.ServiceProvider.GetService<MyDbContext>())
            {
                ... make changes
                await ctx.SaveChangesAsync();
            }
        }
    }
}

4)一切都注册完毕后,您可以在 Startup 类的configure方法中解决所需的内容

4) When everything registered, you can resolve what you need in configure method of Startup class

public void Configure(IApplicationBuilder app, DataParser data) // resolving your data perser and using it
{
    //Opens port for runtime
    InnerComPortSettings.OpenPort();
    //Runtime sms receiver
    data.ReceiveSms();
}

或者您可以在所需的控制器,服务中解析解析器.

Or you can resolve your parser in controllers, services, everuwhere you want.

这篇关于Asp .net Core使用服务实例在Startup.cs中运行的另一个方法中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆