![](/img/trans.png)
[英]Is it possible to use the .Net Framework 4.5 Dll reference in .Net Core Console App that will run in Linux
[英]How reference .net framework data layer to .net core console app
我是.NET Core的新手。 我已经在.NET Framework中拥有一个Web API应用程序。 这包括使用Entity Framework为我的存储库提供的类库项目。
现在,我想创建一个类似于服务的调度程序。 我计划在.NET Core中进行此操作。 为了进行测试,我创建了一个示例.NET Core控制台应用程序,并添加了包含数据层代码的旧dll。
但是,当我运行控制台应用程序时,它显示错误消息“ MyCon”。 请帮我解决一下这个。
这是我的控制台应用程序代码:
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
Console.WriteLine("Hello World!...Scheduler starting....");
Scheduler scheduler = new Scheduler();
}
调度程序代码如下所示
public class Scheduler
{
private readonly Timer _tmr = null;
private readonly IUnitOfWork _unitOfWork = null;
private static readonly HttpClient _httpClient = new HttpClient() { MaxResponseContentBufferSize = 1000000, Timeout = System.TimeSpan.FromSeconds(60) };
public Scheduler()
{
try
{
_tmr = new Timer();
_unitOfWork = new UnitOfWork();
_tmr.Interval = 10000;
_tmr.Elapsed += _tmr_Elapsed;
_tmr.Start();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
_tmr = null;
}
}
private void _tmr_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
_tmr.Stop();
/* My database interaction code here */
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
_tmr.Start();
}
}
}
以下是我的配置代码(appsettings 。:
{
"ConnectionStrings": {
"MyCon": "Data Source=myconnectionstring;"
}
}
我不知道这足以使EF正常工作。
我收到以下错误;
System.InvalidOperationException:在应用程序配置文件中找不到名为“ MyCon”的连接字符串。
在System.Data.Entity.Internal.LazyInternalConnection.Initialize()
在System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
在Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
1..ctor(字符串名称OrConnectionString,布尔型throwIfV1Schema)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
在MyProj.Data.Context.MyContextContext..ctor()
在MyProj.Repositories.UnitOfWork..ctor()
在C:\\ Users \\ akhilraj.rajendran \\ Documents \\ Visual Studio 2017 \\ Projects \\ Sample \\ Sample \\ Scheduler.cs:line 29中的Engager.Scheduler..ctor()
由于我没有足够的声誉来添加评论,因此我正在写一个答案。 根据您提供的信息,您不清楚如何读取连接字符串。 尝试在您的Main函数中添加以下代码,并检查是否从appsetting.json文件中读取了连接字符串。
IConfigurationRoot configuration = builder.Build();
var connstring = configuration.GetConnectionString("MyCon");
这是一个很棒的博客,它将帮助您阅读.Net Core控制台应用程序中的appsettings.json。
https://blog.bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/
现在,这已成为话题,但是,如果要在.Net Core中创建调度程序,则一定要签出HangFire 。
Hangfire是在.NET和.NET Core应用程序中执行后台处理的简便方法。 无需Windows服务或单独的过程。 由持久性存储支持。 开放供商业使用。 Hangfire处理所有计划部分,因此您可以专注于编写作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.