Monday, April 05, 2010

Connecting to SSRS Web-Service from Visual Studio 2008 (part II)

In a previous post (Connecting to SSRS Web-Services from Visual Studio 2008), I had written about how to connect to SSRS from Visual Studio 2008. In that one I told you how to add a reference to the SSRS web-service using the “Add Web-Reference” option. That is basically a cop-out and even though an easy way of doing it, I wanted to figure out how to do it using VS2008 and WCF.

So basically I wanted to use a WCF client and connect to a ASMX web-service. (So this will work for any project that needs a similar configuration and needs to use the current user credentials for accessing the WCF client).

First add a reference to the SSRS service using the “Add Service Reference” option.

Now use the following code:

using (ReportingService2005SoapClient proxy = new ReportingService2005SoapClient("ReportingService2005Soap"))
{
    //tell WCF that you need the server to impersonate the current user on the server
    //an important piece to get this working is in the config file - where the security is setup
    //the security needs to be setup like so:
    /*
        <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Ntlm" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
    */

    proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

    Property name = new Property();
    name.Name = "Name";

    Property description = new Property();
    description.Name = "Description";

    Property[] properties = new Property[2];
    properties[0] = name;
    properties[1] = description;

    try
    {
        Property[] returnProperties;
        
        //this method's signature is different from what you get if you were to use
        //Add Web Reference. The return value is now an out parameter.
        proxy.GetProperties(new ItemNamespaceHeader(), 
            "/reportfolder/reportname", properties, out returnProperties);

        foreach (Property p in returnProperties)
        {
            Console.WriteLine(p.Name + ": " + p.Value);
        }
    }

    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }


}

I have added comments regarding the important parts of the code.

Now the last thing you need to do is to update your config file so that your security node (part of the Binding element) looks like this:

<security mode="TransportCredentialOnly">
    <transport clientCredentialType="Ntlm" proxyCredentialType="None"
        realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

No comments: