部署MEF零件 [英] Deploying MEF parts

查看:127
本文介绍了部署MEF零件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个可执行的外壳程序,该外壳程序使用MEF加载程序集(MEF零件)并根据加载的功能显示/执行相应的操作.

I have built an executable shell that using MEF loads assemblies (MEF Parts) and displays / acts accordingly to the loaded functionality.

我现在想将此应用程序部署为ClickOnce安装.有没有人对此有策略,目前我已经看到了两种策略.

I now want to deploy this application as a ClickOnce install. Does anyone have a strategy for this, at the moment I have seen 2 strategies.

  1. 构建和安装零件,并将其作为重新分发组件添加到shell应用程序-这意味着这两个安装基本上已焊接在一起,这意味着MEF基本没有意义

  1. Build and installer for the parts and add it as a Redistributal Component to the shell application - this means that the 2 installations are basically welded together and it means that MEF is basically pointless

在外壳中构建一个下载器功能,这再次意味着在开始之前需要了解每个MEF部分,并使MEF毫无意义.

Build a downloader function into the shell, this again means that each MEF part needs to be known about before we start, and making MEF pointless.

还有其他人知道其他方法吗?我是否可以反过来建立依赖关系,所以用于MEF部件的clickonce安装程序知道应该使用哪个shell?

Does anyone else know of any other approaches ? Can I build the dependancy the other way around, so a clickonce installer for the MEF parts knows what shell it should use ?

谢谢

推荐答案

我所做的是使用

What I did was use the Packaging API and make a custom file extension that is mapped to the shell UI. All it does is unzip the package to the ProgramData\MyApp\Extensions folder. Then when the app is restarted, the part shows up.

有关详细信息,请参阅此博客文章

           ' Open the Package.
        ' ('using' statement insures that 'package' is
        '  closed and disposed when it goes out of scope.)
        Using package As Package = package.Open(fileName, FileMode.Open, FileAccess.Read)
            tFolder = IO.Path.Combine(tFolder,
                MediaToolz.SharedServices.FileSystem.GetSafeFileName(package.PackageProperties.Title))

            Dim directoryInfo As New DirectoryInfo(tFolder)
            If directoryInfo.Exists Then
                directoryInfo.Delete(True)
            End If
            directoryInfo.Create()

            For Each part In package.GetParts()
                If part.ContentType = Packages.MediaToolzAddinMimeType Then
                    ExtractPart(part, tFolder)
                End If
            Next

            package.Close()
        End Using


'  --------------------------- ExtractPart ---------------------------
''' <summary>
'''   Extracts a specified package part to a target folder.</summary>
''' <param name="packagePart">
'''   The package part to extract.</param>
''' <param name="targetDirectory">
'''   The relative path from the 'current' directory
'''   to the targer folder.</param>
Private Shared Sub ExtractPart(ByVal packagePart As PackagePart, ByVal targetDirectory As String)
    ' Create a string with the full path to the target directory.
    Dim pathToTarget As String = targetDirectory
    If pathToTarget.EndsWith(IO.Path.DirectorySeparatorChar) = False Then pathToTarget += IO.Path.DirectorySeparatorChar
    ' Remove leading slash from the Part Uri,
    '   and make a new Uri from the result
    Dim stringPart As String = packagePart.Uri.ToString().TrimStart("/"c)
    ' I added this line to take off the content pat
    stringPart = IO.Path.GetFileName(stringPart)

    Dim partUri As New Uri(stringPart, UriKind.Relative)

    ' Create a full Uri to the Part based on the Package Uri
    Dim uriFullPartPath As New Uri(New Uri(pathToTarget, UriKind.Absolute), partUri)

    ' Create the necessary Directories based on the Full Part Path
    'Directory.CreateDirectory(Path.GetDirectoryName(uriFullPartPath.LocalPath))

    ' Create the file with the Part content
    Using fileStream As New FileStream(uriFullPartPath.LocalPath, FileMode.Create)
        CopyStream(packagePart.GetStream(), fileStream)
    End Using 'Close & dispose fileStream.
End Sub

'  --------------------------- CopyStream ---------------------------
''' <summary>
'''   Copies data from a source stream to a target stream.</summary>
''' <param name="source">
'''   The source stream to copy from.</param>
''' <param name="target">
'''   The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
    Const bufSize As Integer = &H1000
    Dim buf(bufSize - 1) As Byte
    Dim bytesRead As Integer = 0
    bytesRead = source.Read(buf, 0, bufSize)
    Do While bytesRead > 0
        target.Write(buf, 0, bytesRead)
        bytesRead = source.Read(buf, 0, bufSize)
    Loop
End Sub

这篇关于部署MEF零件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆