Printing SSRS Reports from a Worker Service Project: A Step-by-Step Guide
Image by Rubens - hkhazo.biz.id

Printing SSRS Reports from a Worker Service Project: A Step-by-Step Guide

Posted on

Are you tired of manual report printing from your SSRS (SQL Server Reporting Services) reports? Do you want to automate the printing process using a worker service project? Look no further! In this article, we’ll take you on a journey to explore the world of automated report printing using SSRS and .NET Core Worker Services.

What You’ll Need

To get started, make sure you have the following requirements:

  • .NET Core 3.1 or higher installed on your machine
  • Visual Studio 2019 or higher (optional)
  • SSRS reports setup and configured
  • A basic understanding of C# and .NET Core

Step 1: Create a New Worker Service Project

Let’s begin by creating a new worker service project in Visual Studio:


dotnet new worker

This will create a basic worker service project with the necessary dependencies. You can also create a new project in Visual Studio by selecting the “Worker Service” template under the “.NET Core” section.

Step 2: Add the Required NuGet Packages

In your worker service project, you’ll need to add the following NuGet packages:


dotnet add package Microsoft.ReportingServices.Reader
dotnet add package Microsoft.ReportingServices.Interfaces
dotnet add package System.Drawing.Common

These packages will provide the necessary functionality for interacting with SSRS reports and printing them.

Why Do We Need These Packages?

The Microsoft.ReportingServices.Reader package allows us to read and manipulate SSRS reports, while the Microsoft.ReportingServices.Interfaces package provides the necessary interfaces for interacting with the SSRS report server. The System.Drawing.Common package is required for rendering report elements.

Step 3: Configure the SSRS Report Connection

In your worker service project, create a new class to hold the SSRS report connection settings:


public class ReportConfig
{
    public string ReportServerUrl { get; set; }
    public string ReportPath { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

In the Program.cs file, create an instance of the ReportConfig class and configure it with your SSRS report server settings:


var reportConfig = new ReportConfig
{
    ReportServerUrl = "https://your-ssrs-report-server.com/ReportServer/",
    ReportPath = "/Path/To/Your/Report",
    Username = "your-username",
    Password = "your-password"
};

Step 4: Retrieve the SSRS Report Definition

Using the ReportConfig instance, create a new method to retrieve the SSRS report definition:


private ReportDefinition GetReportDefinition(ReportConfig config)
{
    var reportService = new ReportService2010();
    reportService.Credentials = new NetworkCredential(config.Username, config.Password);
    reportService.Url = config.ReportServerUrl;

    var reportDefinition = reportService.GetReportDefinition(config.ReportPath);
    return reportDefinition;
}

This method uses the ReportService2010 class to connect to the SSRS report server and retrieve the report definition.

Step 5: Print the SSRS Report

Now that we have the report definition, we can print the report using the following method:


private void PrintReport(ReportDefinition reportDefinition)
{
    var reportRenderer = new ReportRenderer(reportDefinition);
    var printDoc = new PrintDocument(reportDefinition.Name);

    reportRenderer.RenderReport("PDF", null, null, null, null, null, null, null, printDoc);

    printDoc.Print();
}

This method uses the ReportRenderer class to render the report in PDF format and then prints it using the PrintDocument class.

Step 6: Implement the Worker Service

In the Worker.cs file, implement the worker service to print the SSRS report:


public class Worker : BackgroundService
{
    private readonly ReportConfig _config;

    public Worker(ReportConfig config)
    {
        _config = config;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var reportDefinition = GetReportDefinition(_config);
            PrintReport(reportDefinition);

            await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
        }
    }
}

This worker service will continuously print the SSRS report at a 1-minute interval. You can adjust the delay time as needed.

Step 7: Run the Worker Service

Finally, run the worker service:


dotnet run

The worker service will start printing the SSRS report at the specified interval.

Tips and Variations

Scheduling the Worker Service

You can schedule the worker service to run at specific times or intervals using a scheduler like Quartz.NET or Hangfire.

Multiple Report Printing

If you need to print multiple reports, create a list of report configurations and iterate through them in the worker service.

Error Handling

Implement error handling and logging mechanisms to handle any exceptions that may occur during the printing process.

Conclusion

Printing SSRS reports from a worker service project is a straightforward process that can save you a lot of time and effort. By following these steps, you can automate the printing process and focus on more important tasks. Remember to configure your SSRS report server correctly and adjust the printing settings as needed.

Step Description
1 Create a new worker service project
2 Add the required NuGet packages
3 Configure the SSRS report connection
4 Rertieve the SSRS report definition
5 Print the SSRS report
6 Implement the worker service
7 Run the worker service

By following this guide, you should now have a solid understanding of how to print SSRS reports from a worker service project. Happy printing!

Keywords: Printing SSRS reports, Worker service project, .NET Core, C#, SSRS report server, ReportDefinition, ReportRenderer, PrintDocument.

Frequently Asked Questions

Get ready to unravel the mysteries of printing SSRS reports from a Worker service project with our expert-approved FAQs!

Can I directly print SSRS reports from a Worker service project?

No, you cannot directly print SSRS reports from a Worker service project because Worker services do not have a user interface and are not designed to interact with the printer. However, you can use a workaround by rendering the report to a file (e.g., PDF) and then printing the file using a separate process or service.

How do I render an SSRS report to a file in a Worker service project?

You can use the `ReportExecutionService` class to render an SSRS report to a file. This class provides methods to execute reports and save them to a file in various formats, such as PDF, Excel, or Word. You’ll need to add the required NuGet packages and configure the report execution settings to render the report to a file.

Can I use the `LocalReport` class to render an SSRS report to a file in a Worker service project?

Yes, you can use the `LocalReport` class to render an SSRS report to a file in a Worker service project. This class allows you to load a report definition file (.rdlc) and render the report to a file. However, make sure to set the `EnableExternalImages` property to `true` and provide a valid `TempImages` folder path to ensure that the report renders correctly.

How do I print the rendered report file from a Worker service project?

Once you’ve rendered the report to a file, you can use a separate process or service to print the file. For example, you can use a Windows Service or a console application to print the file using the `System.Drawing.Printing` namespace or a third-party printing library. You can also use a cloud-based printing service or a scheduled task to automate the printing process.

Are there any security considerations when printing SSRS reports from a Worker service project?

Yes, there are security considerations when printing SSRS reports from a Worker service project. Make sure to handle sensitive data and credentials securely, and consider encrypting the report files or using secure storage. Additionally, ensure that the Worker service has the necessary permissions and access to the printer and report files. Finally, follow best practices for secure coding and testing to prevent potential vulnerabilities.

Leave a Reply

Your email address will not be published. Required fields are marked *