返回图像的SSRS函数会产生System.Security.Permissions.SecurityPermission错误

时间:2017-06-01 22:12:59

标签: image reporting-services

VS2015中的SSRS报告使用自定义函数将值转换为使用zxing.dll的条形码图像。我发现这篇文章Generate barcode using Visual Studio local report or SSRS与我正在尝试做的事情基本相同,并且遵循了它。当我预览报告时,它会生成下面的警告,图像显示red-x。我可以部署报告,但相关列中不显示任何图像。使用正确的mime类型(image / bmp)设置图像,但我尝试了其他具有相同结果的图像。

注意:我已经搜索并阅读了几篇帮助我取得进展的文章,但似乎无法解决这个问题。

完整的错误是:

[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.

我有一个WPF,其中相同的代码(在C#中)工作正常。在SSRS中使用它并不喜欢它。第一条消息似乎表明它是一种权限/版本的东西。我知道SSRS是.NET 2.0。我已将System.Drawing的引用设置为2.0版本,并将2.0版本的zxing.dll复制到服务器。

看起来它正在尝试获取安全权限(可能是zxing.dll?)但无法访问System.Security.Permissions.SecurityPermission?

参考

zxing, Version=0.15.0.0, Culture=neutral, PublicKeyToken=4e88037ac681fe60
System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

我尝试添加对2.0版System.Security的引用,但它仍然产生了同样的错误。

这是自定义功能:

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

更新

我按照文章SSRS: System.Security.Permissions.SecurityPermission(谢谢,迈克尔和超人)中的步骤进行了操作,该文章基于这篇文章How to use custom assemblies or embedded code in Reporting Services

在服务器上,我从位于D:\ Program Files \ Microsoft SQL Server \ MSRS12.MSSQLSERVER \ Reporting Services \ ReportServer \ bin

的位置将zxing.dll添加到GAC

在服务器上,我将一个CodeGroup添加到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>

我重新启动了Reporting Services。行为没有变化:运行已部署的报告不会产生错误,但不会显示条形码。

在VS dev系统上,我将CodeGroup添加到C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ PrivateAssemblies \ RSPreviewPolicy.config并关闭并重新打开Visual Studio。行为没有变化:报告预览显示相同的警告,无法显示条形码。

更新

我已经确认这是某种权限问题。通过注释执行ZXing.BarcoderWriter.Write()调用的行不再产生此错误。事实上,通过用从图像数据填充字节数组的代码替换函数体(下图),它成功显示图像:

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

更新

我尝试使用zxing.dll公钥令牌将服务器上的CodeGroup更改为Strong,但没有改进:

<CodeGroup
    class="UnionCodeGroup"
    version="1"
    PermissionSetName="FullTrust"
    Name="ZXingCodeGroup"
    Description="Code group for ZXing">
    <IMembershipCondition
        class="StrongNameMembershipCondition"
        version="1"
        PublicKeyBlob="4e88037ac681fe60"
    />
</CodeGroup>

0 个答案:

没有答案