Service contracts describe the functional operations implemented by the service. A service contract maps the class methods of the .NET type to WSDL services, port types , and operations. Operation contracts within service contracts describe the service operations, which are methods that that implement functions of the service.
[AttributeUsage(AttributeTargets.Interface
| AttributeTargets.Class, Inherited = false)]
public sealed class ServiceContractAttribute : Attribute
{
public string Name
{ get; set; }
public string Namespace
{ get; set; }
// More
}
This attribute allows you to define a service contract. You can apply the attribute on an interface or class.
[ServiceContract]
public interface IUsersService
{
/// <summary>
/// Get User existence in membership
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
[OperationContract]
string
GetExistingMemberEmail(string email);
/// <summary>
/// Get new password
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
[OperationContract]
string
GetNewPassword(string username);
}
public class UsersService : IUsersService
{
/// <summary>
/// Get User email from membership
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
public string GetExistingMemberEmail(string email)
{
string
UName = null;
UName = Membership.GetUserNameByEmail(email);
return
UName;
}
/// <summary>
/// Get new password
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
public string GetNewPassword(string
username)
{
bool flag = false;
MembershipUser
user=Membership.GetUser(username);
flag=user.UnlockUser();
if
(flag)
return
user.ResetPassword();
else
return
"Password can't retrive yet, Please. contact
administrator";
}
}
You can apply the OperationContract attribute only on methods, not on properties, indexers, or events, which are CLR concepts. WCF only understands operations logical functions and the OperationContract attribute exposes a contract method as a logical operation to perform as part of the service contract. Other methods on the interface (or class) that do not have the OperationContract attribute will not be part of the contract.
Note : The client can never use the service class directly and must always go through a proxy.
Using multiple contracts: A single single class can support multiple contracts by deriving and implementing multiple interfaces decorated with the ServiceContract attribute :
[ServiceContract]
public interface IVideosService
{
/// <summary>
/// Get most recent videos
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
[OperationContract]
DataTable
GetRecentVideos(int VideoID, int UserID);
}
[ServiceContract]
public interface IUsersService
{
/// <summary>
/// Get User existence in membership
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
[OperationContract]
string
GetExistingMemberEmail(string email);
/// <summary>
/// Get new password
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
[OperationContract]
string
GetNewPassword(string username);
}
public class UsersService : IUsersService
, IVideosService
{
/// <summary>
/// Get User email from membership
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
public string GetExistingMemberEmail(string email)
{
string UName = null;
UName = Membership.GetUserNameByEmail(email);
return
UName;
}
/// <summary>
/// Get new password
/// </summary>
/// <param
name="Use"></param>
/// <returns></returns>
public string GetNewPassword(string
username)
{
bool
flag = false;
MembershipUser
user=Membership.GetUser(username);
flag=user.UnlockUser();
if
(flag)
return
user.ResetPassword();
else
return
"Password can't retrive yet, Please. contact
administrator";
}
public DataTable GetRecentVideos
(int VideoID, int
UserID)
{
using (var Context = new UserServiceEntities())
{
DataTable
dt = new DataTable();
var
Video = Context.Videos.Where(c =>c.UserID &&
c.VideoID).OrderByDescending(c =>
c.AddedDate).Take(10).ToList().CopyToDataTable();
return
dt;
}
}
}