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





On Wednesday, May 27, 2015 at 7:02:55 PM UTC-4, Scott Ferguson wrote:
>
> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to