Type or Namespace name not found in Azure Functions

14,116

Solution 1

Problem is caused by the difference of Function runtime.

The tutorial you follow creates function on ~1 runtime where code targets at .NET Framework , while the one you create is on ~2 runtime which runs on .NET Core env. When we create a new Function app its runtime is set to ~2 by default now.

Solution is to set FUNCTIONS_EXTENSION_VERSION to ~1 in Application settings on portal. Then click View Files on function panel and edit function.json to make it work in v1--change eventHubName to path. See event hub trigger configuration.

And some improvements, remove those not used in the context along with System.ServiceModel.Primitives in project.json. Newtonsoft.Json assembly exists but is not added to host, need to reference it explicitly.

  1. assembly and namespace using

    #r "Newtonsoft.Json"
    
    using Newtonsoft.Json.Linq;
    using MongoDB.Bson;
    using MongoDB.Driver;
    using System.Security.Authentication;
    
  2. project.json.

    {
      "frameworks": {
        "net46":{
          "dependencies": {
            "MongoDB.Bson": "2.7.0",
            "MongoDB.Driver": "2.7.0"
          }
        }
       }
    }
    

Solution 2

If you are using run.csx you need to "import" your referenced assemblies by using #r.

The example below is from the documentation.

#r "System.Web.Http"

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public static Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
Share:
14,116
Krishan Madushanka
Author by

Krishan Madushanka

I am a mobile applications developer

Updated on June 05, 2022

Comments

  • Krishan Madushanka
    Krishan Madushanka almost 2 years

    I am trying to push some simulated data to azure iot hub and store the data received to iot hub in a mongo db using azure functions(C#). Receiveing iot hub messages up to azure functions is working. when I am trying to push them to mongo db as follows, it gives the following error. I followed this tutorial while doing this.

    my run.csx

    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel.Description;
    using MongoDB.Bson.IO;
    using MongoDB.Bson;
    using MongoDB;
    using MongoDB.Driver;
    using System.Security.Authentication;
    using System.Text;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    public static void Run(string myIoTHubMessage, TraceWriter log)
    {
        log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
        string deviceId="",data="";
        var raw_obj=JObject.Parse(myIoTHubMessage);
        deviceId=(string)raw_obj["device_id"];
        data=(string)raw_obj["Data"];
        Cosmos cosmos= new Cosmos(deviceId,data);
        cosmos.pushData();
    }
    
    //CosmosDB class
    public class Cosmos
    {
        string deviceId="",data="";
        public BsonDocument document = new BsonDocument();
        public Cosmos(string deviceId, string data)
        {
            this.deviceId=deviceId;
            this.data=data;
        }
        public void pushData()
        {
            MainAsync().Wait();
        }
        public async Task MainAsync()
        {
            string connectionString = 
        @"mongodb://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
            settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12};
            var mongoClient = new MongoClient(settings);
            IMongoDatabase db = mongoClient.GetDatabase("iot");
            var icollection = db.GetCollection<BsonDocument>(deviceId);
            document.Add("Data",data);
            await icollection.InsertOneAsync(document);
        }
    
    }
    

    my project.json file

    {
      "frameworks": {
        "net46":{
          "dependencies": {
            "Newtonsoft.Json": "10.0.3",
            "System.ServiceModel.Primitives":"4.4.0",
            "MongoDB.Bson": "2.4.0",
            "MongoDB.Driver": "2.4.0",
            "MongoDB.Driver.Core": "2.4.0"
          }
        }
       }
    }
    

    when I ran the code it gives following error

    2018-10-10T18:34:25.990 [Error] Function compilation error
    2018-10-10T18:34:26.119 [Error] run.csx(3,27): error CS0234: The type or namespace name 'Description' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)
    2018-10-10T18:34:26.242 [Error] run.csx(4,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.332 [Error] run.csx(5,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.435 [Error] run.csx(6,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.548 [Error] run.csx(7,7): error CS0246: The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.666 [Error] run.csx(10,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.771 [Error] run.csx(11,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:26.828 [Warning] run.csx(13,48): warning CS0618: 'TraceWriter' is obsolete: 'Will be removed in an upcoming version. Use ILogger instead.'
    2018-10-10T18:34:26.946 [Error] run.csx(28,12): error CS0246: The type or namespace name 'BsonDocument' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:27.058 [Error] run.csx(17,17): error CS0103: The name 'JObject' does not exist in the current context
    2018-10-10T18:34:27.201 [Error] run.csx(28,40): error CS0246: The type or namespace name 'BsonDocument' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:27.304 [Error] run.csx(42,5): error CS0246: The type or namespace name 'MongoClientSettings' could not be found (are you missing a using directive or an assembly reference?)
    2018-10-10T18:34:27.431 [Error] run.csx(42,36): error CS0103: The name 'MongoClientSettings' does not exist in the current context
    2018-10-10T18:34:27.632 [Error] Executed 'Functions.EventHubTriggerCSharp1' (Failed, Id=32bc6c5d-73fa-4082-b74b-c86a901f6656)
    

    Can someone help me to fix this issue?

  • Krishan Madushanka
    Krishan Madushanka over 5 years
    importing #r "Newtonsoft.Json" works but #r "System.Runtime.Serialization" and #r "MongoDB" do not work :/
  • Murray Foxcroft
    Murray Foxcroft over 5 years
    Are you sure the assemblies (and their dependencies) are all uploaded too? See here stackoverflow.com/questions/36559509/…
  • Krishan Madushanka
    Krishan Madushanka over 5 years
    I have added them in project.json file
  • Murray Foxcroft
    Murray Foxcroft over 5 years
    Please read the link I pasted in my last comment. You need to UPLOAD them to the Azure Functions instance as well.
  • Krishan Madushanka
    Krishan Madushanka over 5 years
    when I changed FUNCTIONS_EXTENSION_VERSION to ~1 it gives errror Function (EventHubTriggerCSharp1) Error: Can't figure out which ctor to call.
  • Jerry Liu
    Jerry Liu over 5 years
    @KrishanMadushanka Sorry for my omission, you also need click View files to edit function.json, change eventHubName to path.
  • Krishan Madushanka
    Krishan Madushanka over 5 years
    solved the problem by using javascript to write azure function
  • Jerry Liu
    Jerry Liu over 5 years
    @KrishanMadushanka Great, glad you made it. You can post your answer if it's not too much trouble.
  • Krishan Madushanka
    Krishan Madushanka over 5 years
    Sure I will post the answer. Anyway thanks for following up