Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi All,

I am playing around with RESTful web servcies in WCF. I am using the online template in visual studio called 'WCF REST Service Template 40(CS)'.

Anyway, all is good and my service calls return appropriate XML data (which is what I am looking to return)

At first, I quite liked how easy it was to return a class and it would automatically be translated into XML. A simple example would be return a string with a value representing an authentication token, the XML returned would be:

<string>6fa1b98d-7bf9-43a7-ad26-8c3ae382a198</string>


...easy as pie! But now, I am deciding what I really want to return is something like...

<token>6fa1b98d-7bf9-43a7-ad26-8c3ae382a198</token>


...So my ultimate question is, what is the best way to make this happen?

I though it would probably work if I had a custom class called 'token' and I override whatever function handles the 'to xml' part of it - but this would be overkill and just doesn't seem right

What I would really like to be able to do, is to include a 'View' as would be possible using the ASP.Net MVC framework. So I could define my XML format in a View as easy as (in MVC 3)...

<token>
    @ViewBag.Token
</token>


...is this possible in WCF? Perhaps I should use MVC 3 to do the RESTful service instead of WCF? Any thoughts?

Thanks
Posted

1 solution

Well I have been looking into this and what I have found so far is as follows.

In order to return XML formatted exactly how I wanted it, I found this post[^] which shows how to convert an XMLDocument into a Message. Then with a further helper class my calling code can now be as simple as this...

[WebGet(UriTemplate = "Test")]
public Message Test()
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.LoadXml("<token>1234</token>");

    return CustomMessage.XML(doc);
}


...which produces the XML result I was looking for. I was also able to produce an MVC style view parser using a bit of Regex and Reflection to parse for variables in the XML file and substitute them for matching parameters in a given class instance...

public static Message XML(string view, object model)
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(System.Web.Hosting.HostingEnvironment.MapPath("~/Views/" + view + ".xml"));

    return new CustomResponses().CreateResponseMessage(doc, model);
}


public System.Xml.XmlDocument MergeDoc(System.Xml.XmlDocument doc, object model)
{
    string xml = doc.OuterXml;
    System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties();
    foreach (Match match in Regex.Matches(xml, "@ViewBag.[a-zA-Z0-9]{1,}", RegexOptions.None))
    {
         string propertyName = match.Value.Replace("@ViewBag.", "");
         System.Reflection.PropertyInfo property = model.GetType().GetProperty(propertyName);
         xml = xml.Replace(match.Value, property.GetValue(model, null).ToString());
    }
    doc.LoadXml(xml);
    return doc;
}


These two above functions allow for an xml file containing:

<token>@ViewBag.Token</token>


to be merged with a class instance and used like:

public class TokenClass
{
    public string Token { get; set; }
}

[WebGet(UriTemplate = "Test2")]
public Message Test2()
{
    TokenClass token = new TokenClass() { Token = "123456" };

    return CustomMessage.XML("Token", token);//params here are View and Model
}


the returned XML response would be...

<token>123456</token>


...this is not 100% ideal as you do not have the extra functionality of parsing code within the xml file (such as a for loop) that would be possible with the Razor view engine used in MVC 3 (there is a post here[^] that shows how the Razor engine can be used outside of MVC 3 that may interest some but I am not sure it is what I want so I may chose to leave it out)

Anyway, I will probably continue to investigate further possibilities but hopefully this information will be of use to someone else. Also, if anyone reading this has any further comments, I would be interested in hearing them

EDIT:

Here[^] is a nice little article on how to use the Razor view engine without MVC and I can say that it works pretty well
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900