英文:
Azure function: Could not load file or assembly 'System.Net.Http, Version=7.0.0.0'
问题
我有一个Azure函数,我想使用HttpClient执行对特定API端点的POST请求。
我运行项目时没有错误,但当函数启动时,它抛出此错误。我应该怎么解决它?我搜索了很多内容,但没有找到有用或清晰解释的信息。
函数:
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!"); }
}
}
}
框架包含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.
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!"); }
}
}
}
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
我已使用以下版本的软件包,对我来说工作正常:
.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 包:
首先右键单击您的函数应用名称:
然后右键单击并点击“管理 NuGet 包”:
在搜索框中键入软件包名称,然后单击软件包名称并选择您想要的版本,然后进行更新。
如果这不起作用,那么请启动一个新的函数应用并安装我的软件包,它将正常工作,因为我也得到了输出。
输出:
英文:
I have used below version of packages and its working fine for me:
.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 :
Then right click and click on Manage Nuget Packages
:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论