I'm trying to use an azure function to upload data to a mongodb but I get an error saying "The type or namespace name 'MongoDB' could not be found (are you missing a using directive or an assembly reference?)". I have a function.proj file that has refs to the MongoDB.Driver package but that doesn't seem to be working. I tried using the #r sytax to import the package but thats not working either. I'm using version 3 of the azure funtion runtime. Can anybody point me in the right direction?
Contents of the function.proj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver.Core" Version="2.11.3" />
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
<PackageReference Include="MongoDB.Bson" Version="2.11.3" />
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</ItemGroup>
Using directives
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using MongoDB.Driver;
using MongoDB.Bson;
Update:
Thanks for Ggd Hhdhd's working. Azure function can not install some package on azure when based on crx. The solution is to send related dll directly to the bin directory on azure. This is the doc:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#referencing-custom-assemblies
And this is the structure:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#folder-structure
Original Answer:
Please don't use C# script to do this.
Some package is not supported to install in C# script azure function on azure. I notice you install three package, but in fact, you just need to install MongoDB.Driver is ok. MongoDB.Driver is an integrated package that includes MongoDB.Driver.Core and MongoDB.Bson.
Problem is not from your code. The problem comes from just install the MongoDB.Driver package will broken the function.
By the way, the format of the function.proj should be like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
</ItemGroup>
</Project>
You miss the </Project>.
I try to find some package but didn't find.
Related
dotnet add package SimpleHelpers.FileEncoding
SimpleHelpers.FileEncoding
dotnet run
it shows:
C:\Users\user\Downloads\c2\Program.cs(1,7): Error CS0246: The type or namespace name 'SimpleHelpers' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\user\Downloads\c2\c2.csproj]
Program.cs:
using SimpleHelpers.FileEncoding;
namespace c2;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
var encoding = FileEncoding.DetectFileEncoding (#"D:\b\big5\LanTingJiXu_big5.txt");
}
}
c2.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SimpleHelpers.FileEncoding" Version="1.4.0" />
</ItemGroup>
</Project>
update the error message.
It looks like this package was authored to use the content folder with a .cs.pp file transform that were previously rendered and copied to the project folder for projects using packages.config to reference NuGet packages.
This is no longer supported for SDK-style projects which use PackageReference. The package author would need to switch to (or add) contentFiles to support PackageReference or create a pre-built binary (.dll file).
I'm trying to use EF Core 3 to delete all rows from a table like:
db.MyTable.ExecuteSqlRaw("delete from MyTable;");
But I get the error:
DbSet' does not contain a definition for 'ExecuteSqlRaw' and no accessible extension method 'ExecuteSqlRaw' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)
The Microsoft breaking changes page for EF Core 3 does not offer any advice on if there are special packages required to enable this:
https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#fromsql-executesql-and-executesqlasync-have-been-renamed
These are the Nuget packages I have installed:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Linq.Expressions" Version="4.3.0" />
<PackageReference Include="System.Linq.Queryable" Version="4.3.0" />
Using statements:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
Note that FromSqlRaw is available, but ExecuteSqlRaw, ExecuteSqlRawAsync , etc are not.
EDIT: I added a using Microsoft.EntityFrameworkCore and the error has changed to:
'DbSet' does not contain a definition for 'ExecuteSqlRaw' and the best extension method overload 'RelationalDatabaseFacadeExtensions.ExecuteSqlRaw(DatabaseFacade, string, params object[])' requires a receiver of type 'DatabaseFacade'
My edit about the new error led me to an answer:
The Microsoft Breaking Changes documentation is just not providing examples for the Execute methods. To get that to work you have to go through the "Database" property instead. So in short, to use those:
Make sure you have using Microsoft.EntityFrameworkCore;
If using an execute use myContext.Database.ExecuteSqlRaw(#"...sql to excxute...")
I had similar issue with dbContext.Database.ExecuteSqlRaw() and it has been fixed by installing package Microsoft.EntityFrameworkCore.SqlServer without even using the package.
try install-package Microsoft.EntityFrameworkCore.SqlServer in your current project.
You need to add NuGet package reference Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions to your project.
I am trying to make distinction between these two concepts in .NET Core terminology. I will try to illustrate my confusion with an example.
When I create a new class library project (eg: dotnet new classlib -o myclasslib) the generated .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
So far so good. Now, if I try to add WebApi controller class in this project (eg. for the purpose of dynamically loading plugins in my main WebApi application created as: dotnet new webapi -o mywebapi) I would need to use things like ControllerBase class and [ApiController] and [HttpGet] attributes. To keep things simple I just derive MyController from ControllerBase like this:
using System;
namespace myclasslib
{
public class MyController : Microsoft.AspNetCore.Mvc.ControllerBase
{
}
}
Trying to build this with dotnet build I get error:
error CS0234: The type or namespace name 'AspNetCore' does not exist
in the namespace 'Microsoft' (are you missing an assembly reference?)
That's kind of expected because I created classlib project, but if change SDK in .csproj to Sdk="Microsoft.NET.Sdk.Web" and also change TargetFramework to netcoreapp2.2 (hoping to resolve the reference to ControllerBase class) like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
</Project>
I get the same error when building the project. Isn't SDK supposed to include everything I need to build the project?
If I create usual webapi project (eg. dotnet new webapi -o mywebapi) the generated .csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
</Project>
I notice the SDK is same as the one I set, but there is also a metapackage added: <PackageReference Include="Microsoft.AspNetCore.App" />
So why do we need to explicitly add metapackage if we already specified that we want to use Microsoft.NET.Sdk.Web?
One additional question: what version of metapackage is used if we don't specify one in PackageReference (like in this generated webapi .csproj)?
The SDK is just the build tooling and the .NET Core framework itself. ASP.NET Core is a set of NuGet packages. Essentially .NET Core framework != ASP.NET Core code. The concept of metapackages is only tangentially related. What could be called "ASP.NET Core" is actually dozens of individual NuGet packages. You could reference each individually, but as you can imagine, that would be both tiresome and error prone. Metapackages are essentially a NuGet package that depends on multiple other NuGet packages.
By pulling in just the metapackage, therefore, essentially all of that metapackage's dependencies are also pull in. As a result, you can simply add a package reference for Microsoft.AspNetCore.App and you're off to the races. However, the downside to that is that you're potentially getting dependencies that you don't actually need. That's not that big of an issue with something like a web app, because the dependencies can be trimmed, but a class library should not have excess dependencies. As such, you should only reference individual NuGet packages you actually need from the Microsoft.AspNetCore namespace.
Is it possible to add the reference for ExcelDataReader and ExcelDataset in Azure functions? I uploaded dll file of NuGet packages (exceldatareader.3.4.0.nupkg) to the program. But I'm not sure how actually it has to be used. Can anyone help me to achieve this.
#r "Microsoft.WindowsAzure.Storage"
#r "System.IO"
#r "System.Data"
#r "exceldatareader" //*I'm not able to add this reference|*
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using ExcelDataReader;
using System.Data;
Thank you.
To install packages for Azure functions on portal, we need to create dependencies file under function folder. Click View files on the right of function panel, Add file according to runtime.
If your function runtime is ~1, create project.json with following content.
{
"frameworks": {
"net46":{
"dependencies": {
"ExcelDataReader.DataSet": "3.4.0"
}
}
}
}
If your function runtime is ~2, create function.proj as below.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExcelDataReader.DataSet" Version="3.4.0"/>
</ItemGroup>
</Project>
After file created, assemblies will be added to function host environment. Also need to remove unnecessary #r "exceldatareader" or you will got error.
I would like to reference the System.Drawing.dll in a console app I am writing using Visual Studio Code on OSX. i.e. I want to use these using statements
using System.Drawing;
using System.Drawing.Imaging;
to avoid this build error
Program.cs(56,20): error CS0246: The type or namespace name `Bitmap' could not be found. Are you missing an assembly reference?
I can't find a tutorial on this, I don't even know if the dll is available in .net core or mono or whatever visual-studio-code uses.
In your .csproj file, add your dependency as a PackageReference in an ItemGroup, then run dotnet restore or nuget restore. Example:
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Xamarin.iOS" />
<PackageReference Include="Realm" Version="2.1.0" />
<PackageReference Include="xunit">
<Version>2.3.1</Version>
</PackageReference>
</ItemGroup>
Take a look at this article for a full explanation.
The new .NET Core SDK restore command is dotnet restore
To add any asssembly reference in Visual Studio Code, please refer to my post.
Mono offers a WinForms pipeline implementation that you can leverage, that includes support for System.Drawing.