Error CS0006: Metadata file 'MongoDB' could not be found - c#

I have created a Azure Function and it has below code in `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;
public static void Run(string myIoTHubMessage, ILogger log)
{
log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
}
I am having Project.json as follow
{
"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"
}
}
}
}
I am getting below error while running the azure function
2019-01-11T10:01:14.846 [Error] run.csx(5,27): error CS0234: The type or namespace name 'Description' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?)
2019-01-11T10:01:15.108 [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?)
I even tried adding namespace like below but no luck
#r "Newtonsoft.Json"
#r "System.Xml"
#r "System.Xml.Linq"
#r "MongoDB"

It's probably caused by the difference of Function runtime.
project.json is used for functions on ~1 runtime where code targets at .NET Framework, while the function 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.
So the solution is simple, delete existing functions in the Function app and change Function runtime version to ~1(Find it in portal, Platform features>Function app settings). Then you could recreate an IoT Hub (Event Hub) trigger with steps above, things should work this time.
To work with Function 2.0, use function.proj to install packages.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="<packageName>" Version="<version>"/>
</ItemGroup>
</Project>

Related

ServiceBusTrigger not found in basic Azure Function project

I have created a brand new .NET Core 3.1 Azure Function project using the Visual Studio template
I am using V3 of the functions, i.e. the version created by the template
I have the latest VS - 16.10.2
Straight away, it fails with ServiceBusTrigger and Connection not found?
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace AzureFunctionTest
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("AzureFunctionTest", "Research Subscription", Connection = "eventbus-connection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
}
}
Really strange how a template from MS itself does not work and a bit concerning this fails on the first hurdle!!
Here are the relevant parts of my project file
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.1.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
Azure.Messaging.ServiceBus seems to be the latest package and the one recommended to use
What am I missing?
Paul
You should be using Microsoft.Azure.WebJobs.Extensions.ServiceBus package. Link.
You can read this official documentation for further information.

The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft'?

I am using ->
Visual Studio 2017 Enterprise Edition,
,.Net Framework 4.5.2
,Microsoft.ServiceBus.dll version 3.0.0.0
In my local system not getting any issues but after pushing changes into release pipeline getting error like below.
Error CS0234: The type or namespace name 'ServiceBus' does not exist
in the namespace 'Microsoft' (are you missing an assembly reference?
Did I missed anything
Add "Microsoft.Azure.ServiceBus" package to your project:
NuGet: Microsoft.Azure.ServiceBus 3.0.0
Configure your Azure DevOps Pipeline with a NuGet Restore:
Azure DevOps: NuGet task

How to reference Cosmos table API from within azure function?

I'm trying to compile a project I've written in C# script, in the azure portal(can't get any other environment to work).
I'm trying to reference the cosmos table API within my function:
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "Microsoft.Azure.WebJobs"
#r "Microsoft.Azure.WebJobs.Extensions"
#r "Microsoft.Azure.WebJobs.Extensions.Storage"
#r "Microsoft.Azure.Cosmos.Table"
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host.Bindings.Runtime;
using Microsoft.Azure.Cosmos.Table;
But it gives me the following error:
[Error] run.csx(6,1): error CS0006: Metadata file 'Microsoft.Azure.Cosmos.Table' could not be found
2019-09-26T13:50:37.115 [Error] run.csx(14,23): error CS0234: The type or namespace name 'Cosmos' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
How can I make azure function find the reference?
This namespace is not included in Azure function. You need to add the dll file to Azure function app by yourself. Refer to this article for the detailed steps.

'ConfigurationBuilder' does not contain a definition for 'AddJsonFile'

I have the following error:
Program.cs(15,72): error CS1061: 'ConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly
The project is a dotnet core app, console application, using Azure Search SDK
The line of error is below:
using System;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Spatial;
namespace DemoSearchIndexer
{
class Program
{
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
...
The AddJsonFile extension method is available in NuGet:
Microsoft.Extensions.Configuration.Json
When building an ASP.NET Core application, which usually references Microsoft.AspNetCore.App (or, historically, Microsoft.AspNetCore.All), you get this "for free".
When building a console application, or something that doesn't reference the metapackages, you need an explicit reference to Microsoft.Extensions.Configuration.Json.
You also have to set appsettings file properties to "copy always".
See this for more infos.

RestSharp library not found when running csc

I have this code:
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient
{
BaseUrl = new Uri("www.newuri.com"),
Authenticator = new HttpBasicAuthenticator(username: "myusername", password: "mypassword")
};
var request = new RestRequest(Method.POST);
var response = client.Execute(request);
}
}
}
I'm coding using VS2017 and this program compiles perfectly on it. The problem is that I want to compile and run it via Command Prompt and when I try to run command:
csc Program.cs
I'm getting error:
Program.cs(3,7): error CS0246: The type or namespace name 'RestSharp' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(4,7): error CS0246: The type or namespace name 'RestSharp' could not be found (are you missing a using directive or an assembly reference?)
Am I missing something when compiling via CMD?
If you compile with csc, you will need to manually specify the paths to all your references.
Few people compile by manually invoking csc. Instead, they use build software which understands the csproj file format and runs csc for them.
Can you try running msbuild or dotnet build? That will respect all your references you've added in Visual Studio.

Categories

Resources