我有一个问题有点复杂解释(否则我不会有问题:)),但我会试一试。
我有一个asp.net webapplication,我试图在运行时从其他项目加载用户控件,而原始网站没有引用包含usercontrol的项目/ dll。
当我知道所有需要的文件的路径(dll等......我可以使它工作),尤其是对于assembly.loadfile(Path)。但是现在我正在使用一个只能为我提供流的virtualpathProvider。所以我不能再使用assembly.loadfile(path),但我必须使用Assembly.load(byte [])。问题是,当切换到assembly.load(byte [])时,我无法再编译ascx文件。它给了我这个例外:
The 'ReferencedAssemblies' property cannot contain null or empty strings. Parameter name: options
加载的程序集之间的唯一区别是属性Location给出错误。我假设程序集的位置传递给构建器。
有没有人可以就如何解决这个问题提供任何想法? (除了:将程序集写入磁盘上的临时文件夹)
我正在思考我自己的构建提供商或者朝这个方向发展的事情,但我担心我会遇到同样的问题。
这最终似乎是同样的问题: Supply Assembly to CompilerParameters ReferencedAssemblies from memory and not disk?
现在为了更好地理解,并可能帮助其他人: 完整的堆栈跟踪和完整的源代码:
at Microsoft.VisualBasic.VBCodeGenerator.CmdArgsFromParameters(CompilerParameters options)
at Microsoft.VisualBasic.VBCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at System.CodeDom.Compiler.CodeCompiler.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath)
at WebApplication1._Default.CompileAddControl(String path) in E:\Google Drive\Lab\DLLLoading\WebApplication1\Default.aspx.vb:line 29
我有2个网络应用程序,一个正常的应用程序,以及一个仅包含ascx(usercontrol)文件的文件。
这包含usercontrol而不包含其他内容。
<%@ Control
Language="vb"
AutoEventWireup="false"
CodeBehind="Showtime.ascx.vb"
Inherits="WebApplication2.Showtime,WebApplication2"
%>
<asp:Label ID="lblTime" runat="server"></asp:Label>
这里我将程序集名称添加到Inherits value =&gt;否则应用程序将无法理解必须在代码的不同程序集中查找。
Public Class Showtime
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblTime.Text = "Hello world from external :" & Now.ToShortDateString & " - " & Now.ToShortTimeString
End Sub
End Class
这就是Web应用程序所需的全部内容。
这是神奇发生的地方
Imports System.Reflection
Imports System.Web.Hosting
Imports System.IO
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf Global_asax.ResolveEventHandler
End Sub
Public Shared Function ResolveEventHandler(sender As Object, args As System.ResolveEventArgs) As Assembly
Try
If args.Name.StartsWith("WebApplication2") Then
Dim _ret As Assembly
''START OF NOT WORKING CODE
''To make this path work, I added the other application with ascx's as a virtual directory (~/apps/HelloWorld/) in IIS.. (make sure to NOT have a web.config in webapplication2)
Dim _vf As VirtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/apps/HelloWorld/bin/WebApplication2.dll")
'copy to memorystream so i can use the very handy toArray function (speed of coding)
Using _ms As New MemoryStream, _s As Stream = _vf.Open()
_s.CopyTo(_ms)
_ret = Assembly.Load(_ms.ToArray)
End Using
''END OF NOT WORKING CODE
''START OF WORKING CODE
'Dim _ret As Assembly = Assembly.LoadFile(HostingEnvironment.MapPath("~/apps/HelloWorld/bin/WebApplication2.dll"))
''START OF WORKING CODE
Return _ret
End If
Return Nothing
Catch ex As Exception
'this is a test project so debug.writeline will do..
Debug.WriteLine(ex.ToString)
Return Nothing
End Try
End Function
这个有这个控制:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
CompileAddControl("~/apps/HelloWorld/Showtime.ascx")
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub
Private Shared m_dict As New Dictionary(Of String, Type)
Public Sub CompileAddControl(path As String)
Try
Dim _t As Type = Nothing
If Not m_dict.TryGetValue(path, _t) Then
_t = BuildManager.GetCompiledType(path)
m_dict.Add(path, _t)
End If
Dim _o As Control = DirectCast(BuildManager.CreateInstanceFromVirtualPath(path, _t), Control)
phControls.Controls.Add(_o)
Catch ex As Exception
phControls.Controls.Add(New LiteralControl(ex.Message))
End Try
End Sub
答案 0 :(得分:0)
ascx编译器仅支持通过路径引用dll。原因是安全性。在我的问题中的示例中,我必须将所需的dll保存到磁盘上的临时文件夹中,然后从该临时位置加载它。由于安全性,没有其他解决方案。