1、总是将ServiceContractAttribute特性应用到接口上,而不是类上
[OperationContract]
public void MyMethod()
{...}
[OperationContract]
void MyMethod();
public void MyMethod()
{...}
[OperationContract]
string GetName();
[OperationContract]
void SetName(string name);
//避免:
[ServiceContract]
class MyService
{
}
//正确:
[ServiceContract]
interface IMyContract
{
}
class MyService:IMyContract
{
}
2、服务契约要添加前缀I:
[ServiceContract]
interface IMyContract
{...}
3、避免准属性(Property-Like)操作
//避免
[ServiceContract]
interface IMyContract
{
}
4、避免定义只有一个成员的契约
5、每个服务契约最好只定义三到五个成员
6、每个服务契约的成员不要超过20个。12个是可能的实际限定
本文介绍了WCF服务契约设计的最佳实践,包括将ServiceContract应用于接口而非类、为服务契约添加前缀I、避免使用准属性操作等。同时,还提到了关于服务契约成员数量的规定,以确保良好的服务设计。

被折叠的 条评论
为什么被折叠?



