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

  • Home
  • SEARCH
  • 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 826681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:27:12+00:00 2026-05-15T03:27:12+00:00

I have a service proxy class that makes asyn call to service operation. I

  • 0

I have a service proxy class that makes asyn call to service operation. I use a callback method to pass results back to my view model.

Doing functional testing of view model, I can mock service proxy to ensure methods are called on the proxy, but how can I ensure that callback method is called as well?

With RhinoMocks I can test that events are handled and event raise events on the mocked object, but how can I test callbacks?

ViewModel:

public class MyViewModel
{
    public void GetDataAsync()
    {
        // Use DI framework to get the object
        IMyServiceClient myServiceClient = IoC.Resolve<IMyServiceClient>();
        myServiceClient.GetData(GetDataAsyncCallback);
    }

    private void GetDataAsyncCallback(Entity entity, ServiceError error)
    {
        // do something here...
    }

}

ServiceProxy:

public class MyService : ClientBase<IMyService>, IMyServiceClient
{
    // Constructor
    public NertiAdminServiceClient(string endpointConfigurationName, string remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    // IMyServiceClient member.
    public void GetData(Action<Entity, ServiceError> callback)
    {
        Channel.BeginGetData(EndGetData, callback);
    }

    private void EndGetData(IAsyncResult result)
    {
        Action<Entity, ServiceError> callback =
            result.AsyncState as Action<Entity, ServiceError>;

        ServiceError error;
        Entity results = Channel.EndGetData(out error, result);

        if (callback != null)
            callback(results, error);
    }
}

Thanks

  • 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-05-15T03:27:13+00:00Added an answer on May 15, 2026 at 3:27 am

    Played around with this a bit and I think I may have what you’re looking for. First, I’ll display the MSTest code I did to verify this:

    [TestClass]
    public class UnitTest3
    {
        private delegate void MakeCallbackDelegate(Action<Entity, ServiceError> callback);
    
        [TestMethod]
        public void CallbackIntoViewModel()
        {
            var service = MockRepository.GenerateStub<IMyServiceClient>();
            var model = new MyViewModel(service);
    
            service.Stub(s => s.GetData(null)).Do(
                new MakeCallbackDelegate(c => model.GetDataCallback(new Entity(), new ServiceError())));
            model.GetDataAsync(null);
        }
    }
    
    public class MyViewModel
    {
        private readonly IMyServiceClient client;
    
        public MyViewModel(IMyServiceClient client)
        {
            this.client = client;
        }
    
        public virtual void GetDataAsync(Action<Entity, ServiceError> callback)
        {
            this.client.GetData(callback);
        }
    
        internal void GetDataCallback(Entity entity, ServiceError serviceError)
        {
    
        }
    }
    
    public interface IMyServiceClient
    {
        void GetData(Action<Entity, ServiceError> callback);
    }
    
    public class Entity
    {
    }
    
    public class ServiceError
    {
    }
    

    You’ll notice a few things:

    1. I made your callback internal. You’ll need to use the InternalsVisisbleTo() attribute so your ViewModel assembly exposes internals to your unit tests (I’m not crazy about this, but it happens in rare cases like this).

    2. I use Rhino.Mocks “Do” to execute the callback whenever the GetData is called. It’s not using the callback supplied, but this is really more of an integration test. I assume you’ve got a ViewModel unit test to make sure that the real callback passed in to GetData is executed at the appropriate time.

    3. Obviously, you’ll want to create mock/stub Entity and ServiceError objects instead of just new’ing up like I did.

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

Sidebar

Related Questions

i have a class that uses a proxy class inside to call a service
I am designing a Proxy Service sequence that includes an XQuery transformation. I have
I have a windows service written in C# that acts as a proxy for
I currently have service classes that look something like this public class UserService :
I have a service app that creates AppDomain's during the course of its use
I have a WCF service which exposes a method that returns an array of
I have a Silverlight client that I need to call a web service. The
I have a web service and multiple clients that will use it. A MVC
I have a service that reads real-time sensor data. The data is read in
We have a service that has some settings that are supported only over net.tcp.

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.