【发布时间】: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