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 6101053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:27:23+00:00 2026-05-23T13:27:23+00:00

I’m using NUnit and Moq to test a class which uses a generic WCF

  • 0

I’m using NUnit and Moq to test a class which uses a generic WCF service client wrapper I wrote, and I’ve run into an error I can’t figure out. I have the following interface:

public interface IService
{
    void Call();
}

…implemented by the following service client:

public class ServiceClient : ClientBase<IService>, IService
{
    public void Call()
    {
    }
}

…and wrapped by this class with the following generic constraints:

public class ServiceClientWrapper<TClient, TService>
    where TClient : ClientBase<TService>, TService
    where TService : class
{
    public virtual TService CreateServiceClient()
    {
        return (TService)Activator.CreateInstance<TClient>();
    }
}

To make it testable I have a wrapper factory which I can mock. The wrapper factory interface is this:

public interface IServiceClientWrapperFactory
{
    ServiceClientWrapper<TClient, TService>
        CreateServiceClientWrapper<TClient, TService>()
        where TClient : ClientBase<TService>, TService
        where TService : class;
}

I test this set up using this code:

// A mock IService to return from my mock service wrapper:
var mockService = new Mock<IService>();

// A mock client wrapper to return from my mock wrapper factory:
var mockClientWrapper = 
    new Mock<ServiceClientWrapper<ServiceClient, IService>>();
mockClientWrapper
    .Setup(mcw => mcw.CreateServiceClient())
    .Returns(mockService.Object);

// A mock wrapper factory to inject into a client object:
var mockClientWrapperFactory = new Mock<IServiceClientWrapperFactory>();
mockClientWrapperFactory
    .Setup(mcwf => mcwf.CreateServiceClientWrapper<ServiceClient, IService>())
    .Returns(mockClientWrapper.Object);

// Get the mock client wrapper from the mock wrapper factory - boom!
mockClientWrapperFactory.Object
    .CreateServiceClientWrapper<ServiceClient, IService>();

The error is:

GenericArguments[0], ‘TService’, on
‘ServiceClientWrapper`2[TClient,TService]’
violates the constraint of type
parameter ‘TClient’.

The constraints are the same wherever I’ve stated them, it compiles and runs just fine, the error isn’t thrown if I implement IServiceClientWrapperFactory and run it without Moq… any ideas?

  • 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-23T13:27:24+00:00Added an answer on May 23, 2026 at 1:27 pm

    I played with the code a bit, and I get the same result. As far as I can tell, there isn’t anything wrong with your definitions.

    It seems that it might be a bug in Moq, when implementing mocks that have various combinations of generic constraints on them.

    I tried removing various constraints (and commenting out or modifying code, as required). It seems if I remove TService from where TClient : ... , TService, then I don’t get this error.

    This seems further corroborated by the fact that there have been multiple similar bugs in the past:

    • http://code.google.com/p/moq/issues/detail?id=127
    • http://code.google.com/p/moq/issues/detail?id=177

    FYI, the version I used to repro your problem was: Moq.4.0.10827, NET40 (not NET40-RequiresCastle)

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a text area in my form which accepts all possible characters from

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.