Azure数据库可在localhost上运行,但不能与Azure Service应用程序一起使用 [英] Azure database works on localhost, but not when used with azure service app

查看:49
本文介绍了Azure数据库可在localhost上运行,但不能与Azure Service应用程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直试图将我的第一个项目发布为天蓝色.我已经完成了所有设置,服务应用程序和sql数据库.

So I've been trying to publish my first project to azure. I've got everything set-up, a service app and a sql database.

我的初始页面加载正确(这是.net核心Web应用程序的标准视图).

My initial page loads properly(It's the standard view for a .net core web application).

我需要做的第一件事是注册一个新用户.每当我尝试通过我的azure应用程序(myapp.azurewebsites.net)时,它都会失败,并且日志显示它与数据库相关.

The first thing I need to do is register a new user. Whenever I try through my azure app (myapp.azurewebsites.net) it fails and the logs says it's db related.

但是,我尝试通过在生产环境中的计算机上运行应用程序来尝试相同的操作,然后再次连接到Azure SQL服务器,并且一切运行正常.我可以注册用户,我可以创建帖子,我可以对其进行编辑.<允许访问天蓝色服务的代码选项已打开.此错误来自事件日志.我没有包括堆栈跟踪.

However I try the same thing by running the application on my machine in production environment, again connected to the azure sql server and everything works perfectly. I can register users, I can create posts, I can edit them. The allow access to azure services option is turned on. This error is from the eventlogs. I have not included the stacktrace.

Category: Microsoft.EntityFrameworkCore.Query EventId: 10100 RequestId: 800001be-0000-ba00-b63f- 
84710c7967bb RequestPath: /Identity/Account/Register SpanId: |1e5a93ae-43f424904f38ea9f. TraceId: 
1e5a93ae-43f424904f38ea9f ParentId: ActionId: c3430236-e61c-4785-a3c3-4f60ba115b6e ActionName: 
/Account/Register An exception occurred while iterating over the results of a query for context type 
'MyApp.Data.ApplicationDbContext'. Microsoft.Data.SqlClient.SqlException (0x80131904): Server 
name cannot be determined. It must appear as the first segment of the server's dns name 
(servername.database.windows.net). Some libraries do not send the server name, in which case the 
server name must be included as part of the user name (username@servername). In addition, if both 
formats are used, the server names must match.

这些是我尝试将连接字符串添加到appsettings.json文件的不同方式.(服务器名称,目录,用户和密码已替换,它们已正确写入appsettings文件中)

Those are the different ways I tried to add the connection string to the appsettings.json file. (Server name, catalog, user and password have been replaced, they are written correctly in the appsettings file)

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user@server;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;

推荐答案

好了,经过一天半的时间,我终于设法修复了它.解决方案非常简单,很可能是我的新手错误,造成了很多麻烦.

Alright so after a day and a half, I finally managed to fix it. The solution is rather simple and it is most likely my newbie mistake, that caused so much trouble.

此后,我一直在关注如何设置应用程序和数据库连接的教程.在本教程中,使用的连接字符串是默认字符串,可在"myApp->配置->连接字符串"中找到,格式为:

I was following a tutorial for setting up the application and database connection after that. In the tutorial, the connection string that was being used, was the default one, found in the "myApp -> Configuration -> Connection strings", the format was:

Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;

此人正在指南中工作,但不适合我.所以我要做的是转到我的"sqldb->连接字符串",然后复制那里提供的那个.然后,我返回到应用程序配置,并使用SqlServer作为类型将其添加为新的配置字符串.

This one was working in the guide, but not for me. So what I did, was go to my "sqldb -> connection strings" and copied the one provided there. I then went back to the app configuration and added it as a new configuration string using SqlServer as the Type.

此字符串的格式为:

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

此后,该应用程序开始正常运行.

After that, the app started working properly.

这篇关于Azure数据库可在localhost上运行,但不能与Azure Service应用程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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