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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:18:24+00:00 2026-05-13T13:18:24+00:00

I’m trying to write some code to help unit test WCF services. These services

  • 0

I’m trying to write some code to help unit test WCF services. These services are accessed through a facade class that creates the proxy instance, then calls the proxy method and returns the result; for each proxy method. I’d like to be able to replace the current creation code with something that either creates the real service or a fake one.

I couldn’t get that to work. I boiled it down to the following:

using System.ServiceModel;

namespace ExpressionTrees
{
    public interface IMyContract
    {
        void Method();
    }

    public class MyClient : ClientBase<IMyContract>, IMyContract
    {
        public MyClient()
        {
        }

        public MyClient(string endpointConfigurationName)
            : base(endpointConfigurationName)
        {
        }

        public void Method()
        {
            Channel.Method();
        }
    }

    public class Test
    {
        public TClient MakeClient<TClient>()
            where TClient : ClientBase<IMyContract>, IMyContract, new()
        {
            return new MyClient("config");

            // Error:
            // Cannot convert expression of type 'ExpressionTrees.ServiceClient' to return type 'TClient'
        }
    }
}

Why is it that, even though the MyClient class derives from ClientBase<IMyContract> and implements IMyContract, that I can’t return a MyClient instance from a method meant to return a TClient? TClient specifies a type constraint I had thought meant the same thing.


My goal was to call code like this:

    public void CallClient<TClient>()
        where TClient : ClientBase<IMyContract>, IMyContract
    {
        TClient client = null;
        bool success = false;
        try
        {
            client = MakeClient<TClient>();
            client.Method();
            client.Close();
            success = true;
        }
        finally
        {
            if (!success && client != null)
            {
                client.Abort();
            }
        }
    }

But instead of always calling MakeClient<TClient>, I wanted to be able to have a unit test inject a mock object. Since the code above depends both on ClientBase<IMyContract>, IMyContract, it seems I was trying to “synthesize” a generic class that would satisfy that constraint.

In retrospect, this wouldn’t make sense. As an example, the ClientBase<IMyContract> would expect to be instantiated in such a way that a Channel object would be constructed that it could then delegate the Close method to.

I’ve wound up punting on having the exact same code run both for the real and for the fake services. I’m now injecting an IMyService, and either calling IMyService.Method or client.Method depending on whether my injected property is null.

  • 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-13T13:18:25+00:00Added an answer on May 13, 2026 at 1:18 pm

    Basically your code boils down to:

        public static T MakeFruit<T>() where T : Fruit 
        { 
            return new Apple(); 
        } 
    

    This always returns an apple, even if you call MakeFruit<Banana>(). But MakeFruit<Banana>() is required to return a banana, not an apple.

    The meaning of the generic type constraint is that the type argument provided by the caller must match the constraint. So in my example, you can say MakeFruit<Banana>() but not MakeFruit<Tiger>() because Tiger does not match the constraint that T must be convertible to Fruit. I think you believe that the constraint means something else; I’m not sure what that is.

    Think about it like this. A formal parameter has a formal parameter type. The formal parameter type restricts the type of the expression that is used as an argument. So when you say:

    void M(Fruit x)
    

    you are saying “the argument passed for formal parameter x in M must be convertible to Fruit”.

    Generic type parameter constraints are exactly the same; they are restrictions on what type arguments may be passed for the generic type parameters. When you say “where T : Fruit”, that is just the same as saying (Fruit x) in the formal parameter list. T has got to be a type that goes to Fruit, just as the argument for x has got to be an argument that goes to Fruit.

    Why do you even want to have a generic method in the first place? I don’t understand what exactly you’re trying to model with this method or why you want it to be generic.

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

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following Func<string,IEnumerable<Guid>> lambda = filter => ( _map… May 13, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer I would advise you to avoid creating JSON by hand… May 13, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer Options like driving directions can be achieved by adding links… May 13, 2026 at 6:18 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.