Azure function v2. Cannot reference dll from nuget

0

I am testing how to write a simple azure function that updates an azure sql database. From VS2017 I made a new Cloud->Azure function v2 with netstandars 2.0. My code is the following:

[FunctionName("Activate")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        string serialNumber = req.Query["SerialNumber"];

        try
        {
            var str = "CONNECTION_STRING_FOR AZURE SQL";
            using (SqlConnection conn = new SqlConnection(str))
            {
                conn.Open();
                var text = $"UPDATE CLIENT SET ACTIVATION_DATE = '2018-07-23 WHERE SERIAL_NUMBER='{serialNumber}'";

                using (SqlCommand cmd = new SqlCommand(text, conn))
                {
                    // Execute the command and log the # rows affected.
                    var rows = cmd.ExecuteNonQuery();
                    log.Info($"{rows} rows were updated");
                }
            }
        }
        catch (Exception ex)
        {
            return new BadRequestObjectResult(ex.Message);
        }

        return serialNumber != null
            ? (ActionResult)new OkObjectResult($"Hello, {serialNumber}")
            : new BadRequestObjectResult("error");
    }

It compiles and can be run locally from visual studio:

Hosting environment: Production
Content root path: C:\Projects\xxx\bin\Debug\netstandard2.0
Now listening on: http://localhost:7071
Application started. Press Ctrl+C to shut down.
[24/7/2018 8:50:48 πμ] Reading host configuration file 'C:\Projects\xxx\bin\Debug\netstandard2.0\host.json'
[24/7/2018 8:50:48 πμ] Host configuration file read:
[24/7/2018 8:50:48 πμ] {}
[24/7/2018 8:50:48 πμ] Starting Host (HostId=xxx-1133968365, InstanceId=58079dab-xxx-4bb9-aaf5-f24xxx63d3c, Version=2.0.11651.0, ProcessId=11304, AppDomainId=1, Debug=False, ConsecutiveErrors=0, StartupCount=1, FunctionsExtensionVersion=)
[24/7/2018 8:50:48 πμ] Unable to configure java worker. Could not find JAVA_HOME app setting.
[24/7/2018 8:50:48 πμ]
[24/7/2018 8:50:48 πμ] Could not configure language worker Java.
[24/7/2018 8:50:48 πμ]
[24/7/2018 8:50:49 πμ] Generating 1 job function(s)
[24/7/2018 8:50:49 πμ] Found the following functions:
[24/7/2018 8:50:49 πμ] ClientFunctions.Run
[24/7/2018 8:50:49 πμ]
[24/7/2018 8:50:49 πμ] Host initialized (1511ms)
[24/7/2018 8:50:49 πμ] Host started (1616ms)
[24/7/2018 8:50:49 πμ] Job host started
Listening on http://localhost:7071/
Hit CTRL-C to exit...

Http Functions:

        Activate: http://localhost:7071/api/Activate

But if I access it through postman I get:

[24/7/2018 8:52:58 πμ] Executing 'Activate' (Reason='This function was programmatically called via the host APIs.', Id=30cff68d-b8db-446e-bd6d-407990524198)
[24/7/2018 8:52:58 πμ] Executed 'Activate' (Failed, Id=30cff68d-b8db-446e-bd6d-407990524198)
[24/7/2018 8:52:58 πμ] System.Private.CoreLib: Exception while executing function: Activate. Mik3UpdateFunctionNew: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

What I find strange is that each time I start VS2017 and debug the program, a window appears that says downloading azure functions cli tools 2.3.2.

Can someone suggest anything? Thanks

azure
azure-functions
asked on Stack Overflow Jul 24, 2018 by Harris • edited Jul 24, 2018 by CHEEKATLAPRADEEP-MSFT

2 Answers

2

@Josh is right. The downloading tip you saw means VS tried to download the latest functin CLI. But actually the downloading failed and it fell back to use the old one, as you can see in your CLI output

Starting Host (...,Version=2.0.11651.0,...)

It represents the function host version, which is used by CLI 2.0.1-beta.25. While for now the latest host is 2.0.11933.0 with CLI 2.0.1-beta.33, i.e.2.3.2 specified by VS.

Here are some details about how to consume the latest CLI.

  1. If you already has the tools folder %LocalAPPDATA%\AzureFunctionsTools, delete it to avoid corrupting new tools.

  2. On VS menus, Tools->Extensions and Updates, find Azure Functions and Web Jobs Tools, make sure it's updated to latest version(right now 15.0.40617.0).

  3. If it has been the latest, just restart VS and create a new Azure Function project, wait at the create dialog for VS to download new CLI and templates.

enter image description here

After a while, we can see the tip change to

enter image description here

Then you can see Starting Host (...,Version=2.0.11933.0,...) while debugging locally and your code should work.

If you still see 2.0.11651.0, go check whether this folder%LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2exists, which contains cli, templates and manifest.json. If the downloading turns out failed, just delete %LocalAPPDATA%\AzureFunctionsTools folder and restart VS to download again.

Update--how to download tools manually due to poor network.

Open %LocalAPPDATA%\AzureFunctionsTools\feed.json, find "2.3.2":{...} content. Download its cli, itemTemplates and projectTemplates.(Need to rename manually after download).

And folder structure in %LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2 is like this(same to 2.0.1-beta.25 folder)

enter image description here

cli
--...
templates
--ItemTemplates.nupkg
--ProjectTemplates.nupkg
manifest.json

Content of manifest.json

{
  "ReleaseName": "2.3.2",
  "CliEntrypointPath":"C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.3.2\\cli\\func.exe",
  "TemplatesDirectory": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.3.2\\templates",
  "FunctionsExtensionVersion": "beta",
  "SdkPackageVersion": "1.0.14"
}
answered on Stack Overflow Jul 25, 2018 by Jerry Liu • edited Jul 25, 2018 by Jerry Liu
0

The prompt you are seeing is the Azure Functions extensions for Visual Studio updating your local Azure Functions CLI to the latest version. There has been a lot of updates recently with the Azure Functions 2.0 preview, especially with how assemblies are being resolved (which falls into the category of your error). I would encourage you to allow the update to complete. With all the changes occurring as of late with 2.0 it's easy to get some mismatched versions and references between local dev and whats deployed to azure that causes various problems.

If the error still occurs I would suggest opening an issue on Github with the error details as they are looking for feedback in regards to any assembly binding errors that folks are still getting.

answered on Stack Overflow Jul 24, 2018 by Josh Carlisle

User contributions licensed under CC BY-SA 3.0