Azure Function beta/v2 - Integration Dependensies - SQL/MySql
Clash Royale CLAN TAG#URR8PPP
Azure Function beta/v2 - Integration Dependensies - SQL/MySql
I'm using the beta version of the Azure Functions because I'm using the Graph API and I would like to connect a database (MySQL/SQL).
Just using the latest beta version/v2 with the following guide from Microsoft to connect a SQL Database I get an error.
I know that there are a few differences regarding the integration of the dependencies. I mean the use and the structure of the file "project.json
" instead of "function.proj
"
project.json
function.proj
What I have to do to make it work?
This is the returned error. I remind that it's with the beta version because in my real project I use the Graph Api as well:
2018-08-08T06:43:59 Welcome, you are now connected to log-streaming service.
2018-08-08T06:44:06.613 [Information] Executing 'Functions.TimerTriggerCSharp1' (Reason='This function was programmatically called via the host APIs.', Id=a3eb2b63-b336-4f56-bc3e-a1862b8f2d06)
2018-08-08T06:44:06.671 [Information] C# Timer trigger function executed at: 8/8/2018 6:44:06 AM
2018-08-08T06:44:06.786 [Error] System.Private.CoreLib: Exception while executing function: Functions.TimerTriggerCSharp1. System.Private.CoreLib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__-1020707796: Object reference not set to an instance of an object.
2018-08-08T06:44:06.906 [Error] Executed 'Functions.TimerTriggerCSharp1' (Failed, Id=a3eb2b63-b336-4f56-bc3e-a1862b8f2d06)
2018-08-08T06:45:07.294 [Information] Executing 'Functions.TimerTriggerCSharp1' (Reason='Timer fired at 2018-08-08T06:45:07.2936684+00:00', Id=2a28db1e-f70f-4919-b7c9-078d39fb12a6)
2018-08-08T06:45:07.300 [Information] C# Timer trigger function executed at: 8/8/2018 6:45:07 AM
2018-08-08T06:45:07.303 [Error] System.Private.CoreLib: Exception while executing function: Functions.TimerTriggerCSharp1. System.Private.CoreLib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__-1020707796: Object reference not set to an instance of an object.
2018-08-08T06:45:07.893 [Error] Executed 'Functions.TimerTriggerCSharp1' (Failed, Id=2a28db1e-f70f-4919-b7c9-078d39fb12a6)
This instead is my "run.csx" file:
#r "System.Configuration"
#r "System.Data"
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;
public static void Run(TimerInfo myTimer, ILogger log)
log.LogInformation($"C# Timer trigger function executed at: DateTime.Now");
var str = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(str))
conn.Open();
//Just a test
var text = "UPDATE Persons SET FirstName = 'Alfred Schmidt', LastName= 'Frankfurt'";
using (SqlCommand cmd = new SqlCommand(text, conn))
// Execute the command and log the # rows affected.
//var rows = await cmd.ExecuteNonQueryAsync();
//log.Info($"rows rows were updated");
log.LogInformation($"C# Timer trigger function executed at: DateTime.Now");
Update
I'm trying to connect a MySQL database instead of a SQL and I get the following error:
2018-08-08T12:54:46.569 [Error] run.csx(15,27): error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?)
These are my dependencies:
#r "System.Configuration"
#r "System.Data"
using System;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Threading.Tasks;
var str = Environment.GetEnvironmentVariable("MYSQLCONNSTR_MySqlConnection");
The link you posted for the "guide from Microsoft to connect a SQL database" is just a link to create a SQL database in the Azure Portal. Is this what you meant to post? +1 to error message too!
– Marie Hoeger
Aug 7 at 23:31
Hi Alberto, Hi Marie, Many thanks for your reply. I added copy of the code so you can give me a better support. The code is exactly that one in the Microsoft example but I'm using the beta version of the Azure Functions because in my real project I need to use also the Graph Api.
– Filippo
Aug 8 at 6:57
1 Answer
1
ConfigurationManager is no longer supported in v2 function, see this relate comment.
In 2.0, we have moved to the new ASP.NET Core configuration model. We intend to support binding to an IConfiguration instance that will provide a similar API, but in the meantime, the workaround is to read environment variables as opposed to relying on the ConfigurationManager and its APIs.
So you could use following statement to get connection string. Prefix SQLAZURECONNSTR_
is added by Azure for SQLAzure connection string set under Connection strings part.
SQLAZURECONNSTR_
var str = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_SqlConnection");
Update for MySql Connection
Go to https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/
, it shows content of function app. Right click on your function folder, New file, create function.proj
as below. Adding this dependency file imports related assembly for you to reference.
https://<functionappname>.scm.azurewebsites.net/dev/wwwroot/
function.proj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.12" />
</ItemGroup>
</Project>
Hi Jerry, Many thanks for your prompt reply. It is actually one step forward. :) However, if I uncomment the following lines to get the resuly from the query to the DB I get the following error: 2018-08-08T09:57:16.506 [Error] run.csx(22,24): error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. How can I fix it?
– Filippo
Aug 8 at 9:58
@Filippo Just change your method signature to
public static async Task Run
.– Jerry Liu
Aug 8 at 10:03
public static async Task Run
I'm infinitely grateful to you for your help! :D I spent so much time trying to make it working that you can't even imagine. There is not any good documentation online regarding the beta version.
– Filippo
Aug 8 at 12:10
@Filippo Not at all, could you accept my answer to close this question?
– Jerry Liu
Aug 8 at 12:52
Please, only one more question. I'm trying to connect a MySQL database instead of a SQL and I get the following error: 2018-08-08T12:54:46.569 [Error] run.csx(15,27): error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?) These are my dependencies: #r "System.Configuration" #r "System.Data" using System; using System.Configuration; using MySql.Data.MySqlClient; using System.Threading.Tasks; var str = Environment.GetEnvironmentVariable("MYSQLCONNSTR_MySqlConnection"); Thank u in advance!
– Filippo
Aug 8 at 13:06
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please share the error message you have received
– Alberto Morillo
Aug 7 at 14:40