连接到 Azure 数据库的 Azure Function App C# HTTP 触发器

Azure Function App C# HTTP Trigger connected to an Azure Database

我尝试为移动应用程序创建一个无服务器后端,遵循该示例(James 干杯):https://blog.xamarin.com/creating-a-serverless-backend-for-mobile-apps/

一切正常,直到我想将我的脚本连接到数据库。

我想将该 Azure Function App 与 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 尝试了很多字符串:

而且它永远行不通:(

拜托,有人有想法吗?

好的,我找到了答案...

我需要进入 Function App 的设置,并在设置中添加连接字符串,如下所述:

然后,我需要使用这行代码:

string sConnexionString = "MS_TableConnectionString";

而且效果很好!

感谢 Adrian Hall 让我改进了搜索