dotnet publish fails with 'Metadata generation failed' sometimes - azure

We have an Azure function app. Runtime version is 2. Occasionally this command fails on our build server (Jenkins running on Windows Server):
dotnet publish C:\temp\OurFunctionApp.csproj -c Release -o C:\temp\output
The error:
C:\Users\user1.nuget\packages\microsoft.azure.webjobs.script.extensionsmetadatagenerator\1.0.1\build\Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator.targets(20,5): error : Metadata generation failed. [C:\temp\OurFunctionApp.csproj]
When I run this command from command line it always works fine.
We don't have Visual Studio installed on our build server.
How to diagnose and fix this error?
.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.8.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="3.0.1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Project1\Project1.csproj" />
<ProjectReference Include="..\Project2\Project2.csproj" />
<ProjectReference Include="..\Project3\Project3.csproj" />
<ProjectReference Include="..\Project4\Project4.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="ourlist.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

I have the same issue with Net Framework 4.7.
To solve the problem you should update Microsoft.NET.Sdk.Functions nuget package.

In order to avoid the same issue, i have created a Windows accounts (Admin) by DevOps Agent.
The goal of that is to have for each account it's own folder for nugets. (No more // issues on the same folder)
C:\Users\Agent1.nuget
C:\Users\Agent2.nuget
...
Since i have change all Windows service with the new windows account i have no more errors in my builds ;) (need to restart the Agent Windows Service)

After sometime I just deleted and recreate the application. That worked to me

My problem was that I updated Microsoft.NET.Sdk.Functions to version 3+ while I still had a version 2 function. I installed and target v1.0.37 and that resolved the problem

Adding the following to the csproj file:
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
is what fixed it for me.

Related

Why host.json not being copied to output in F# Azure Functions project?

In simple F# Azure Functions project host.json not being copied to output in F# Azure Functions project (same local.settings.json) even if specified so in fsharp-azure-functions-signalr-problem.fsproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<RootNamespace>fsharp_azure_functions_signalr_problem</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.0.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
</ItemGroup>
<ItemGroup>
<Compile Include="negotiate.fs" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Same C# project have no such problem.
Example of my F# project you can git clone https://github.com/ed-ilyin/fsharp-azure-functions-signalr-problem.git
Without host.json file I have following error:
> func start
...
Microsoft.Azure.WebJobs.Host: Error indexing method 'negotiate'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'connectionInfo' to type SignalRConnectionInfo. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
...
So manual copy to compiled folder can solve this problem. I have test the F#, it seems problem comes from your .fsproj file, the definition of host.json should look like this:
<Content Include="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
This work fine on my side.
None Include works too:
<None Include="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
The problem is tracked in this issue.
C# projects by default include all files via a wild card. Thats why Update works. In F# no files are included by default (because file order matters). So there is nothing to update.

Azure function v2, Core 3.1 Could not load file or assembly System.Memory, Version=4.2.0.0

I'm running locally an Azure function v2 on Core 3.1. Function connects reads events from an EventHub and write data to a Redis database.
While connecting function gets an error
System.Private.CoreLib: Exception while executing function:
One or more errors occurred. (Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.).
Pipelines.Sockets.Unofficial: Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
Same Redis connection code works normally as expected outside the Azure function.
I have installed System.Memory nuget package v 4.5.3 into the project but it does not help.
There is no System.Memory version 4.2 listed in nuget for Core 3.1
function uses Startup
[assembly: FunctionsStartup(typeof(...))]
The project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Configuration\Configuration.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
What's wrong?
The problem was fixed by changing the Azure functions version to v3 in project .csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
If the problem persists, verify that you are using the correct version of Azure Functions Core Tools (v3), it happened to me that I took version 2 from nodejs that is, I had version 2 installed in nodejs and version 3 in the folder From microsoft in the path they were both but I took the first one that was the nodejs one and it kept giving me the error until I delete the nodejs one, you can update it to version 3 in nodejs and I suppose it will work the same.
Can you try to add true to your .csproj file:
<PropertyGroup>
...
<UseNETCoreGenerator>true</UseNETCoreGenerator>
</PropertyGroup>

Azure functions runtime exception, the type initializer for system data sqlclient excetion, Unable to load DLL 'sni.dll'

I am using aspnet core 3.0 and azure function v3-preview with system.data.sqlclient version 4.7.0
When i try to run azure function(on both service queue trigger and time trigger) it gives below error :
The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception. The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found.
I tried solutions from below links but didn't work:
1. Azure Function - System.Data.SqlClient is not supported on this platform
2. https://github.com/Azure/app-service-announcements-discussions/issues/9
I tried using "Microsoft.Data.SqlClient" for aspnetcore 3.0 But still the same exception occurs while running Azure function on Azure portal.
Please help !
Here is csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AzureFunctionsVersion>v3-preview</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.30-beta2" />
<PackageReference Include="runtime.native.System.Data.SqlClient.sni" Version="4.6.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<!-- For publish --><!--
<ItemGroup>
<None Include="$(USERPROFILE)\.nuget\packages\\microsoft.data.sqlclient\1.0.19249.1\runtimes\win\lib\netcoreapp2.1\microsoft.Data.SqlClient.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
--><!-- For local debug --><!--
<Target Name="CopyToBin" BeforeTargets="Build">
<Copy SourceFiles="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient\1.0.19249.1\runtimes\win\lib\netcoreapp2.1\microsoft.Data.SqlClient.dll" DestinationFolder="$(OutputPath)\bin" />
</Target>-->
<ItemGroup>
<None Include="$(USERPROFILE)\.nuget\packages\\system.data.sqlclient\4.7.0\runtimes\win\lib\netcoreapp2.1\system.Data.SqlClient.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="CopyToBin" BeforeTargets="Build">
<Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.data.sqlclient\4.7.0\runtimes\win\lib\netcoreapp2.1\system.Data.SqlClient.dll" DestinationFolder="$(OutputPath)\bin" />
</Target>
</Project>
Please make sure your azure function meet these criteria in this link.
And here is a workaround provided by others, add the following to your .csproj:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
</Target>
<Target Name="PostPublish" BeforeTargets="Publish">
<Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
</Target>
It worked for me by adding below code in csproj file :
<Target Name="PostPublish" BeforeTargets="Publish">
<Exec Command="move $(PublishDir)\runtimes $(PublishDir)\bin" />
</Target>
This line should be taking care of that:
<Exec Command="move $(PublishDir)\runtimes $(PublishDir)\bin" />
reference : github

Version problem on Azure Functions v1 (using .Net461/.Net472) with NotificationHub v3.1.0

I would like to run Azure Function v1 with .Net461/.Net472 for testing NotificationHub package. However, there is exception when executing below code:
await _hub.CreateOrUpdateRegistrationAsync(registration);
Error is :
Exception while executing function: PushRegister. Microsoft.Azure.NotificationHubs: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=XXX' or one of its dependencies. The system cannot find the file specified.
I tried to downgrade to 10.0.3 then had error like
Microsoft.Azure.NotificationHubs: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0 ...
Below is the XXX.csproj file for reference:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AzureFunctionsVersion>v1</AzureFunctionsVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.3" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\YYY\YYY.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Most of the Nuget libraries have the dependency on Newtonsoft package, even the Microsoft.NET.Sdk.Functions has a dependency on it but it is of older version.
You cannot use any other Nuget Packages that have a dependency on Newtonsoft.Json version which is higher than the function runtime dependent version.
When you try to install a NuGet which has the dependency on a higher version of Newtsonsoft package, you simply cannot install it. The only solution is to use an older version of that library that you need.
I have also written a blog on this and on github this is already an open issue
https://medium.com/#hharan618/common-issues-while-development-of-azure-functions-76b08299af58

Deploying a netcoreapp1.1 to Azure

I have netcoreapp1.1 that I am trying to publish to Azure with WebDeploy. I am able to do dotnet publish and deploy the contents to Azure, but the site just says "You do not have permission to view this directory or page.", which suggests the app is not running.
My previous apps have used net461 as target framework and I have noticed that publishing these (with dotnet publish) have caused a web.config file to be created, which explains how IIS is able to run it, but this does not happen when the target framework is netcoreapp1.1.
The command I have been using to publish is this:
dotnet publish --configuration Release --runtime win7-x64 --output .deploy
Here is my project file:
<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
</PropertyGroup>
<ItemGroup>
<Compile Include="Controllers/*.fs" />
<Compile Include="Startup.fs" />
<Compile Include="Program.fs" />
<None Remove="**/*.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.1.*" />
<PackageReference Include="FSharp.NET.Sdk" Version="1.0.*" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
How am I meant to run a netcoreapp1.1 on Azure? Does it have to be self contained or is it possible to have the Azure App Service run it from dlls?
Edit:
Manually copying a web.config into my publish directory I am able to get the app to run. How can I make dotnet publish create it automatically?
Also, I am still curious about my previous questions.
Add info into you csproj file that you need to copy web.config, like this:
<ItemGroup>
<Content Update="web.config">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

Resources