WCF 中的服务契约





0/5 (0投票)
服务契约是指服务向其使用者公开其能力和需求的集体机制。我们必须说明
服务契约是指服务向其使用者公开其能力和需求的集体机制。它定义了服务在执行时将执行的操作。它还说明了服务的更多信息,例如消息数据类型、操作位置以及客户端与服务通信所需的协议。
有三种类型的属性用于标注这些类型的操作。
1. ServiceContractAttribute
2. OperationContractAttribute
3. MessageParameterAttribute。
ServiceContractAttribute:它用于将类型声明为服务契约。它可以不带任何参数声明,也可以接受命名参数。
[ServiceContract(Name="MyService", Namespace="http://tempuri.org")]
public interface IMyService
{
[OperationContract]
int AddNum(string numdesc, string assignedTo);
}
OperationContractAttribute: 它只能应用于方法。它用于声明属于服务契约的方法。它控制服务描述和消息格式。
MessageParameterAttribute.: 它控制操作参数和返回值的名称在服务描述中显示的方式。它控制参数和返回值如何在传输层序列化为 XML 请求和响应元素。我们需要使用 Name 属性,因为不能将变量名用作编程语言。
[OperationContract]
[return : MessageParameter(Name="reswait")]
string MyOp([MessageParameter(Name="string")]string s);