How to elegantly prevent a webservice proxy from being exposed to COM?

孤街醉人 提交于 2019-11-27 08:13:56

问题


I have a C# assembly that I use as an in-proc COM server consumed by an unmanaged C++ application. The assembly consumes a webservice that will not ever change so there's no need to ever update the webservice proxy classes. That's why the proxy classes are created once and Reference.cs files are simply put into the repository and only compiled from there.

The problem is that by default the webservice proxy classes are public and are therefore exposed to COM. This inflates the typelib and pollutes registry. Changing visibility to internal break the assembly, so those entities need to remain public, but need not be exposed to COM.

The dumb way is to approach every public interface/class in the Reference.cs files and mark it with

[System.Runtime.InteropServices.ComVisible(false)]

After that it is no longer exposed to COM.

Is there a better way?


回答1:


You could

  • mark the assembly itself to not to be ComVisible and then explicitly mark all interfaces/classes/enums you want to expose to COM as ComVisible.
  • use a 2nd file and partial class to mark your proxy types to be ComVisible(false)

    [System.Runtime.InteropServices.ComVisible(false)]
    partial class YourProxyType {} 
    
    [System.Runtime.InteropServices.ComVisible(false)]
    partial class AnotherProxyType {}
    


来源:https://stackoverflow.com/questions/1649752/how-to-elegantly-prevent-a-webservice-proxy-from-being-exposed-to-com

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