Introduction
It is a bit difficult to find an error when using a custom assembly which connects to the database and fetches the values especially when it is running on the client machine. The best solution is to write error messages in
a text file. This article shows how can we write errors in a text file using a custom assembly in
Reporting Services.
To use a text file from a custom assembly in Reporting Services we will have to give permission to the file in the custom assembly and then we will be able to write errors in this file. Side by side we will have to add permissions in the
assemblyinfo file to make the custom assembly DLL accessible from Report
Server. Also we will have to add permissions for the DLL in the rssrvpolicy.config file.
I have used Visual Studio to develop a class library (custom assembly) using
VB.NET.
Using the code
Steps for Assemblyinfo.vb
- Add <Assembly: AllowPartiallyTrustedCallers()> in
assemblyinfo.vb file.
- Import "Imports System.Security".
Steps for Class file in Class Library (custom assembly)
- Import "Imports System.IO".
- Import "Imports System.Security.Permissions".
- Add the following lines in your method to give permission to your text file:
Dim frp As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.AllAccess, _
"You Text File path will come here + filename.extension")
frp.Assert()
- To write error text in this file, use the following code:
Try
................. Your code here.............
Catch ex As Exception
Dim sw1 As StreamWriter = New StreamWriter(getPath("FestivalBSLogFile.txt"), True)
sw1.WriteLine(DateTime.Now.ToShortDateString & " " & _
DateTime.Now.ToLongTimeString & ": " & ex.Message)
sw1.Flush()
sw1.Close()
End Try
- You can modify the code according to your requirements. This is a very simple example just to make sure it is working.
Steps for Deployment on Report Server
- Copy custom assembly (Class library) DLL in the C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\ReportServer\bin\reportHelperLib.dll folder.
This is a default folder for Reporting Services. The path can be different on your machine so please check it.
- Open the file rssrvpolicy.config, the default path is C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\ReportServer (it can be different on your
machine so please check).
- Add the following codegroup in this file:
<CodeGroup class="UnionCodeGroup" version="1"
PermissionSetName="FullTrust" Name="reportHelperLibSample"
Description="reportHelperLib. ">
<IMembershipCondition class="UrlMembershipCondition" version="1"
Url="C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\ReportServer\bin\reportHelperLib.dll"
/>
</CodeGroup>
- You need to change three things here: Name, Description, and URL.
- Name is your DLL custom assembly name.
- Description is your DLL description.
- URL is your DLL custom assembly path. By default, it is C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\ReportServer\bin\reportHelperLib.dll.
- Codegroup should be added at the end of the
codegroups
section just before ending
codegroup
tags.
..... here.....
</CodeGroup>
</CodeGroup>
</PolicyLevel>
</policy>
- Restart report server from Control Panel>Administrator>Services>Report
Server.
- If restarting is not working then restart your computer.
Points of Interest
Please do not forget to write comments on how I can improve this article. There may be a few things which I have overlooked and if you will write a comment, I will update my article accordingly and it will be very helpful for other readers. Thanks.