what is the advantage of .net4's new no pia feature [deploying PIA's]

独自空忆成欢 提交于 2019-11-30 22:59:59

It's not that common to actually need a PIA. You have to have one if you expose any interop types from the Excel type library in one of your public classes. This goes wrong when other code uses your class and doesn't use the same interop library. A type in .NET is only identical when they came from the same assembly. You'd get a difficult to interpret error message like "Cannot cast Application to Application". The PIA ensures that everybody is using the same type. As long as everybody is using the same PIA version, which in itself is a difficult problem. Deploying your own interop DLL along with your app is fine if you can avoid this. Which is not difficult in most scenarios.

This problem was solved in .NET 4.0 through a feature called 'type equivalence'. It is specific to COM interface types, the CLR considers them compatible when they have the same [Guid] and the same declaration, regardless what assembly contains them. This was then taken advantage of with the 'embed interop types' feature (same as 'no pia'), the compiler embeds the interop types in the metadata of your assembly. Only the ones you actually use.

So you don't have to ship the interop library anymore and don't need the PIA. And it is a lot smaller since you only pay for the types you actually use. That's a lot of bang for the buck, strongly recommended.

I haven't done much interop myself, but I believe that:

  • Sometimes the PIA can be quite large; if the app itself is quite small, the PIA can dwarf it
  • The no-PIA approach is more flexible with respect to versioning: so long as you only use the members provided by the version of the COM object which is actually provided, you're fine... whereas I think with the PIA approach, you need to use the PIA for the same version of the COM object as the one on the target machine

One of the key things to understand about NoPIA is that it does not embedd the PIA into your assembly but instead only embeds the portion of the PIA that your application uses. It does this in a very fine grained manner (all the way down to the method level). The result is usually a very significant reduction in the deployment size of your application.

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