SSRS report in VS2015 using a custom function to convert a value to barcode image using zxing.dll. I found this article Generate barcode using Visual Studio local report or SSRS which is essentially identical to what I'm trying to do and have followed it. When I preview the report, it produces the warnings below and the image shows the red-x. I can deploy the report but no images appear in the related columns. The image is set with the correct mime type (image/bmp) but I've tried others with the same results.
NOTE: I've searched for and read several articles that helped me make progress but can't seem to resolve this issue.
The complete errors are:
[rsRuntimeErrorInExpression] The Value expression for the image ‘ImageFromFunction’ contains an error: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
[rsInvalidDatabaseImageProperty] The value of the ImageData property for the image ‘ImageFromFunction’ is “=Code.TextToBarcode128Bmp(Fields!barcode_value.Value)”, which is not a valid ImageData.
I have a WPF where this same code (in C#) works fine. There is just something about using it in SSRS that it doesn't like. The first message seems to indicate that it is a permissions/version kind of thing. I know that SSRS is .NET 2.0. I've set my reference to System.Drawing to 2.0 versions and copied over the 2.0 version of zxing.dll to the server.
It looks like it is attempting to get security permissions (probably for the zxing.dll?) but is unable to reach System.Security.Permissions.SecurityPermission?
References
zxing, Version=0.15.0.0, Culture=neutral, PublicKeyToken=4e88037ac681fe60
System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
I tried added a reference to the 2.0 version of System.Security but it still produced the same error.
This is the custom function:
Public Function TextToBarcode(ByVal text As String) As Byte()
Dim writer As New ZXing.BarcodeWriter()
writer.Format = ZXing.BarcodeFormat.CODE_128
writer.Options = New ZXing.Common.EncodingOptions
writer.Options.Width = 125
writer.Options.Height = 100
Dim bmp As System.Drawing.Bitmap = writer.Write(text)
Dim ms AS System.IO.MemoryStream = new System.IO.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim imagedata as byte()
imagedata = ms.GetBuffer()
Return imagedata
End Function
UPDATE
I followed the steps in the article SSRS: System.Security.Permissions.SecurityPermission (Thanks, Michael and Superman) which is based upon this article How to use custom assemblies or embedded code in Reporting Services
On the server, I added zxing.dll to the GAC from its location in D:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\bin
On the server, I added a CodeGroup to D:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\rssrvpolicy.config:
<CodeGroup
class="UnionCodeGroup"
version="1"
PermissionSetName="FullTrust"
Name="ZXingCodeGroup"
Description="Code group for ZXing">
<IMembershipCondition
class="UrlMembershipCondition"
version="1"
Url="D:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\bin\zxing.dll"
/>
</CodeGroup>
And I restarted Reporting Services. No change in behavior: Running deployed report produces no errors but does not display the barcode.
On the VS dev system, I then added the CodeGroup to C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PrivateAssemblies\RSPreviewPolicy.config and closed and re-opened Visual Studio. No change in behavior: Preview of report shows same warnings and fails to display the barcode.
UPDATE
I've confirmed this is some kind of permissions issue. By commenting the line that does the ZXing.BarcoderWriter.Write() call it no longer produces this error. In fact, by replacing the function body with code that populates a byte array from image data (below), it successfully displays the image:
Public Function getBarCodeHorizontal(ByVal text1 As String) As Byte()
Dim text as String = "89504E470D0A1A0A0000000D494844520000009600000028080200000093A394B0000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000026549444154785EED9AB16EDB400C864F7D112BC892C10F5103196474CD1B548F20C32FE1B1A3FC065D8B6A28203F84072F46EC2751792475C79364C0E822B1E0372824EF3FDAB93FC7C041B2AEEBDC145996C1135621204D0888C934EC0A155CE46E04C9C293AB424F6229A3546A085A22821EE24701E9A942C84AD00C0461952AC0584FABB2EE758894510ACF81009EA1422910768DB748BEF057432D66A17ACC42F59885EA310BD56316AAC72C548F59A81EB3503D66A17ACC42F59885EA310BD56316AAC72C548F59A81EB3503D66A17ACC42F59885EA310BD56316AAC72C544FF21F8F8646EC16AAC72C54CFFF61E169976D8F774EFE9DFB719BED4E9CA8C16EA17ACC42F52CCA4298878C188B7EBA31E9B00CEAEDF1CA254FD0F3484C87ACC8E2AB0D1AA7133599AEC3E64B603916C2E96CCEF50D3EE4745DBB2E733C2428E6E5BAC5E2AD765C05E0FC378EEADDFE521EA8EAEBACBFD5E70D8ABF7EAB9A9F7FD8A2D3AF43F1F1BEF2D16E73A8A8415B35E58F672C19375F04F84D2C805B5D383ED2C8A018D3B67205DB0DF83AA5A21EC5107131DDD61394E32089279BCFCF626EE1E7A529DE5E3879C0EA75EDCE57B850F7EB994B88AF079A32C75197970D89DDEAFDC3E13D8C7710E9A7A2573EC9B8F9FC2CC6C297B7A2B97C72F200EFDCFA152C483C1B20AFD9EFEFE8177B080E567BAAA07DF9658F2A7FA59E64A2F9FCF0FB991F7F90724E618CA7DB4F2C186449DCAB7DDC275123D5BE0F20669F588DDBFDAB0D37CB77F0A0F9CC2CC742000F9388E7836748F49E11415DD4B5F81D27CA429DFC2C7862DBAAED17FD57D684656C1E364E379F17FB33B77AECA3BD7ACC42E538F7178726B2F89D7D5EF80000000049454E44AE426082"
Dim bytes As Byte() = New Byte(text.Length \ 2 - 1) {}
Dim b as Byte
For i As Integer = 0 To text.Length - 1 Step 2
bytes(i \ 2) = Byte.Parse(text(i).ToString() & text(i + 1).ToString(), System.Globalization.NumberStyles.HexNumber)
Next
Return bytes
End Function
UPDATE
I tried changing the CodeGroup on the server to Strong using the zxing.dll public key token, but no improvement:
<CodeGroup
class="UnionCodeGroup"
version="1"
PermissionSetName="FullTrust"
Name="ZXingCodeGroup"
Description="Code group for ZXing">
<IMembershipCondition
class="StrongNameMembershipCondition"
version="1"
PublicKeyBlob="4e88037ac681fe60"
/>
</CodeGroup>