Pergunta

Eu tenho uma classe abstrata que eu gostaria de ser capaz de expor a WCF para que quaisquer subclasses também será capaz de ser iniciado como um serviço WCF.
Isto é o que eu tenho até agora:

[ServiceContract(Name = "PeopleManager", Namespace = "http://localhost:8001/People")]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[DataContract(Namespace="http://localhost:8001/People")]
[KnownType(typeof(Child))]
public abstract class Parent
{
    [OperationContract]
    [WebInvoke(Method = "PUT", UriTemplate = "{name}/{description}")]
    public abstract int CreatePerson(string name, string description);

    [OperationContract]
    [WebGet(UriTemplate = "Person/{id}")]
    public abstract Person GetPerson(int id);
}

public class Child : Parent
{
    public int CreatePerson(string name, string description){...}
    public Person GetPerson(int id){...}
}

Ao tentar criar o serviço no meu código eu uso este método:

public static void RunService()
{
    Type t = typeof(Parent); //or typeof(Child)
    ServiceHost svcHost = new ServiceHost(t, new Uri("http://localhost:8001/People"));
    svcHost.AddServiceEndpoint(t, new BasicHttpBinding(), "Basic");
    svcHost.Open();
}

Ao usar o Pai como o tipo de serviço I get
The contract name 'Parent' could not be found in the list of contracts implemented by the service 'Parent'. OU Service implementation type is an interface or abstract class and no implementation object was provided.

e quando utilizar o Criança como o tipo de serviço I get
The service class of type Namespace.Child both defines a ServiceContract and inherits a ServiceContract from type Namespace.Parent. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, then another service class cannot derive from it.

Existe uma maneira para expor as funções na classe Child então eu não ter que adicionar especificamente os atributos WCF?

Editar
Portanto, este

[ServiceContract(Name= "WCF_Mate", Namespace="http://localhost:8001/People")]  
    public interface IWcfClass{}  

    public abstract class Parent : IWcfClass {...}  
    public class Child : Parent, IWcfClass {...}

com o início do serviço com retornos crianças
The contract type Namespace.Child is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute.

Foi útil?

Solução

O contrato de serviço é geralmente uma interface, não uma classe. Coloque o seu contrato em uma interface, temos a classe abstrata implementar essa interface, e deixe-nos saber o que acontece quando você usa crianças para iniciar o serviço.

Edit: Ok, agora você precisa modificar seu método RunService a esta abaixo. O tipo de contrato se IWcfClass, não filho ou pai.

public static void RunService()
{
        Type t = typeof(Child);
        ServiceHost svcHost = new ServiceHost(t, new Uri("http://localhost:8001/People"));
        svcHost.AddServiceEndpoint(typeof(IWcfClass), new BasicHttpBinding(), "Basic");
        svcHost.Open();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top