1

I need to have a generic Service contract but if I do that I receive this error:

[ServiceContract]
public interface IService<T> where T : MyClass
{
    [OperationContract]
    void DoWork();
}

The contract name 'x.y' could not be found in the list of contracts implemented by the service 'z.t'.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user758977
  • 431
  • 1
  • 7
  • 19

3 Answers3

0

If you use an servicereference on thee client side generic will fail.

Use the following on client side with generic:

var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("");
var myChannelFactory = new ChannelFactory<IService>(myBinding, myEndpoint);
IService gks = myChannelFactory.CreateChannel();
Naha
  • 506
  • 6
  • 14
0

As long as you use a closed generic for your interface it does work - see below. What you cannot do is to have an open generic as the contract type.

public class StackOverflow_6216858_751090
{
    public class MyClass { }
    [ServiceContract]
    public interface ITest<T> where T : MyClass
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest<MyClass>
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    static Binding GetBinding()
    {
        BasicHttpBinding result = new BasicHttpBinding();
        //Change binding settings here
        return result;
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest<MyClass>), GetBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest<MyClass>> factory = new ChannelFactory<ITest<MyClass>>(GetBinding(), new EndpointAddress(baseAddress));
        ITest<MyClass> proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
0

Your service contract is not interoperable. It's not possible to expose generics like that via WSDL.

Take a look at this article (link) for a possible workaround.

Tad Donaghe
  • 6,625
  • 1
  • 29
  • 64
  • What if all the clients connected to this service are .Net ones? does the other person care about interop? This could be for an internal app and therefore not needed to be interoperable. Bad answer IMO – War Aug 14 '14 at 12:19