Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a ServicedComponent instance in-process for unit tests?

Tags:

c#

com+

[ClassInterface(ClassInterfaceType.None)]
[Guid("12C969B3-330D-4230-ACDA-F9BED3286B1E")]
[ProgId("Lib.Class")]
public class MyService : ServicedComponent, IMyService
{
      ...
}

Unit Test:

MyService target = new MyService ();

This code creates a TransparentProxy object, which is how the component should run in production.

How can I create an instance of the class in-process and local, such that I can inject its dependencies?

like image 333
Alex Waddell Avatar asked Jul 20 '26 08:07

Alex Waddell


1 Answers

I don't like it, but you can use conditional compilation statements, Debug/Release or I use DebugComPlus/Debug/Release (where only Debug is not a ServicedComponent). Note that you should implement IDisposable in the Debug condition because you'll have to call Dispose on ServicedComponent for real code, and the Dispose method won't be there if it's not a ServicedComponent.

Otherwise I really recommend just testing it through COM+ because that's what you'll be running in real life, and you'll encounter issues with COM+ in production that you'll miss if you test outside of COM+. That's the way we had it now on one project I am on, and there were many difficult to find bugs because of that. Personally always test through COM+ now.

like image 116
Richard Anthony Freeman-Hein Avatar answered Jul 23 '26 00:07

Richard Anthony Freeman-Hein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!