【问题标题】:"Exception has been thrown by the target of an invocation” from Script task脚本任务中的“调用目标引发异常”
【发布时间】:2018-04-05 00:57:42
【问题描述】:

在使用 DTEXEC 从 .bat 文件运行包含脚本任务的 SSIS 包时,我收到以下错误。使用“EXEC xp_cmdshell”从 MS SQL 中的存储过程调用 .bat 文件

错误: 代码:0x00000001 说明:调用的目标已抛出异常。

当我在 Visual Studio 中运行 SSIS 包时,它运行良好。 当我手动运行 .bat 文件时,它运行良好。 我收到错误的唯一一次是在运行存储过程时。

这是脚本任务:

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.IO
Imports System.IO.FileInfo
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()>
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum
    Protected Sub SaveFile(ByVal url As String, ByVal localpath As String)
        Dim loRequest As System.Net.HttpWebRequest
        Dim loResponse As System.Net.HttpWebResponse
        Dim loResponseStream As System.IO.Stream
        Dim loFileStream As New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
        Dim laBytes(256) As Byte
        Dim liCount As Integer = 1
        Try
            loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
            loRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
            loRequest.Timeout = 600000
            loRequest.Method = "GET"
            loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
            loResponseStream = loResponse.GetResponseStream
            Do While liCount > 0
                liCount = loResponseStream.Read(laBytes, 0, 256)
                loFileStream.Write(laBytes, 0, liCount)
            Loop
            loFileStream.Flush()
            loFileStream.Close()
        Catch ex As Exception
        End Try
    End Sub
    Public Sub Main()
        Dim url, destination, newname As String
        destination = Dts.Variables("Folder_Destination").Value.ToString + "\" + Dts.Variables("FileName").Value.ToString + ".xpdf"
        url = "http://localhost/reportserver?/PDFExport/DEVOrderPDF&rs:Command=Render" + Dts.Variables("FileName").Value.ToString + "&rs:Format=PDF"
        SaveFile(url, destination)

        newname = Path.GetFileNameWithoutExtension(destination) + ".pdf"
        My.Computer.FileSystem.RenameFile(destination, newname)

        Dts.TaskResult = ScriptResults.Success
    End Sub
End Class

我将补充一点,当运行存储过程时,我看到文件正在使用 .xpdf 扩展名创建,但是大小为 0kb,并且在将扩展名更改为 .pdf 时无法打开。它几乎就像它部分运行,但在调用 SSRS url 时没有完成。

【问题讨论】:

  • 您对 SSRS 使用什么类型的身份验证?您是否检查了事件查看器 - 安全 - 未能检查用于连接 SSRS 的帐户的审核?
  • 我对 SSRS 很陌生,但我相信我使用的是 Windows 身份验证。我检查了事件查看器并看到了几个失败的审核。我也看到了帐户名称,但我该去哪里更改/更正它?
  • 我找到了在 IIS 管理器中更改运行报告的帐户名称的位置。事件查看器现在显示成功的审核,但问题仍然存在。
  • 您应该使用 using 语句或在 finally 块中释放文件句柄。事实上,如果抛出异常,您的文件句柄将不会被释放。

标签: sql-server visual-studio reporting-services ssis


【解决方案1】:

这里有 3 件事可以帮助您。

  1. 别吃例外,这是你的朋友,你应该退货。
  2. 始终释放您的资源。
  3. 检查 SSRS 错误日志。

SSRS 错误日志将包含帮助您解决问题的详细信息。可以在 %ProgramFiles%\Microsoft SQL Server\MSSQL.x\Reporting Services\LogFiles 中找到这些日志。

修复 1 最终将是解决方案,但是,如果您看不到它,则无法修复它。

Dim loFileStream = Nothing
Try
     Try
       loFileStream = New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
       loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
       loRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
       loRequest.Timeout = 600000
       loRequest.Method = "GET"
       loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
       loResponseStream = loResponse.GetResponseStream
       Do While liCount > 0
           liCount = loResponseStream.Read(laBytes, 0, 256)
           loFileStream.Write(laBytes, 0, liCount)
       Loop
       loFileStream.Flush()
   Catch ex As Exception
       LogExceptionSomewhere("My process failed because of -->" + ex.ToString())
   End Try
Finally    
    If loFileStream<>Nothing Then    
        loFileStream.Close()
    EndIf
End Finally

【讨论】:

  • 我添加了您的代码,现在当我在 Visual Studio 中运行它时,我得到了相同的错误,并包含以下详细信息:在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig,布尔构造函数)在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object[] 参数,Object[] 参数)在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化)
  • 您可以暂停代码并检查 Exception.InnerException 吗?
猜你喜欢
  • 2018-09-20
  • 2015-05-13
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2021-12-27
相关资源
最近更新 更多