سؤال

لدي فئة مجردة التي أود أن تكون قادرة على فضح 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.

و عند استخدام الطفل حسب نوع الخدمة أحصل على
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.

هل كانت مفيدة؟

المحلول

عقد الخدمة عادة ما تكون واجهة ، وليس فئة.مكان العقد الخاص بك في واجهة ، فئة مجردة تنفيذ هذه الواجهة و اسمحوا لنا أن نعرف ما يحدث عند استخدام الطفل لبدء تشغيل الخدمة.

تحرير:حسنا, الآن أنت بحاجة إلى تعديل RunService طريقة هذا أدناه.العقد نوع إذا 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();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top