Ich habe eine abstrakte Klasse, die ich WCF verfügbar machen möchte, damit alle Unterklassen auch als WCF-Dienst gestartet werden können.
Dies ist, was so weit ich habe:Verfügbarmachen von WCF-Unterklassen basierend auf einer abstrakten Klasse
[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){...}
}
Beim Versuch, den Dienst in meinem Code erstellen Ich benutze diese Methode:
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();
}
Wenn Parent als die Art des Dienstes unter Verwendung von I
bekommen The contract name 'Parent' could not be found in the list of contracts implemented by the service 'Parent'.
OR Service implementation type is an interface or abstract class and no implementation object was provided.
und wenn das Kind als die Art des Dienstes mit erhalte ich 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.
Gibt es eine Möglichkeit, die Funktionen in der Child-Klasse verfügbar zu machen, so muss ich die WCF-Attribute nicht speziell hinzufügen?
bearbeiten
Also die
[ServiceContract(Name= "WCF_Mate", Namespace="http://localhost:8001/People")]
public interface IWcfClass{}
public abstract class Parent : IWcfClass {...}
public class Child : Parent, IWcfClass {...}
mit dem Service mit Kindern Start zurück
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.
Wenn Parent IWcfClass implementiert und Child Parent ausdehnt, muss Child auch IWcfClass nicht implementieren. –