Referencing external assemblies from PowerShell snap-in

和自甴很熟 提交于 2019-12-23 03:22:43

问题


I'm developing a custom PowerShell snap-in, which references another project in the solution.
When I try to debug the snap-in (following [these instructions][1]), the assembly fails to load and my cmdlet fails with the message "Could not load file or Assembly..."

How do you instruct PowerShell on how to locate assemblies, or how do you specify where are located the assemblies needed by the snap-in?

I'd prefer to avoid registering the assemblies in the GAC, at least during development.


回答1:


Not sure of the exact behaviour, but I would try and make use of fuslogvw to see exactly where the runtime is looking for the problematic assemblies. That would give you a clue as to how to get them to be copied into the correct place on build. This post by Scott Hanselman is quite useful, and this is the official documentation.

If you find the solution, please add an answer, as this must be a common scenario.




回答2:


SOLUTION (Posting it here as suggested by one of the comments to my question)

My problem was rather specific, as I'm developing on a 64 bits server machine, but I'm posting the solution in case it could help someone else as well.

Using fuslogvw as suggested, I saw that the dependent assembly was being searched using the machine.config file under C:\Windows\Microsoft.NET\Framework64 and then the binding failed; launching the project with "start without debugging", instead, the machine.config file under C:\Windows\Microsoft.NET\Framework was taken (notice the missing 64 at the end).

I thought that the problem could be due to the image format, and infact the dependent assembly was being compiled with x86 as CPU target; I changed it to "Any CPU" and now the assembly is loaded correctly.



来源:https://stackoverflow.com/questions/1542495/referencing-external-assemblies-from-powershell-snap-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!