1

i have an azure function and i wanto to do a post with HttpClient to an endpoint of a certain API. I run the project without error but when the function start it throw this error. What i have to do to solve it? I searched and found a lot but nothing usefull or cleary explained.

enter image description here

Function :

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using Tesi.Worker;

[assembly: WebJobsStartup(typeof(Startup))]

namespace Tesi.Worker
{
    public class StandingsFunctions
    {
        private static HttpClient _client = new HttpClient();

        public StandingsFunctions()
        {
        }

        [FunctionName("WeeklyUpdateStandings")]
        public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            HttpContent content = null; 
            var response =  _client.PostAsync("https://sportdataapi.azurewebsites.net/api/WebHook/UpdateStandings",content);

            if (response.IsCompletedSuccessfully) { log.LogInformation("Post andata a buon fine " + response.Status.ToString()); }
            else { log.LogInformation("Errore!"); }
        }
    }
}

enter image description here

framework contains System.http.net, where it's located in directory? How can i try to decrease the version to test if that is the problem?Can i delete the full framework and install all package by my self? (have sense?)

  • https://stackoverflow.com/questions/51276617/httpclient-best-practices-in-azure-functions. Hope this helps – amit agarwal Mar 06 '23 at 18:52
  • Sounds like you're using an inprocess function app. It tries to remove redundant dll's, but does so a bit over aggressively causing these kinds of errors. In your `.csproj` try adding `<_FunctionsSkipCleanOutput>true` in the `` and see if it works then. – NotFound Mar 07 '23 at 08:51

1 Answers1

1

I have used below version of packages and its working fine for me:

enter image description here

.csproj file:


    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>

How can i try to decrease the version to test if that is the problem?

You can find manage nuget packages as below:

Firstly right click on Your Function App name :

enter image description here

Then right click and click on Manage Nuget Packages:

enter image description here

Type in Search Box, name of the package and then click on the Package name and select the version you want and then Update.

If this doesn't work then start a new Function App and install my Packages and it will work fine as i got Output too.

Output:

enter image description here

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • Example [Reference](https://stackoverflow.com/questions/74470489/system-io-filenotfoundexception-could-not-load-file-or-assembly-microsoft-exte/74473381#74473381) – RithwikBojja Mar 07 '23 at 05:19
  • 1
    Thanks, was the correct idea. Probably in one of my try to solve the problem i updated .Net 6 to 7. For that, some new features cause conflict or are changed from the version that i know. So, i downgrade to version 6 and downgrade the SDK as you said and it works. – Matteo Pagliarello Mar 07 '23 at 08:53
  • Glad it solved the problem. – RithwikBojja Mar 07 '23 at 09:04