Newtonsoft.Json参考抱怨Azure函数 [英] Newtonsoft.Json reference complaining on Azure Functions

查看:58
本文介绍了Newtonsoft.Json参考抱怨Azure函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个称为SmsWebhook的Azure功能.它调用外部程序集中的方法AzureFunctionsSample.Services.dll,该方法引用了Newtonsoft.Json 8.0.3

I'm running an Azure Functions, called SmsWebhook. It calls a method in an external assembly, AzureFunctionsSample.Services.dll that has a reference to Newtonsoft.Json 8.0.3

我的Run.csx的详细信息如下:

#r "AzureFunctionsSample.Services.dll"
using System.Net;
using AzureFunctionsSample.Services

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    ...
}

在上面的Run()方法中,我创建一个实例并在该实例中调用一个方法.但是,每当我调用该方法时,都会出现以下错误:

Within the Run() method above, I create an instance and call a method in the instance. However, whenever I call that method, I receive the following error:

2016-05-19T13:41:45  Welcome, you are now connected to log-streaming service.
2016-05-19T13:41:46.878 Function started (Id=64fccf0c-d0ef-45ef-ac1c-7736adc94566)
2016-05-19T13:41:46.878 C# HTTP trigger function processed a request. RequestUri=https://ase-dev-fn-demo.azurewebsites.net/api/smswebhook
2016-05-19T13:41:46.878 Function completed (Failure, Id=64fccf0c-d0ef-45ef-ac1c-7736adc94566)
2016-05-19T13:41:46.894 Exception while executing function: Functions.SmsWebhook. Microsoft.Azure.WebJobs.Script: One or more errors occurred. AzureFunctionsSample.Services: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

我在bin目录下手动添加了相同版本的Newtonsoft.Json.dll,但是仍然出现相同的错误.为什么在Newtonsoft.Json.dll文件中抱怨?

I manually added the same version of Newtonsoft.Json.dll under the bin directory, but still got the same error. Why is it complaining at the Newtonsoft.Json.dll file?

如果我将外部程序集中的所有逻辑移到Run.csx中,顺便说一句,它不会抱怨.

If I move all the logics within the external assembly into the Run.csx, it won't complain, by the way.

推荐答案

@JustInChronicles,我在此添加它作为参考,但是预期的行为应该是从您的文件夹,如预期.

@JustInChronicles, I'm adding this here as an answer for reference, but the expected behavior should be that indirect dependencies of private assemblies are resolved from your bin folder, as expected.

我汇总了以下测试以重现您的情况:

I put together the following test to reproduce your scenario:

  • 使用简单类型创建一个简单的类库,该类使用Json.NET序列化对象并返回JSON字符串.该程序集引用了Json.NET 8.0.3.结果包括它正在使用的Json.NET程序集版本
  • 创建了一个函数,该函数引用带有#r "DependencyWithJsonRef.dll" only 类型并返回由上述方法产生的结果
  • DependencyWithJsonRef.dllNewtonsoft.Json.dll(8.0.3)部署到函数的bin文件夹中
  • Created a simple class library with a simple type that uses Json.NET to serialize an object and return the JSON string. This assembly references Json.NET 8.0.3. The result includes the Json.NET assembly version it is using
  • Created a function that references that type only with a #r "DependencyWithJsonRef.dll" and returns the result produced by the method mentioned above
  • Deployed DependencyWithJsonRef.dll and Newtonsoft.Json.dll (8.0.3) to my function's bin folder

调用该函数会产生预期的结果.

Invoking the function produces the expected result.

以下是函数,供参考:

#r "DependencyWithJsonRef.dll"

using System.Net;

public static string Run(HttpRequestMessage req, TraceWriter log)
{
    var myType = new DependencyWithJsonRef.TestType();
    return myType.GetFromJson();
}

如您所见,不需要显式引用间接依赖项(Json.NET).

As you can see, no explicit reference to indirect dependencies (Json.NET) required.

这是我得到的输出:

{
    "Prop1":"Test",
    "Prop2":1,
    "AssemblyName": "Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"
}

快速笔记:您可能需要检查的一件事,特别是如果在开发函数时更新了该依赖项,则是未缓存程序集结果.确保您从头开始的一种可靠方法是(在部署功能和程序集之后)进入Kudu并终止 non-scm w3wp进程,以查看是否有帮助.我很想知道这是否能解决问题,因为如果有的话,我们可以做些改进.

Quick note: One thing you may want to check, particularly if you've updated that dependency while developing your function is that assembly resultion results were not cached. A sure way to make sure you're starting with a clean slate is to (after you deploy your function and assemblies) go to Kudu and kill the non-scm w3wp process to see if that helps. I'd be curious to know if that does the trick as there are a few things we can to to improve this if it does.

这篇关于Newtonsoft.Json参考抱怨Azure函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆