Unit Testing objects registered with PerWCFOperation

103 views
Skip to first unread message

Scott Ferguson

unread,
May 27, 2015, 7:02:55 PM5/27/15
to castle-pro...@googlegroups.com
Hello,

I have a WCF application and all objects are registered with a LifeStyle of PerWCFOperation.

This works great in Production, and support all my concurrency needs.

The issue is I have Integration Tests that can no longer access the Service classes, yielding an exception of:

    Could not obtain scope for component "FooService". This is most likely either a bug in custom IScopeAccessor or you're trying to access scoped component outside of the scope (like a per-web-request component outside of web request etc)

I'd rather not register my classes with a different LifeStyle for unit tests, my DbContext is being shared by Repo's and a UoW object, and NUnit runs the tests in parallel.

Right now, the base class does the access like so (this is the only place I explicitly resolve in the entire app...in Production, everything is constructor injection from the root of the application):

    public class ServiceIntegrationTestBase<T> : ServiceIntegrationTestBase
    {
        protected T CUT;

        [TestFixtureSetUp]
        protected void Setup()
        {
            // Fails when executing this line:
            CUT = IocContainer.Resolve<T>();
        }

        [TestFixtureTearDown]
        protected void TearDown()
        {
            IocContainer.Release(CUT);
        }
    }

How do I access the WCF Service classes from my unit tests? I feel like I need to get backed up to the DefaultServiceHostFactory, and simulate a WCF call, but haven't been able to get it yet.

Scott Ferguson

unread,
Jun 1, 2015, 11:13:12 PM6/1/15
to castle-pro...@googlegroups.com
I came up with a solution, for anyone that may stumble upon this in the future.

Basically, I fire up a true instance of the web service when the Integration Tests startup. Then I create a Channel and set CUT to that. None of my tests had to change, just changed the base.

It's also nice, b/c NUnit is firing many concurrent requests at the service at once, so it's inherently testing concurrency against a true instance of the service.


        private void Initialize()
        {
            if (_isInitialized)
                return;

            lock (SyncLock)
            {
                if (_isInitialized)
                    return;

                // Register everything with IoC, PerWCFOperation
                Application.Startup();

                // localhost:8733 is a post left open for debugging (http://stackoverflow.com/questions/885744/wcf-servicehost-access-rights)
                var baseAddress = new Uri("http://localhost:8733/Design_Time_Addresses/" + typeof(T).Name);

                var serviceHost = new DefaultServiceHostFactory(IocContainer.Instance.Kernel).CreateServiceHost(typeof(T).AssemblyQualifiedName, new Uri[] { baseAddress });

                var serviceEndPoint = serviceHost.AddServiceEndpoint(typeof(T).FullName, new BasicHttpBinding(), baseAddress);

                serviceHost.Open();

                CUT = new ChannelFactory<T>(serviceEndPoint).CreateChannel();

                _isInitialized = true;
            }
        }
Reply all
Reply to author
Forward
0 new messages