Com 签署F#程序集(强名称组件)

Com 签署F#程序集(强名称组件),com,f#,strongname,shell-extensions,code-translation,Com,F#,Strongname,Shell Extensions,Code Translation,我在CodeProject上找到了这篇文章: 我想尝试一下会很好,但在F#。所以我想出了以下代码: open System open System.IO open System.Text open System.Runtime.InteropServices open System.Windows.Forms open SharpShell open SharpShell.Attributes open SharpShell.SharpContextMenu [<ComVisible(

我在CodeProject上找到了这篇文章:

我想尝试一下会很好,但在F#。所以我想出了以下代码:

open System
open System.IO
open System.Text
open System.Runtime.InteropServices
open System.Windows.Forms
open SharpShell
open SharpShell.Attributes
open SharpShell.SharpContextMenu

[<ComVisible(true)>]
[<COMServerAssociation(AssociationType.ClassOfExtension, ".txt")>]
type CountLinesExtension() =
    inherit SharpContextMenu.SharpContextMenu()

    let countLines =
        let builder = new StringBuilder()
        do 
            base.SelectedItemPaths |> Seq.iter (fun x -> builder.AppendLine(sprintf "%s - %d Lines" (Path.GetFileName(x)) (File.ReadAllLines(x).Length)) |> ignore  )
            MessageBox.Show(builder.ToString()) |> ignore

    let createMenu =
        let menu = new ContextMenuStrip()
        let itemCountLines = new ToolStripMenuItem(Text = "Count Lines")
        do
            itemCountLines.Click.Add (fun _ -> countLines)
            menu.Items.Add(itemCountLines) |> ignore
        menu

    override this.CanShowMenu() = true
    override this.CreateMenu() = createMenu
开放系统
开放系统
开放系统.Text
open System.Runtime.InteropServices
打开System.Windows.Forms
开锐壳
打开SharpShell.Attributes
打开SharpShell.SharpContextMenu
[]
[]
类型CountLinesExtension()=
继承SharpContextMenu.SharpContextMenu()
让我们数线=
let builder=new StringBuilder()
做
base.selectedits |>Seq.iter(fun x->builder.AppendLine(sprintf“%s-%d行”(Path.GetFileName(x))(File.ReadAllLines(x.Length))|>忽略)
MessageBox.Show(builder.ToString())|>忽略
让我们创建菜单=
let menu=new ContextMenuStrip()
让itemCountLines=new ToolStripMenuItem(Text=“Count Lines”)
做
itemCountLines.Click.Add(乐趣->countLines)
menu.Items.Add(itemCountLines)|>忽略
菜单
覆盖此选项。CanShowMenu()=true
重写此项。CreateMenu()=CreateMenu
但是,我注意到在VS2012中不支持签署F#assembly(本文中的步骤4)。我了解到,如果我想这样做,我需要手动创建一个密钥(在命令提示符中键入“sn-kkeyname.snk”),然后在“项目属性->构建->其他标志”(-keyfile:keyName.snk)中添加一个标志

我仍然没有成功地运行这个。此外,使用作者的应用程序(在“调试Shell扩展”一节中),我得到一个错误,即我的程序集不包含COM服务器

我相信我在组件上签名有问题。你能帮我运行这个吗?

签署F程序集的一种方法是通过
AssemblyFileKeyAttribute
属性

创建一个新模块,并在其中放入:

module AssemblyProperties

open System
open System.Reflection;
open System.Runtime.InteropServices;

[<assembly:AssemblyKeyFileAttribute("MyKey.snk")>]

do()
模块组件属性
开放系统
开放系统;反思;
打开System.Runtime.InteropServices;
[,将
--keyfile:MyKey.snk
添加到
属性-->构建
选项卡中的
其他标志
字段中


使用任何一种方法;运行
sn-v myfsharpsassembly.dll
将断言编译后程序集是有效的。

可能问题在于程序集不是“ComVisible”

我在F#中使用这个库,如下所示:

[<assembly:ComVisible(true)>]
[<assembly:AssemblyKeyFile("Key.snk")>]
do
   ()
[]
[]
做
()

也就是说,我在顶级do块中指定程序集级属性。

我认为COM服务器错误的问题与您的密钥有关,而不是与F#有关。请确保您使用的证书已在本地计算机上注册。您也可以尝试类似的操作:“不包含COM服务器”表明COM组件的可见性/注册存在问题。您可以使用
sn-v
(或者是
-v
?)验证您的签名,但底线是,如果问题出在某个地方,说authenticode签名错误,就会出现错误。请获取有关该案例的ProcMon和DbgView。如果您获得更多关于准确错误消息等的详细信息,请在此处添加该详细信息,我运行了“sn-v”它导致程序集'ShellExtensions.dll'更准确地说是有效的,当我尝试在服务器管理器中加载此程序集时(从文章中),我得到一个错误“不是SharpShell扩展”。我还尝试在GAC中注册该程序集,但也不起作用。