[英]How reference .net framework data layer to .net core console app
I am new to .NET Core. 我是.NET Core的新手。 I already have a Web API application in .NET framework.
我已经在.NET Framework中拥有一个Web API应用程序。 This includes class library projects for my repository using Entity Framework.
这包括使用Entity Framework为我的存储库提供的类库项目。
Now I want to create a scheduler like service. 现在,我想创建一个类似于服务的调度程序。 I am planning to do it in .NET Core.
我计划在.NET Core中进行此操作。 For testing I created a sample .NET Core console application and added my old dll which contains my data layer code.
为了进行测试,我创建了一个示例.NET Core控制台应用程序,并添加了包含数据层代码的旧dll。
But when I run the console app, it shows an error as 'MyCon' not found. 但是,当我运行控制台应用程序时,它显示错误消息“ MyCon”。 Please help me with this.
请帮我解决一下这个。
This is my console app code: 这是我的控制台应用程序代码:
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();
}
The scheduler code looks like the following 调度程序代码如下所示
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();
}
}
}
The following is my configuration code(appsettings.: 以下是我的配置代码(appsettings 。:
{
"ConnectionStrings": {
"MyCon": "Data Source=myconnectionstring;"
}
}
I don't know is this enough for EF to work. 我不知道这足以使EF正常工作。
I got the following error; 我收到以下错误;
System.InvalidOperationException: No connection string named 'MyCon' could be found in the application config file.
System.InvalidOperationException:在应用程序配置文件中找不到名为“ MyCon”的连接字符串。
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
在System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.get_Connection()在System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext在Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
1..ctor(String nameOrConnectionString, Boolean throwIfV1Schema)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext1.IsIdentityV1Schema(DbContext db)
1..ctor(字符串名称OrConnectionString,布尔型throwIfV1Schema)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
at MyProj.Data.Context.MyContextContext..ctor()在MyProj.Data.Context.MyContextContext..ctor()
at MyProj.Repositories.UnitOfWork..ctor()在MyProj.Repositories.UnitOfWork..ctor()
at Engager.Scheduler..ctor() in C:\\Users\\akhilraj.rajendran\\Documents\\Visual Studio 2017\\Projects\\Sample\\Sample\\Scheduler.cs:line 29}在C:\\ Users \\ akhilraj.rajendran \\ Documents \\ Visual Studio 2017 \\ Projects \\ Sample \\ Sample \\ Scheduler.cs:line 29中的Engager.Scheduler..ctor()
I'm writing an answer since I do not have enough reputation to add a comment. 由于我没有足够的声誉来添加评论,因此我正在写一个答案。 From the information you provided it is not clear how you are reading the connection string.
根据您提供的信息,您不清楚如何读取连接字符串。 try adding below code in your Main function and check whether connection string is read or not from appsetting.json file.
尝试在您的Main函数中添加以下代码,并检查是否从appsetting.json文件中读取了连接字符串。
IConfigurationRoot configuration = builder.Build();
var connstring = configuration.GetConnectionString("MyCon");
Here is a great blog that will help you with reading appsettings.json in a .Net Core console application. 这是一个很棒的博客,它将帮助您阅读.Net Core控制台应用程序中的appsettings.json。
https://blog.bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/ https://blog.bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/
Now this is off topic, but, If you want to create a schedulers in .Net Core, you should definitely check out HangFire . 现在,这已成为话题,但是,如果要在.Net Core中创建调度程序,则一定要签出HangFire 。
Hangfire is an easy way to perform background processing in .NET and .NET Core applications. Hangfire是在.NET和.NET Core应用程序中执行后台处理的简便方法。 No Windows Service or separate process required.
无需Windows服务或单独的过程。 Backed by persistent storage.
由持久性存储支持。 Open and free for commercial use.
开放供商业使用。 Hangfire handles all the scheduling part so you can focus on writing jobs.
Hangfire处理所有计划部分,因此您可以专注于编写作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.