Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:52+00:00 2026-06-10T16:36:52+00:00

I’ve got a windows service that is Opening two WCF services. I want to

  • 0

I’ve got a windows service that is Opening two WCF services. I want to unit test OnStart() and assert that service1.Open() and service2.Open() is being called. OnStart() looks like this:

protected override void OnStart(string[] args)
{
    // host WCF services            
    _service1.Open();           
    _service2.Open();
}

I’m injecting the services in a constructor overload as such:

public WinService(ServiceHostBase service1, 
                                  ServiceHostBase service2)
{
    _service1 = service1;
    _service2 = service2;
    InitializeComponent();
}

I’m using RhinoMocks to generate a Stub of ServiceHostBase like this:

[TestMethod()]
public void WinServiceOnStartCallsDependenciesAsExpected()
{
    ServiceHostBase service1 = MockRepository.GenerateStub<ServiceHostBase>();
    ServiceHostBase service2 = MockRepository.GenerateStub<ServiceHostBase>();
    WinService target = new WinService(service1, service2);
    WinService_Accessor privateTarget = new WinService_Accessor(new PrivateObject(target));     
    privateTarget.OnStart(null);

When my test calls OnStart(), I get a null reference exception when it calls service1.Open(). I’ve confirmed that service1 is a mocked object at that point and that it is Open() that is throwing the null. I know that Open() is actually a method on System.ServiceModel.Channels.CommunicationObject and I tried Stubbing or Mocking that as well, but I still get the object ref error. It’s not a virtual method, so I would think it’s just not being overridden by the mocked version, but when I try to set up an Expectation reportservice.Stub(r => r.Open()), I get a different exception about there not being a default timeout, as if it’s running the actual CommunicationObject Open() method instead of something RhinoMocky that is throwing the null ref.

All this to say, I’m just looking for help on how to confirm that Open() is being called on my ServiceHost in a Unit Test. =]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:36:53+00:00Added an answer on June 10, 2026 at 4:36 pm

    ServiceHostBase.Open calls CommunicationObject.Open under the hood. Which, as part of its implementation, does a lot of different things – like verifying object’s state, creating other objects, calling methods, properties and so forth. And since it’s not virtual (your assumption was correct), Rhino will call base class implementation.

    To make it work, you’ll probably have to mock and stub lot of CommunicationObject dependencies and it still will not be certain whether you’ll succeed or not (some types/methods might simply be unmockable by Rhino, think static, sealed or other non-virtual). This is why you should either:

    1. Leave this kind of testing to integration tests; you’ll then have .Open tested in end-user environment
    2. Introduce a wrapper around ServiceHostBase and pass it as dependency by interface; this involves extra work (new interface and simple wrapper class), but allows you to do exactly what you wanted

    Keep in mind that adding wrapper will only delegate problem further (to the wrapper class’ Open method, which essentially will call ServiceHostBase.Open – should you unit test that too?). On the other hand, integration tests might not catch issues as fast as unit tests (assuming you run them less often). Depending on how critical you consider OnOpen to be, which way you chose is more or less a judgement call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I want to show the soap response to UIWebview.. my soap response is, <p><img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.