2009-03-05 25 views
4

抽象クラスをWCFに公開して、サブクラスもWCFサービスとして開始できるようにしたいと思います。これは私がこれまで持っているものです
抽象クラスに基づくWCFサブクラスの公開

[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){...} 
} 

私はこの方法を使用私のコードでサービスを作成しようとしている:私は
を得る

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(); 
} 

サービスの種類として親を使用してThe contract name 'Parent' could not be found in the list of contracts implemented by the service 'Parent'. または Service implementation type is an interface or abstract class and no implementation object was provided.

サービスの種類としてChildを使用すると、
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.

具体的にWCF属性を追加する必要はないので、Childクラスの関数を公開する方法はありますか?

編集
だから、この

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

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

子でサービスを開始するとのサービス契約は通常、インターフェースではなくクラスである
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.

+0

ParentがIWcfClassを実装し、ChildがParentを拡張する場合、ChildはIWcfClassも実装する必要はありません。 –

答えて

8

を返します。契約をインタフェースに置き、抽象クラスにこのインタフェースを実装させ、Childを使用してサービスを開始するときに何が起こるかをお知らせください。

編集:これで、RunServiceメソッドを以下に変更する必要があります。 IWcfClassの場合は契約タイプ、ChildまたはParentではありません。

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(); 
} 
+0

入力していただきありがとうございますが、正しく実装していない限り、まだ動作しません。私は編集の質問に変更を加えましたので、読みやすくなりました。 – bju1046

+0

[OperationContract]および[Web *]属性も、親クラスまたは子クラスではなく、名前空間にある必要があります。 – bju1046

関連する問題