Kunal
Kunal

Reputation: 1943

How to customize XmlResponse in WCF service using WebHttpBinding

I need to intercept the response of a service created using WCF with webHttpBinding (REST based service).

Now I want the XML emitted in a customized way. For e.g, currently a service method returns output like this:

<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <UserCellInfo>+91-98102239</UserCellInfo>
   <UserDepartment>.NET Projects</UserDepartment>
   <UserID>A10129</UserID>
   <UserName>Jeff Thomson</UserName>
</User>

but I want it in a different custom way.Something like below

<?xml version="1.0" encoding="UTF-8"?>
<rsp>
   <User>
      <UserCellInfo>+91-98102239</UserCellInfo>
      <UserDepartment>.NET Projects</UserDepartment>
      <UserId>A10129</UserId>
      <UserName>Jeff Thomson</UserName>
   </User>
</rsp>

In MSDN, I read that I need to use IDispatchMessageInspector to intercept the XML that goes out. But I couldn't find any code reference or example for it.

Any good ideas how to achieve this?

Upvotes: 0

Views: 419

Answers (1)

marc_s
marc_s

Reputation: 754200

So I assume your WCF service method returns an instance of a User object in this method call, right?

Why not just define a wrapper class called rsp that contains the User instance? Then returning the rsp object would render in the desired style. Just a nice clean wrapper - no messy XML manipulation on the fly......

Upvotes: 1

Related Questions