Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 {}
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...