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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:14:47+00:00 2026-05-28T03:14:47+00:00

I have an abstract class that I use to set default values in a

  • 0

I have an abstract class that I use to set default values in a WCF proxy. I want all my other proxies to derive from this class passing in the service client that was generated when they added a service reference. Therefore I have the following

public abstract class ProxyFactory<T> where T : new()
{
    protected BasicHttpBinding BasicHttpBinding = new BasicHttpBinding();

    public abstract T GetTranslationServiceClient();

    protected ProxyFactory()
    {
        BasicHttpBinding.Security.Mode = BasicHttpSecurityMode.None;
    }

    protected T CreateClientForServiceAtEndpoint(string uri)
    {
        var address = new EndpointAddress(uri);
        return new T(BasicHttpBinding, address);
    }
}

So for instance if you had a project and added a service reference to WCF service called MyService you would have a generated class called MyServiceClient. Then I would create a new class called MyServiceProxy that would derive from this class where T would be of type MyServiceClient. This would allow me to have all my common configuration in done in the base and the only code I would need to write is an override for the CreateClientForServiceAtEndpoint with the url of the service.

public override MyServiceImplClient GetMyServiceClient()
{
    return CreateClientForServiceAtEndpoint("http://localhost/MyServices/MyServiceImpl.svc");
}

The this derived class will be used as a factory so calling clients would create a new instance and invoke CreateClientForServiceAtEndpoint to get a preconfigured WCF proxy

The problem is the line return new T(BasicHttpBinding, address); in the CreateClientForServiceAtEndpoint method of the abstract class. It will not allow me to pass the parameters to the constructor. How can this be done?

  • 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-28T03:14:48+00:00Added an answer on May 28, 2026 at 3:14 am

    In this case you need to create your returned instance with Activator.CreateInstance:

    protected T CreateClientForServiceAtEndpoint(string uri)
    {
        var address = new EndpointAddress(uri);
        return (T) Activator.CreateInstance(typeof(T), BasicHttpBinding, address);
    }
    

    And there is no need for the where T : new() restriction.

    EDIT: After Marc’s comment

    Because there is now why to say anything about a T‘s constructor (other then new()) the Activator.CreateInstance can throw if the supplied T does not have the right constructor.

    But you can add some additional guide to your API users with the
    creation of an abstract base class:

    public abstract class ServiceImplClientBase
    {
        public ServiceImplClientBase(BasicHttpBinding basicHttpBinding, EndpointAddress endpointAddress)
        {
            //...
        }
    }
    

    And use it as the type restriction: where T : ServiceImplClientBase. But it can just provide a hint for the need of the constructor because derived types can declare the base constructor as private or protected.

    • 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 I want to use to store properties for another
I have an abstract generic view-model that I use as a base-class for several
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class that has a virtual method. The method is virtual
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
Ok so I have an abstract base class called Product, a KitItem class that
What if I have a class that both extends an abstract class and implements
I have a class that inherits from Page, called APage. public abstract class APage:
If I have a base class such that public abstract class XMLSubscription <T extends

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.