简体   繁体   中英

Error when calling Polymorphic method on WCF Client

This error comes as result of This previous question. I'm trying to call a polymorphic method from WCF client. This is my contract:

public interface IOhmioService
{        
  [OperationContract]
  IEnumerable<Enumerador> GetEnumerador<T>() where T : IEnumerador, new();        
}

This is my class implementation:

public class OhmioService : IOhmioService
{
  public IEnumerable<Enumerador> GetEnumerador<T>() where T : IEnumerador, new()
  {
      T _obj = new T();
      return _obj.Enumerar();  
  }
}

And call it from client like this:

public IEnumerable<Enumerador> Clients { get; set; } 
Clients = this.serviceClient.GetEnumerador<Clientes>();

If i call this method from within the class everything works fine. But if i call it from WCF client a get this error:

The non generic Method 'Ohmio.Client.OhmioService.OhmioServiceClient.GetEnumerador()' cannot be used with types arguments

What i'm i doing wrong? Thanks!

UPDATE

Ok. I try suggested solution, and get this Horrible error:

Type 'System.RuntimeType' wasn't spected with the contract name RuntimeType: http://schemas.datacontract.org/2004/07/System '. Trye to use DataContractResolver or add the unknown types staticaly to the list of known types (for instance, using the attribute KnownTypeAttribute or adding them to the list of known types to pass to DataContractSerializer)

Maybe using generic types over wcf is not such a good idea after all. I was trying to reduce repetitive code on the WCF service.

You cannot have generic OperationContract methods in a WCF ServiceContract . See here for further details: WCF. Service generic methods

You need to pass the type as a method parameter:

public interface IOhmioService
{        
  [OperationContract]
  IEnumerable<Enumerador> GetEnumerador(string typeName);       
}

public class OhmioService : IOhmioService
{
  public IEnumerable<Enumerador> GetEnumerador(string typeName)
  {
      var type = Type.GetType(typeName);
      var _obj = (IEnumerador)Activator.CreateInstance(type);
      return _obj.Enumerar();  
  }
}

UPDATE

See update above; pass the fully qualified name of the type. That won't cause a serialization issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM