Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

huangapple go评论88阅读模式
英文:

Azure function: Could not load file or assembly 'System.Net.Http, Version=7.0.0.0'

问题

我有一个Azure函数,我想使用HttpClient执行对特定API端点的POST请求。
我运行项目时没有错误,但当函数启动时,它抛出此错误。我应该怎么解决它?我搜索了很多内容,但没有找到有用或清晰解释的信息。

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

函数:

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!"); }
        }
    }
}

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

框架包含System.http.net,它位于哪个目录?我如何尝试降低版本以测试是否是问题?我可以删除完整的框架并自己安装所有包吗?(有意义吗?)

英文:

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.

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

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!"); }
        }
    }
}

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

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?)

答案1

得分: 1

我已使用以下版本的软件包,对我来说工作正常:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

.csproj 文件:

<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>

我如何尝试降低版本以测试是否是问题?

您可以按照以下方式管理 NuGet 包:

首先右键单击您的函数应用名称:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

然后右键单击并点击“管理 NuGet 包”:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

在搜索框中键入软件包名称,然后单击软件包名称并选择您想要的版本,然后进行更新。

如果这不起作用,那么请启动一个新的函数应用并安装我的软件包,它将正常工作,因为我也得到了输出。

输出:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

英文:

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

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

.csproj file:


    &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
      &lt;PropertyGroup&gt;
        &lt;TargetFramework&gt;net6.0&lt;/TargetFramework&gt;
        &lt;AzureFunctionsVersion&gt;v4&lt;/AzureFunctionsVersion&gt;
      &lt;/PropertyGroup&gt;
      &lt;ItemGroup&gt;
        &lt;PackageReference Include=&quot;Microsoft.NET.Sdk.Functions&quot; Version=&quot;4.1.1&quot; /&gt;
      &lt;/ItemGroup&gt;
      &lt;ItemGroup&gt;
        &lt;None Update=&quot;host.json&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
        &lt;/None&gt;
        &lt;None Update=&quot;local.settings.json&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
          &lt;CopyToPublishDirectory&gt;Never&lt;/CopyToPublishDirectory&gt;
        &lt;/None&gt;
      &lt;/ItemGroup&gt;
    &lt;/Project&gt;

>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 :

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

Then right click and click on Manage Nuget Packages:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

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:

Azure函数:无法加载文件或程序集 ‘System.Net.Http,版本=7.0.0.0’

huangapple
  • 本文由 发表于 2023年3月7日 01:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定