GOTCHA #73 Simply tur ning the switch for COM interop is dangerousChecking the project setting Register for COM Interop prepares your assembly for COM interoperability. When your code is compiled, Visual Studio invokes regasm to place the codebase enTRy details for the assembly into the Registry at HKEY_CLASSES_ROOT\CLSID\...\InprocServer32. When things are that easy, you probably wonder "what's the catch?" There are several things to ponder. First, consider some of the .NET capabilities that are not quite supported in COM, or must not be used with COM:
Say you are working on your code one afternoon and someone walks in saying, "Hey I need to access this class through COM, can I?" You say "Sure, all I need to do is turn on the settings to register the assembly for COM interop." This will result in the COM clients talking to your assembly in the manner shown in Figure 8-18. Figure 8-18. Improperly exposing an assembly for COM InteropAsk the questions:
Most likely, you wrote your assembly with .NET clients in mind. When you are approached with a request to expose your classes for COM interoperability, it may be better to write a separate façade assembly that the COM client will interact with, as shown in Figure 8-19. You provide a wrapper or façade that takes requests from COM clients and routes them to one or more .NET classes. The wrapper classes do not expose any COM interfaces directly to the COM client; instead they implement .NET interfaces that are available and accessible to those clients (see Gotcha #72, "All but one of the ClassInterface options are ineffective"). Why should you do this? The wrapper or façade approach gives you a number of advantages:
Figure 8-19. Exposing .NET components for COM interop using Wrappers
IN A NUTSHELLDo not open up an assembly for arbitrary COM access. Instead provide a façade for the COM clients to interact through. SEE ALSOGotcha #71, "Auto-generating GUID for your classes leads to versioning woes" and Gotcha #72, "All but one of the ClassInterface options are ineffective." |