简体   繁体   English

在构建服务器上注册COM引用的DLL

[英]Registering COM referenced DLLs on a build server

We're developing a C# application that references a few COM libraries (AutoIT for example). 我们正在开发一个引用一些COM库的C#应用​​程序(例如AutoIT)。

I am including all referenced components under source control, in a 3rd party "Libs" folder. 我在源控件下包含所有引用的组件,在第三方“Libs”文件夹中。

The problem is that COM dll's don't have a HintPath property in the .csproj file, and i assume these must be manually registered using regsvr32 (or using a script of some sort). 问题是COM dll在.csproj文件中没有HintPath属性,我认为必须使用regsvr32(或使用某种脚本)手动注册这些属性。

I am currently looking into creating an MSBuild script that will run before every build, however i couldn't figure out if i should be manually calling regsvr32.exe or use some predefined MSBuild task? 我目前正在寻找创建一个将在每次构建之前运行的MSBuild脚本,但是我无法弄清楚我应该手动调用regsvr32.exe还是使用一些预定义的MSBuild任务?

Currently, this is what i've attmpted as a test: 目前,这是我作为测试所支持的:

 <?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <MyAssemblies Include="D:\*.dll" />
  </ItemGroup>
  <Target Name="Build">
    <RegisterAssembly
      Assemblies="@(MyAssemblies)" >
    </RegisterAssembly>
  </Target>
</Project>

This generates errors that the DLLs i've placed in the given folder are not valid DLLs. 这会生成我在给定文件夹中放置的DLL不是有效DLL的错误。

What is a good solution for this problem? 什么是这个问题的好方法?

EDIT: 编辑:

Projects that reference COM dlls have something similar to this in the .csproj file: 引用COM dll的项目在.csproj文件中有类似的内容:

<COMReference Include="AutoItX3Lib">
      <Guid>{F8937E53-D444-4E71-9275-35B64210CC3B}</Guid>
      <VersionMajor>1</VersionMajor>
      <VersionMinor>0</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>tlbimp</WrapperTool>
      <Isolated>False</Isolated>
    </COMReference>

This does not include any hint path as other managed assemblies, so on a build server, the referenced COM dll is not found. 这不包括任何提示路径作为其他托管程序集,因此在构建服务器上,找不到引用的COM dll。

When registering the COM dll on the build server using REGSVR32, the build succeeds. 使用REGSVR32在构建服务器上注册COM dll时,构建成功。

For my original answer to a similar question see: TFS Build server and COM references - does this work? 对于我对类似问题的原始答案,请参阅: TFS构建服务器和COM引用 - 这是否有效?

A better option for build servers may be to use the COMFileReference item in your project file instead of COMReference . 构建服务器的更好选择可能是使用项目文件中的COMFileReference项而不是COMReference An example would look like this: 一个例子看起来像这样:

<ItemGroup>
   <COMFileReference Include="MyComLibrary.dll">
     <EmbedInteropTypes>True</EmbedInteropTypes>
   </COMFileReference>
</ItemGroup>

The COM dll doesn't need to be registered on the machine for this to work. COM dll无需在机器上注册即可使用。

Each COMFileReference item can also have a WrapperTool attribute but the default seems to work fine. 每个COMFileReference项也可以有一个WrapperTool属性,但默认似乎工作正常。 The EmbedInteropTypes attribute is not documented as being applicable to COMFileReference, but it seems to work as intended. EmbedInteropTypes属性未记录为适用于COMFileReference,但它似乎按预期工作。

See https://docs.microsoft.com/en-ca/visualstudio/msbuild/common-msbuild-project-items#comfilereference for a little more detail. 有关详细信息,请参阅https://docs.microsoft.com/en-ca/visualstudio/msbuild/common-msbuild-project-items#comfilereference This MSBuild item has been available since .NET 3.5. 从.NET 3.5开始,此MSBuild项目已可用。

You don't register COM servers on a build server. 您不在构建服务器上注册COM服务器。 That's only required when you actually want to run the compiled code. 只有在您真正想要运行已编译的代码时才需要这样做。 What you need is the type libraries for the COM servers so you can get the interop assemblies. 您需要的是COM服务器的类型库,以便您可以获得互操作程序集。 Which you create with Tlbimp.exe. 您使用Tlbimp.exe创建的。

Whether you want to run Tlbimp on the build server or up front on a dev machine depends a great deal on how you deploy these COM servers. 您是想在构建服务器上运行Tlbimp还是在开发机器上预先运行Tlbimp,这在很大程度上取决于您如何部署这些COM服务器。 Keeping a copy of the COM executables and .tlb files very close to your interop libraries is a good idea. 保持COM可执行文件和.tlb文件的副本非常靠近您的互操作库是一个好主意。 In other words, check them in. The installer can now retrieve a known-good version of the COM server as well. 换句话说,检查它们。安装程序现在也可以检索已知良好版本的COM服务器。

1) Try to reference the COM libraries to your csproj as references - if you haven't done so. 1)尝试将COM库引用到csproj作为参考 - 如果你还没有这样做的话。

2) Try to append to your csproj file: 2)尝试附加到您的csproj文件:

<Project ... >
    ...
    <Target Name="BeforeBuild">
        <Exec Command="regsvr32.exe yourComponent.dll" />
    </Target>
</Project>

PS: if you are using some sort of build server software you should not modify the csproj but the script used by the build at the server. PS:如果您使用的是某种构建服务器软件,则不应修改csproj,而应修改服务器上构建使用的脚本。

I'm not sure, if there's any task in MSBuild, to call regsvr32, but REgisterAssembly is calling regasm.exe - that is - registering .NET components for COM interop. 我不确定,如果MSBuild中有任何任务,调用regsvr32,但REgisterAssembly正在调用regasm.exe - 即为COM互操作注册.NET组件。

I am sure, that just calling regsvr32 manually would be the fastest way to achieve the desired result. 我确信,手动调用regsvr32将是实现所需结果的最快方法。

Another thing is - what will happen if the COM DLLs are already registered by the previous build and you'll run your build script again? 另一件事是 - 如果COM DLL已经由之前的版本注册并且您将再次运行构建脚本会发生什么? (I don't really know how the regsvr32 will react, just a thought here) (我真的不知道regsvr32会如何反应,只是想到这里)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM