Contract Inheritance

Service contract interfaces can derive from each other, enabling you to define a hierarchy

of contracts. However, the ServiceContract attribute is not inheritable:

[AttributeUsage(Inherited = false,...)]

public sealed class ServiceContractAttribute : Attribute

{…}

Consequently, every level in the interface hierarchy must explicitly have the Service Contract attribute.

Service-side contract hierarchy

[ServiceContract]

interface IUser

{

[OperationContract]

string AddUser(string UserName, string Pass,string email, string firstName, string lastName)

}

[ServiceContract]

interface IAdminUser : IUser

{

[OperationContract]

string DeleteUser(int UserID);

}

When it comes to implementing a contract hierarchy, a single service class can implement

the entire hierarchy, just as with classic C# programming:

class User : IAdminUser

{

public string AddUser(string UserName, string Pass,string email, string firstName, string lastName)

{

try

{

using (var Context=new MyEntities())

var user = Context.Users.Where(c => c.UserName == UserName).FirstOrDefault();

if (user != null)

return "Duplicate Username.";

else

{

Users Us = new Users();

Us.UserName = UserName;

Us.Password = Pass;

Us.Email = email;

Us.FirstName = firstName;

Us.LastName = lastName;

Us.AddedDate = DateTime.Now;

Us.Guid = Guid.NewGuid().ToString();

Context.AddToUsers(Us);

Context.SaveChanges();

return Us.UserID.ToString();

}

}

catch (Exception e)

{

return e.Message;

}

}

public string DeleteUser(int UserID)

{

try

{

using (var Context = new MyEntities())

{

var user = Context.Users.Where(c => c.UserName == UserName).FirstOrDefault();

if (user != null)

user.IsDeleted = true;

Context.SaveChanges();

return user.UserName ;

}

}

}

catch (Exception e)

{

return e.Message;

}

}

The host can expose a single endpoint for the bottommost interface in the hierarchy:

<service name = "User">

<endpoint

address = "http://localhost:8001/User/"

binding = "basicHttpBinding"

contract = "IAdminUser" />

</service>