You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.NET OData Client访问Dell Redfish API时遇错误

问题

使用ODataClient库连接Dell iDRAC的Redfish接口时遇到以下问题:

  • 触发错误:

    MetadataDocumentCannotHaveMoreThanOneEntityContainer : Metadata document cannot have more than one entity container.

  • 访问以下路径时抛出XmlError,提示Data at the root level is invalid:
    • /redfish/v1/odata
    • /redfish/v1/$metadata

服务根URL为https://myserver.com/redfish/v1,相关代码示例如下:

public sealed class RedFishContext : DataServiceContext
{
    public RedFishContext(IHttpClientFactory httpClientFactory, Uri serviceRoot) : base(serviceRoot)
    {
         HttpClientFactory = httpClientFactory;
         Session = CreateQuery<Session>("Session");
         Format.UseJson();
    }

    public DataServiceQuery<Session> Session { get; }


    public void AddSession(Session session)
    {
        AddObject("Session", session);
    }
}
解决方案

问题根源

Redfish接口仅部分兼容OData规范,和标准OData存在两处关键差异:

  1. Redfish元文档允许包含多个实体容器,而ODataClient要求元数据只能有一个实体容器,导致第一个错误。
  2. Redfish默认返回JSON格式元数据,但ODataClient默认按XML格式解析,引发XmlError。

修复步骤

  1. 强制请求JSON格式元数据
    在上下文构造函数中,除了设置Format.UseJson(),还要给所有请求添加Accept: application/json头,确保元数据以JSON返回:

    public RedFishContext(IHttpClientFactory httpClientFactory, Uri serviceRoot) : base(serviceRoot)
    {
         HttpClientFactory = httpClientFactory;
         Session = CreateQuery<Session>("Session");
         Format.UseJson();
         // 配置请求头,强制接收JSON
         Configurations.RequestPipeline.OnMessageCreating(args =>
         {
             args.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
             return args.RequestMessage;
         });
    }
    
  2. 预处理元数据解决多容器问题
    ODataClient无法直接处理多实体容器,需要手动获取并修改元数据:

    • 单独发送请求获取/redfish/v1/$metadata的JSON内容(确保请求头为application/json)
    • 对元数据进行预处理,合并或只保留需要的实体容器
    • 将修改后的元数据提供给ODataClient使用,替代自动加载的元数据
  3. 跳过自动元数据加载,手动定义实体
    放弃ODataClient的自动元数据解析,直接参考Redfish文档定义实体类:

    • 按照Redfish规范编写Session等实体的属性和结构
    • 在上下文类中直接使用AddObject和Execute方法执行操作,不依赖CreateQuery自动生成逻辑

额外建议

优先使用Redfish专用SDK访问iDRAC接口,这类工具已适配Redfish的特殊规范,能避免OData兼容性问题。

内容的提问来源于stack exchange,提问作者Marko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:43:27