Nirman
Nirman

Reputation: 6783

How to return XmlDocument as a response from a ServiceStack API

We are having a few ServiceStack APIs which are being called from an external tool. This tool expects the input of "XmlDocument" type, and there is no provision to write code to convert a string to XmlDocument in that tool.

The problem is that earlier we had SOAP services which were returning XmlDocument and the tool was working well. However, recently we converted all those services to ServiceStack and since then we are not able to get a proper XmlDocument response. It always returns empty nodes. I believe this is not straight-forward and something in between is missing.

Following is my response object.

public class RequisitionImportResponse : ResultResponse
{
    public XmlDocument ResponseXML { get; set; }
}

Currently I am getting empty nodes in ResponseXML, where I would like to receive proper XmlDocument which it was used to return when the service was in SOAP.

Could anyone please assist here?

Thanks

Upvotes: 2

Views: 182

Answers (1)

mythz
mythz

Reputation: 143319

You can return a string in your Service, e.g:

public class RequisitionImport : IReturn<string> { ... }

public class XmlServices : Service
{
    [AddHeader(ContentType=MimeTypes.Xml)]
    public object Any(RequisitionImport request)
    {
        XmlDocument xmlDoc = ...;
        return xmlDoc.OuterXml;
    }
}

Or you can allow Services to return an XmlDocument:

public class XmlServices : Service
{
    public object Any(RequisitionImport request)
    {
        XmlDocument xmlDoc = ...;
        return xmlDoc;
    }
}

If you configure a Response Filter to check for XmlDocument and write it to the response, e.g:

GlobalResponseFiltersAsync.Add(async (req, res, dto) => {
    if (dto is XmlDocument doc) {
        res.ContentType = MimeTypes.Xml;
        await res.WriteAsync(doc.OuterXml);
        res.EndRequest();
    }
});

Upvotes: 3

Related Questions