16 Oct 2008

SSRS to Web Service

I've recenlty been doing some work with Sql Server Reporting Services talking to a WCF Service. This isn't quite as easy as it sounds. The first thing you need to do is specify a new data source as an XML based source.

And the request to the Service replaces the text in the Dataset.



You then specify a nice request and response as with any message based WCF service:

namespace MyReports
{
[MessageContract(IsWrapped = true)]
public class DataRequest
{
[MessageBodyMember="A"]
private string _fieldA = string.Empty;

[MessageBodyMember="B"]
private string _fieldB = string.Empty;

[MessageBodyMember="C"]
private string _fieldC = string.Empty;
}
}

The response looks something like this:

namespace MyReports
{
[MessageContract]
public class DataReponse
{
[MessageBodyMember="Data"]
private data[] _data;
}
}


And the data object looks like this:

namespace MyReports
{
[DataContract]
public class Data
{
[DataMember(Name="First"]
private string _field1;

[DataMember(Name="Second"]
private string _field2;

[DataMember(Name="Third"]
private string _field3;
}

// Ctor, Getters and setters excluded
}


Voila you're done... Or are you?!?

I found quite a nasty gotcha in doing this. My request class was only being partially populated everytime I did a request. I checked the attributes on the service and classes, I used TCP Trace to make sure the values were actually crossing the wire (which they were), but still the request wasn't being populated properly.

I even asked my pal [insert favourite search engine here] but couldn't fnd anything on it. So I started playing around with the fields in the report and low 2 of the 6 request params were now being populated! What had changed? The ordering. After a few more trial and errors I found the problem. The parameters from the report have to be sent in alphabetic order, otherwise for some reason the request doesn't deserialise properly.

No comments: