3

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

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

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 类型的“Collection”。您不能对不属于 Edm 类型“Collection”的参数调用 CreateCollectionWriter。

如何从 ODataClient 成功调用上述操作?

4

2 回答 2

1

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

Simple.OData.Client 的新错误修复版本 4.7.2 已经为我解决了这个问题!

于 2015-06-16T08:31:51.207 回答
0

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

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

        });
   return getRules ;
}
于 2015-06-15T09:42:34.277 回答