5

I want to provide a service like:

[ServiceContract]
interface IMyService<T>
{
   [ServiceOperation]
   void Foo(T item);
}

I can discover at runtime the Type of all arguments (using some attributes decorations) of my generic contract and using something like:

typeof(IMyService<>).MakeGenericType(typeof(Contact))

I can get the bounded generic type both for the contract and service implementation. Starting from a base configuration at runtime I can also add the endpoints to the host. Even more knowing all the data contracts I am interested in, I can add to the host a DataContractResolver for each of them

The challange is in hosting the service. At compile time I don't know the type argument, but I can find it at runtime. How can I extend ServiceHost or ServiceHostBase to inject in it the service type:

MyService<Contact> 

when it is needed? ServiceHost will anyhow need it only at runtime...

komisacroS
  • 81
  • 2
  • 6
  • Are you making an endpoint per possible generic contract type (versus 1 endpoint), and are you self-hosting, or hosting in IIS? – CodingWithSpike Oct 20 '11 at 20:12
  • Possible [duplicate.](http://stackoverflow.com/questions/3503067/generic-servicecontract) – Chris Hannon Oct 20 '11 at 23:59
  • Yes, I am making an endpoint for each possible generic contract (bounded generic as Juval Lowy calls it). The issue is not about making the endpoints, but to create the host which has to have one implementations service type. I could create one ServiceHost for each possible service implementation. I'm not sure how this will work... I want to host in WAS. – komisacroS Oct 21 '11 at 08:04

2 Answers2

0

Can you not use a ServiceHostFactory?

Sajay
  • 444
  • 2
  • 6
  • Thanks for the answer. However, this is not what I am looking for... Indeed I will use ServiceHostFactory to make WAS to use my own ServiceHost, if I could somehow make a ServiceHost that hosts a generic service. – komisacroS Oct 23 '11 at 19:41
-1

I know this post is old but for others: http://www.codeproject.com/Articles/290148/Pattern-for-Creating-Generic-WCF-Services

EDIT Some code semantics from the link:

[ServiceContract]
interface IBaseService<T> where T : class, INumber
{ 
    [OperationContract]
    string GetData(int value); 
}

public class BaseService<T> : IBaseService<T>
    where T : class, INumber
{
    private T _num;

    public BaseService(INumberFactory<t> numberFactory)
    {
         _num = numberFactory.Create();
    }

    public string GetData(int value)
    {
        var ret = value * _num.Get();

        return ret.ToString();
    }
}

public class NumberOne : INumber
{
    public int Get()
    {
        return 1;
    }
}

public class NumberTwo : INumber
{
    public int Get()
    {
        return 2;
    }
}

public interface INumber
{
    int Get();
}



    public class myServiceHostFactory<TService> : System.ServiceModel.Activation.ServiceHostFactory where TService : class
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            ServiceHost host = new ServiceHost(serviceType, baseAddresses);

            host.AddServiceEndpoint(typeof(TService), new NetTcpBinding(), "");
            return host;
        }


    }
Stix
  • 485
  • 4
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Sushil Sep 29 '15 at 17:42
  • Agreed , I edited and added code semantics from the link. please do not -1 the post until I have had a chance to heed the advice. – Stix Sep 29 '15 at 18:27
  • I am not the downvoter. a lot of people review the answers so someone must have down voted your answer. – Sushil Sep 29 '15 at 18:46