首页 > 解决方案 > 'System.Net.Http.Formatting.IContentNegotiator' 尚未注册

问题描述

我通常是 Java 开发人员。今天,我决定学习如何使用 Microsoft 的工具在 Azure 中构建无服务器 Web 应用程序。

由于我是 Mac 用户,我安装了 Mac 版本的 Visual Studio。

接下来,我创建了一个新的 Functions 解决方案并选择了名为“Http trigger with parameters”的模板。

然后我按下“运行”按钮,看到它打开了一个带有 URL 的控制台窗口,我可以使用它来访问该服务。

但是当我从另一个窗口用 curl 访问那个 URL 时,我得到了这个:

System.Private.CoreLib:执行函数时出现异常:Demo。Autofac:请求的服务“System.Net.Http.Formatting.IContentNegotiator”尚未注册。为避免此异常,请注册组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional() 方法解决可选依赖项。

一般来说,作为 Microsoft 工具的新手,我不知道下一步该做什么。我也有点惊讶微软会发布一个工具,在该工具中运行没有任何更改的默认模板将不起作用。

坦率

标签: visual-studioazure

解决方案


我相信具体问题是在请求对象上调用扩展方法 CreateResponse() 。在默认模板上,您应该在 run() 方法的末尾看到类似的内容;

return req.CreateResponse(HttpStatusCode.OK);

解决方法是显式实例化和填充 HttpResponseMessage 对象。

在正常的 CRUD 插入操作之后,这样的东西对我有用;

// Save the entity and return a reference 
var blobReference = await SaveBlob(blob);
// Create an HttpContent object to package the reference
var blobRefContent = new ObjectContent<BlobReference>(blobReference, (MediaTypeFormatter)MediaTypeFormatter.GetDefaultValueForType(typeof(BlobReference)));
// Create a response containing the reference and the original request, as well as a successful return code
var response = new HttpResponseMessage(HttpStatusCode.Created) { Content = blobRefContent, RequestMessage = req };
// Return the response
return response; 

推荐阅读