简体   繁体   English

WCF Rest服务接受动态作为参数

[英]WCF rest service to accept dynamic as parameter

In my application, I am sending a json object to a service and at the service end, I expect an object of type dynamic 在我的应用程序中,我正在向服务发送一个json对象,在服务端,我期望一个动态类型的对象

public bool TestService(dynamic entity)
{
    // process
}

When i debug and see the entity filled, I am not able to type cast it. 当我调试并看到实体已填充时,无法键入强制转换。 Any idea how i can retrieve fields from the entity sent 任何想法我怎么能从发送的实体中检索字段

I'm curious - if you're sending up a JSON formatted object, why not have your service method accept a string and then use something like JSON.net to cast it to the appropriate type? 我很好奇-如果您要发送JSON格式的对象,为什么不让您的服务方法接受字符串,然后使用JSON.net之类的内容将其转换为适当的类型?

public bool TestService(string entity)
{
    var myObject = JsonConvert.DeserializeObject<MyObjectType>(entity);
    //do stuff with myObject...
}

Or you could deserialize it into an anonymous object: 或者,您可以将其反序列化为匿名对象:

public bool TestService(string entity)
{
    var myAnonymousObject = new { Name = String.Empty, Address = String.Empty };
    var myObject = JsonConvert.DeserializeAnonymousType(entity, myAnonymousObject);
    //do stuff with myObject
}

I guess I'm not sure why your JSON formatted object needs to be dynamic. 我想我不确定为什么您的JSON格式的对象需要动态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM