Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Generic Class

How can this work as a WCF Service?

public class BusinessObject<T> where T : IEntity
{
    public T Entity { get; set; }

    public BusinessObject(T entity)
    {
        this.Entity = entity;
    }
}

public interface IEntity { }

public class Student : IEntity
{
    public int StudentID { get; set; }
    public string Name { get; set; }
}

I want to expose the BusinessObject <T> class and the all the class that inherits the IEntity interface in the WCF Service.

My code is in C#, .NET Framework 4.0, build in Visual Studio 2010 Pro.

like image 736
John Isaiah Carmona Avatar asked Jan 24 '26 06:01

John Isaiah Carmona


1 Answers

While exposing BusinessObject to the clients via WCF, you must do that by using closed generic type.

[DataContract]
public class BusinessObject<T> where T : IEntity
{
    [DataMember]
    public T Entity { get; set; }

    public BusinessObject(T entity)
    {
        this.Entity = entity;
    }
}  

[ServiceContract]
interface IMyContract {
[OperationContract]
BusinessObject<Student> GetStudent(...) // must be closed generic
}
like image 124
chandmk Avatar answered Jan 25 '26 18:01

chandmk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!