我尝试为移动应用创建无服务器后端,跟随该示例(欢呼詹姆斯):https://blog.xamarin.com/creating-a-serverless-backend-for-mobile-apps/
对我来说一切正常,直到我想将我的脚本连接到数据库。
我想将Azure功能应用程序与Azure数据库一起使用。例如,我希望我的移动应用程序调用http请求(触发功能应用程序),功能应用程序向数据库发出查询,然后将http请求发送回移动设备。 我试着关注该视频:https://channel9.msdn.com/Series/Windows-Azure-Web-Sites-Tutorials/Create-an-event-processing-Azure-Function?ocid=player 在此视频中,开发人员将其脚本连接到数据库并对数据库执行查询。
这是让我失败的一步:(
这是我的代码:
#r "System.Configuration"
#r "System.Data"
using System.Net;
using System.Configuration;
using System.Data.Sql;
using System.Threading.Tasks;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
if (req == null)
log.Info("req is null");
else
log.Info("req is not null");
//log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
string sConnexionString = "Name=MS_TableConnectionString";
log.Info(sConnexionString);
if (ConfigurationManager.ConnectionStrings[sConnexionString] == null)
log.Info("ConfigurationManager.ConnectionStrings[sConnexionString] is null");
else
log.Info("ConfigurationManager.ConnectionStrings[sConnexionString] is not null");
var str = ConfigurationManager.ConnectionStrings[sConnexionString].ConnectionString;
if (str == null)
log.Info("str is null");
else
log.Info("str is not null");
using (SqlConnection conn = new SqlConnection(str))
{
if (conn == null)
log.Info("conn is null");
else
log.Info("conn is not null");
conn.Open();
var text = "INSERT INTO MyEasyTable(id) values (1)";
using (SqlCommand cmd = new SqlCommand(text, conn))
{
var rows = await cmd.ExecuteNonQueryAsync();
log.Info($"{rows} inserted.");
}
}
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
这是输出日志:
2016-10-24T13:19:09.452 Compilation succeeded.
2016-10-24T13:19:13.257 Function started (Id=6ffaade4-58b0-4005-8fe7-7dc06b96c2b7)
2016-10-24T13:19:14.018 req is not null
2016-10-24T13:19:14.018 Name=MS_TableConnectionString
2016-10-24T13:19:14.018 ConfigurationManager.ConnectionStrings[sConnexionString] is null
2016-10-24T13:19:14.018 Function completed (Failure, Id=6ffaade4-58b0-4005-8fe7-7dc06b96c2b7)
2016-10-24T13:19:14.037 Exception while executing function: Functions.HttpTriggerCSharp1. HttpTriggerCSharp1: Object reference not set to an instance of an object.
由于该日志,我了解ConfigurationManager无法连接到我的Azure数据库:(
我为sConnexionString尝试了很多字符串:
“数据源= tcp:BDDSERVERNAME.database.windows.net,1433;初始目录= BDD_NAME;用户ID = USER @ BDDSERVERNAME;密码= MYPASSWORD”
“MS_TableConnectionString”
“名称= MS_TableConnectionString”
它永远不会奏效:(
拜托,有人有想法吗?
答案 0 :(得分:3)
好的,我找到了答案......
我需要转到功能应用程序的设置,并在设置中添加连接字符串,如下所述:Azure Functions Database Connection String
然后,我需要使用这行代码:
data_set_data_provider.py
效果很好!
感谢Adrian Hall允许我优化我的搜索