tags:

views:

37

answers:

1

Hi All,

I Need by service contract to return the xml/json result depending on the request type.I also need a kind of helper function which will convert my result set (i am using linq to sql) so that i do not need to create the xml format for the result set by iterating through the table row many times.What is the suitable way to do that.

I need a kind of short cut method which will convert the table data to xml result.Had i been using asp.net mvc i would have been able to generate the xml data by overriding the the ExecuteResult method in the ActionResult and giving Conetnt-Type = "text/xml" as OP.But since i am using Wcf i don't have the controller context(as controller context is the parameter that needs to be passed to Execute Result).

My present code for converting the table data to the xml format is below.

public XDocument UsersLists(string authToken)
    {
        bool IsAuthenticated = Authenticate(authToken);
        XDocument xDoc = new XDocument();
        XElement root = new XElement("Users");
        if (IsAuthenticated)
        {
            List<User> lstUsers = _lService.UserRepository.GetUserCompanyFromAccountID(GetAccountId(authToken)).ToList();
            if (lstUsers != null)
            {
                root.Add(new XElement("Message", "Success"));
                foreach (var u in lstUsers)
                {
                    XElement chid = new XElement("User");
                    root.Add(new XElement("UserId", u.UserId));
                    root.Add(new XElement("FirstName", u.FirstName));
                    root.Add(new XElement("LastName", u.LastName));
                    root.Add(new XElement("Email", u.Email));
                    root.Add(new XElement("CompanyName", u.Company.CompanyName));
                    root.Add(chid);
                }
                xDoc.Add(root);
                return xDoc;
            }

            else
            {
                return ReturnFailure(xDoc, root);
            }

        }
        else
        {
            return ReturnFailure(xDoc, root);
        }
    }

I need to eliminate this way of generating xml for each table records.

An early response is priceless. Thanks

Technology : Windows Communication Foundation.

A: 

Implementation of single operation returning both XML or JSON differs between WCF 3.5 or WCF 4.0. For implementing this feature in WCF 3.5 check this thread. Generally you will have to create custom behavior and set the content type based on Accept header from the request. WCF 4.0 has native support for this feature. WebHttpBehavior has property AutomaticFormatSelectionEnabled. If you set this property to true it should just work out of the box.

To your second question. You don't need any custom formatting like in ASP.NET MVC. Formatting in WCF is handled by serialization. Just return collection from your operation and you will see what happens. Linq-To-Sql generated entities should be serializable by default. Just don't forget to execute query before returning from method. If you need special format of date which is not the same as Linq-To-Sql entities create custom data type which have public parameterless constructor and all required properties with getter and setter. If you want to make it clear makr that class with DataContract attribute and all properties with DataMember attribute. Return collection of that custom class instances from your operation.

Ladislav Mrnka