【问题标题】:Exposing WCF subclasses based on an abstract class基于抽象类公开 WCF 子类
【发布时间】:2009-03-05 21:55:39
【问题描述】:

我有一个抽象类,我希望能够将其公开给 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();
}

当使用 Parent 作为我得到的服务类型时
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.

有没有办法在 Child 类中公开函数,这样我就不必专门添加 WCF 属性?

编辑
所以这个

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

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

使用 Child 返回启动服务
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.

【问题讨论】:

  • 如果 Parent 实现了 IWcfClass,Child 扩展了 Parent,那么 Child 也不需要实现 IWcfClass。

标签: c# wcf abstract-class


【解决方案1】:

服务契约通常是一个接口,而不是一个类。将你的契约放到一个接口中,让抽象类实现这个接口,并让我们知道当你使用 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();
}

【讨论】:

  • 感谢您的输入,但它仍然不起作用,除非我没有正确实现它。我在编辑中添加了对问题的修改,以便于阅读。
  • 还有 [OperationContract] 和 [Web*] 属性需要在命名空间中,而不是在父类或子类中。
猜你喜欢
  • 2018-12-01
  • 1970-01-01
  • 2021-02-21
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多