micjahn / ZXing.Net

.Net port of the original java-based barcode reader and generator library zxing
Apache License 2.0
2.7k stars 668 forks source link

Unable to use zxing.dll with SSRS 2016 #40

Open Timber11 opened 7 years ago

Timber11 commented 7 years ago

Is there a guide to how to use zxing.dll with SSRS 2016?

I have done all that should be required to use zxing.dll with SSRS but can't get past this error:

[rsRuntimeErrorInExpression] The Value expression for the image ‘Image1’ contains an error: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

The security error is generated when I use the BarCodeWriter.Write method.

This project is really useful and I would like to use it, but I'm stumped.

Thanks. Tim

micjahn commented 7 years ago

Can you explain what you have tried?

I did some tests 4 years ago. I got it running with SSRS 2012. Not sure if it works the same way with SSRS 2016. First I made a little additional project "SSRSBarcodeDemo" which implements ICustomReportItem. The custom report item adds a custom designer for the barcode control. The report item itself calls the BarcodeWriter class. To get it running I had to copy the SSRSBarcodeDemo assembly and the zxing.dll to the following directory: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies I think for SSRS2016 the "11.0" has to be changed to "14.0" or something similar.

I added the following lines

    <ReportItems>
      <ReportItem Name="ZXingBarcodeControl" Type="SSRSBarcodeDemo.BarcodeCustomReportItem, SSRSBarcodeDemo" />
    </ReportItems>

to the file C:\Program Files\Microsoft SQL Server\MSRS11.WORKSHOP\Reporting Services\ReportServer\rsreportserver.config before the closing tag </Extensions>

I added the lines

              <CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust" Name="ZXingBarcodeControl" Description="This code group grants FullTrust to ZXing Barcode Demo assembly.">
                <IMembershipCondition class="StrongNameMembershipCondition" version="1" PublicKeyBlob="0024000004800000140100000602000000240000525341310008000001000100014c9a01956f13a339130616473f69f975e086d9a3a56278936b12c48ca45a4ddfee05c21cdc22aedd84e9468283127a20bba4761c4e0d9836623fc991d562a508845fe314a435bd6c6ff4b0b1d7a141ef93dc1c62252438723f0f93668288673ea6042e583b0eed040e3673aca584f96d4dca19937fbed30e6cd3c0409db82d5c5d2067710d8d86e008447201d99238b94d91171bb0edf3e854985693051ba5167ca6ae650aca5dd65471d68835db00ce1728c58c7bbf9a5d152f491123caf9c0f686dc4e48e1ef63eaf738a12b3771c24d595cc5a5b5daf2cc7611756e9ba3cc89f08fb9adf39685bd5356858c010eb9aa8a767e5ef020408e0c9746cbb5a8" />
              </CodeGroup>

to the file C:\Program Files\Microsoft SQL Server\MSRS11.WORKSHOP\Reporting Services\ReportServer\rssrvpolicy.config and to the file C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\RSPreviewPolicy.config

Without the custom report item and using only custom code in the report you should get a different exception now: "That assembly does not allow partially trusted callers". I could only fix it with a special build of zxing which adds the attribute AllowPartiallyTrustedCallers. https://docs.microsoft.com/en-us/sql/reporting-services/custom-assemblies/using-strong-named-custom-assemblies

Timber11 commented 7 years ago

I positioned the zxing.dll (.NET 2.0 version) in the C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PrivateAssemblies folder.

I created a new report. I added a reference to zxing.dll and system.drawing. I added the following custom code:

Public Function getBarCode(ByVal s As String) As Byte() Dim b As System.Drawing.Bitmap Dim writer As New ZXing.BarcodeWriter() writer.Format = ZXing.BarcodeFormat.DATA_MATRIX writer.Options = New ZXing.Common.EncodingOptions writer.Options.Width = 100 writer.Options.Height = 100 writer.Options.Margin = 30 b = writer.write("1234") Dim bitmapData as byte() = Nothing Using ms As New System.IO.MemoryStream() b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) bitmapData = ms.ToArray() End Using
Return bitmapData End Function

Then on the report you add an Image. Set the Image Source to Database. Set the Field to an expression with value =Code.getBarCode("1234"). Set the MIME type to image/bmp.

Run the report and get the runtime error.

Thanks for you attention

Tim

micjahn commented 7 years ago

Then it is as I said before. You have to add the mentioned lines to the file rssrvpolicy.config and RSPreviewPolicy.config. And you need a zxing.dll with the attribute AllowPartiallyTrustedCallers which isn't available yet. I can't say when I will provide such a version of the assembly because it has some side effects. Meanwhile you can try to write a wrapper assembly which contains your custom report code. With such a wrapper it should be possible to work around the strong name problem.

Codechenyin commented 6 years ago

ah,i can‘t understan,can you?

VIJAYASHANKAR commented 4 years ago

I have followed all the steps and end up with the error

anonymous access. [rsRuntimeErrorInExpression] The Value expression for the image 'Image1' contains an error: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

asushil commented 3 years ago

[rsRuntimeErrorInExpression] The Value expression for the image 'Image1' 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 'Image1' is "=Code.getBarCode(Fields!csv.Value)", which is not a valid ImageData. Preview complete -- 0 errors, 2 warnings

bergerb-com commented 2 years ago

I would like to leave a link here, if someone has the same issue.

This happens because of Bitmap.LockBits

https://github.com/micjahn/ZXing.Net/blob/eef88ee53ced1035eade39f44544aa955d2db6b6/Source/lib/renderer/BitmapRenderer.cs#L169 https://www.codeproject.com/Articles/625868/LockBits-Alternative-SecurityException-Workaround

More details: https://stackoverflow.com/a/71710028/11829240