簡體   English   中英

連接到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應用程序與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”

  • “NAME = MS_TableConnectionString”

它永遠都行不通:(

拜托,有人有主意嗎?

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

我需要轉到Function App的設置,並在設置中添加連接字符串,如下所述: Azure Functions數據庫連接字符串

然后,我需要使用以下代碼行:

string sConnexionString = "MS_TableConnectionString";

而且效果很好!

感謝Adrian Hall使我能夠優化搜索范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM