开发者

WCF and Anonymous Types

I want to return an anonymous type over WCF. Is this poss开发者_C百科ible?


You cannot use anonymous types, but maybe you are talking about WCF and untyped messages?

There is an option in WCF to just define a parameter of type Message (and possibly a return value of the same type). This is just the raw message that goes to WCF (and comes back from it).

I can't find much good information out there - there's some documentation on MSDN, but the best I've seen so far is Kurt Claeys' blog post WCF : Untyped messages on WCF operations.

I would not recommend using this approach - it's a lot more grunt work to handle the message contents directly yourself and that's what WCF is trying to spare us from - but if you absolutely, positively have to tweak every bit of your message - this seems like the way to go.

Marc


You can't return an anonymous type from any method, can you? So why would you be able to return it from WCF?


Looks like you cannot do so with the XML Serializer because of some complaint about a parameterless constructor but it works for the json serializer if you are serving to an ajax client as indicated by Dave Ward.


OK, I understand. But then if I define a type - MyObj - for this purpose and mark its members IsRequired=false, how can I create+send across an instance of MyObj with only some of its members? Is this possible??

Take a look at [DataMember(EmitDefaultValue=false)]


No, it is not. You'll have to define your types ahead of time.


You definitely can return anonymous types. This works, for example:

public object GetLatestPost()
{
  XDocument feedXML = XDocument.Load("http://feeds.encosia.com/Encosia");

  var posts = from feed in feedXML.Descendants("item")
                   select new
                   {
                     Title = feed.Element("title").Value,
                     Link = feed.Element("link").Value,
                     Description = feed.Element("description").Value
                   };

  return posts.First();
}

If you call that method as an ASMX ScriptService's WebMethod, you'll get this JSON from it:

{"d":
    {"Title":"Using an iPhone with the Visual Studio development server",
     "Link":"http://feeds.encosia.com/~r/Encosia/~3/vQoxmC6lOYk/",
     "Description":" Developing iPhone-optimized portions of an ASP.NET ..."}}

You can use a return type of IEnumerable to return a collection of anonymous types also.


You can use the ExpandoObject. When you define a property in a DTO as ExpandoObject the client is generated as Dictionary:

Contract DTO

public class TaskDTO
{
    public string Type { get; set; }
    public ExpandoObject Args { get; set; }
    public string Id { get; set; }
    public TaskDTO SuccessTask { get; set; }
    public TaskDTO FailTask { get; set; }
    public bool IsFinal { get; set; }
}

Client

using (var client = new JobServiceClient())
{
    var task = new TaskDTO
    {
        Id = Guid.NewGuid().ToString(),
        Type = "SendEmailTask",
        IsFinal = true
    };
    dynamic args = new ExpandoObject();
    args.To = "who@mail.com";
    args.Title = "test job service";
    args.Content = "test job service";
    task.Args = ((IDictionary<string, object>)args).ToDictionary(i => i.Key, i => i.Value);
    client.Execute(task);
}

Service

dynamic args = dto.Args;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜