【问题标题】:Simple.OData.Client - Unable to invoke Action that accepts entity collection parameterSimple.OData.Client - 无法调用接受实体集合参数的操作
【发布时间】:2015-06-15 08:16:18
【问题描述】:

我收到错误“参数‘wheels’属于 Edm 类型‘Collection’。 您不能对不属于的参数调用 CreateCollectionWriter Edm 类型种类 'Collection'。”

以下是我的设置的详细信息:

Web API 2.2 OData v4 服务:我在我的服务中的 WheelsController 类中定义了 Action,如下所示:

public async Task<IHttpActionResult> UpdateWheels(ODataActionParameters   parameters)
{
object value;
parameters.TryGetValue("carId", out value);
int carId= (int)value;
parameters.TryGetValue("wheels", out value)
IEnumerable<Wheel> wheels = (IEnumerable<Wheel>)value;
// logic goes here....
return OK();
}

在 WebApiConfig.cs 文件中,Action 配置定义如下:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Car>("Cars");
builder.EntitySet<Wheel>("Wheels");
var action = builder.EntityType<Wheel>().Collection.Action("UpdateWheels");
action.Parameter<int>("carId");
action.CollectionParameter<Wheel>("wheels");

我成功地从 FireFox 浏览器中的 RESTClient 扩展调用上述操作作为对 URL“http://localhost/Service/Wheels/UpdateWheels”的 POST 请求,请求正文为

{"carId":2,
"wheels":[{"Id":1,"Name":"Wheel Front 1","Description":"Front wheel left",   "PositionEnum":"FrontLeft"},
{"Id":2,"Name":"Wheel Front 2","Description":"Front wheel right",   "PositionEnum":"FrontRight"}]
}

但是,当我尝试在客户端应用程序(例如

)中使用 Simple.OData.Client 调用上述服务操作时,它会给出 错误
public async void TestUpdateWheels(List<Wheel> wheelList)
{
// client is derived from ODataClient from assembly Simple.OData.Client.Core.dll, v4.3.0.0
await client.For<Wheel>()
.Action("UpdateWheels")
.Set(new { carId = 2, wheels = wheelList})
.ExecuteAsync();
}

错误信息:参数 'wheels' 属于 Edm 类型 '收藏'。您不能在参数上调用 CreateCollectionWriter 这不是 Edm 类型的“集合”。

如何从 ODataClient 成功调用上述 Action?

【问题讨论】:

    标签: odata asp.net-web-api2 simple.odata


    【解决方案1】:

    当我向项目站点报告时,这原来是 Simple.OData.Client 版本 4.3.0 中的一个错误。详情请访问链接https://github.com/object/Simple.OData.Client/issues/117

    Simple.OData.Client 新的错误修复版本 4.7.2 修复了 给我的问题!

    【讨论】:

      【解决方案2】:

      以这种方式尝试。它适用于我的一个项目。

      public async Task<string> TestUpdateWheels(List<Wheel> wheelList)
      {
          string getRules = await client.ExecuteActionAsScalarAsync<string>
           ("UpdateWheels", new Dictionary<string, object>
              {
                 { "YourParamater", wheelList}
      
              });
         return getRules ;
      }
      

      【讨论】:

      • 感谢发帖!我已经尝试过你提到的。但是,这也会失败并显示相同的错误消息。
      • 您需要为服务操作添加属性 [HttpPost] [ODataRoute("TestUpdateWheels")] [ActionName("TestUpdateWheels")] public IHttpActionResult TestUpdateWheels(){}
      • 正如我所写的,我可以从 Browser RESTClient 调用 Action,但不能从 Simple.OData.Client 应用程序调用。我已经将 [HttpPOST] 属性应用于 Action 方法。由于是绑定动作,所以不需要应用路由属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 2021-02-12
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      相关资源
      最近更新 更多