0

I am attempting to publish my Azure HTTP function from Visual Studio Code to our Azure platform.

The code works fine when running the function locally and publishes successfully but throws out the following error when published.

I have tried using DocumentDB instead of CosmosDB but that lacks the insert functionality required for inserting data into CosmosDB. Stackoverflow has no solutions to a problem this specific.

Function code

//write to cosmosdb
[FunctionName("InsertItem")] 
public static HttpResponseMessage Run( 
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, 
    [CosmosDB( 
        databaseName: "ToDoList", 
        collectionName: "RFIDContainer", 
        ConnectionStringSetting = "myCosmosDBConnection")] 
    out RFIDBaseTag document, 
    ILogger log) 
{ 
    string hexData = "";
    string afi = "";
    string eid = "";
    string dsfid = "";
    //Guid DeviceID = new Guid();
    //Guid AppID = new Guid();

    var content = req.Content; 
    string jsonContent = content.ReadAsStringAsync().Result; 

    dynamic json = JsonConvert.DeserializeObject<MyClass>(jsonContent);

    hexData = json?.hexData;
    afi = json?.afi;
    eid = json?.eid;
    dsfid = json?.dsfid;

    /*Guid devGuid;
    Guid.TryParse(json.AppID.ToString(), out devGuid);
    DeviceID = devGuid;

    Guid appGuid;
    Guid.TryParse(json.AppID.ToString(), out appGuid);
    AppID = appGuid;*/

    byte[] hexToByte = AzureRFIDTagReader.StringToByteArray(hexData);

    RawRFIDReading raw = new RawRFIDReading();

    raw.afi = afi;
    raw.eid = eid;
    raw.dsfid = dsfid;
    raw.RawData = hexToByte;

    RFIDBaseTag rtag = RFIDTagFactory.GetTag(raw);
    string serializedtag = JsonConvert.SerializeObject(rtag);

    //document = JsonConvert.DeserializeObject<MyClass>(jsonContent);
    //document = JsonConvert.DeserializeObject<RFIDBaseTag(serializedtag);
    document = rtag;

    log.LogInformation($"C# Queue trigger function inserted one row");

    return new HttpResponseMessage(HttpStatusCode.Created); 
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=zzzz",
    "myCosmosDBConnection": "AccountEndpoint=xxx:443/;AccountKey=www;"
  }
}

Error Message:

Function (xxx/InsertItem) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'InsertItem'. Microsoft.Azure.WebJobs.Host: Unable to resolve the value for property 'CosmosDBAttribute.ConnectionStringSetting'. Make sure the setting exists and has a valid value.

I am able to post to the function locally but not on Azure.

Any suggestions?

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Did you add the connection string setting to the Function app? – DavidG Sep 20 '19 at 15:30
  • As per @Matias' answer, `local.settings.json` isn't published to Azure (as the file name suggests). [Here's one way](https://stackoverflow.com/a/53881986) to substitute your "real" Azure deployed settings after the function is deployed to Azure. – StuartLC Sep 20 '19 at 15:40

1 Answers1

3

When you publish your Function, your local.settings.json file is not published.

You need to add those settings as part of the Azure Functions Application Settings. In your case, you need to add myCosmosDBConnection there with the value.

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • Yes, that all makes perfect sense. New function variable can be set at - Function App Repo -> Function App Settings -> Manage Application Settings -> + New Application setting. Cheers! – gmac010101 Sep 23 '19 at 10:57