简体   繁体   English

在WCF客户端上调用多态方法时出错

[英]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. 我正在尝试从WCF客户端调用多态方法。 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: 但是,如果我从WCF客户端调用它,则会收到此错误:

The non generic Method 'Ohmio.Client.OhmioService.OhmioServiceClient.GetEnumerador()' cannot be used with types arguments 非通用方法'Ohmio.Client.OhmioService.OhmioServiceClient.GetEnumerador()'不能与类型参数一起使用

What i'm i doing wrong? 我做错了什么? Thanks! 谢谢!

UPDATE 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 '. 未使用合同名称RuntimeType指定类型“ System.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) 尝试使用DataContractResolver或将未知类型静态地添加到已知类型列表中(例如,使用属性KnownTypeAttribute或将它们添加到已知类型列表中以传递给DataContractSerializer)

Maybe using generic types over wcf is not such a good idea after all. 毕竟,在wcf上使用泛型类型并不是一个好主意。 I was trying to reduce repetitive code on the WCF service. 我试图减少WCF服务上的重复代码。

You cannot have generic OperationContract methods in a WCF ServiceContract . WCF ServiceContract不能有通用的OperationContract方法。 See here for further details: WCF. 有关更多详细信息,请参见此处: 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 UPDATE

See update above; 见上面的更新; pass the fully qualified name of the type. 传递类型的全限定名。 That won't cause a serialization issue. 这不会导致序列化问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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