4

サブクラスも 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'. OR が得られますService implementation type is an interface or abstract class and no implementation object was provided.

そして、私が得るサービスのタイプとして子を使用するとき
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 属性を具体的に追加する必要がないように、子クラスで関数を公開する方法はありますか?

編集
だからこれ

[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.

4

1 に答える 1

8

通常、サービス コントラクトはクラスではなくインターフェイスです。コントラクトをインターフェイスに配置し、抽象クラスにこのインターフェイスを実装させ、Child を使用してサービスを開始するとどうなるかをお知らせください。

編集: OK、RunService メソッドを以下のように変更する必要があります。Child または Parent ではなく、IWcfClass の場合はコントラクト タイプ。

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();
}
于 2009-03-05T21:59:09.060 に答える