Should I apply service attributes to the interface, to the concrete class, or to both?
I am implementing a .Net WCF service and I want to apply a namespace for future versioning. A coworker ment开发者_开发知识库ioned that I ought to apply several attributes to both the interface and the concrete implementation class for that interface, but it is not clear to me 1) if this is correct, and 2) assuming it is correct, why it would be important to apply the attributes in both places.
[ServiceContract]
[ServiceBehavior(Namespace = Constants.NameSpace1_0)]
[WebService(Namespace = Constants.NameSpace1_0, Name = "MyService1_0")]
[WebServiceBindingAttribute(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true)]
public interface IMyService
{
[OperationContract]
[OperationContract(Name = "MyMethod", Action = Constants.NameSpace1_0 + "/IMyService/MyMethod")]
string MyMethod(string phrase);
}
public class MyServiceConcrete : IMyService
{
public string MyMethod(string phrase);
}
In this example, should I also apply the attributes that are applied to the interface to the concrete class (and its methods) that implements the MyService interface? Should I apply the same attributes in both places or just one of the places, and why? Thanks.
You only need to apply the service attributes to the interface. The interface, and only the interface, is used by WCF to define the API - the implementation always implements all interface members, so the attributes are not needed there. Attributes on the concrete implementation will just get ignored.
精彩评论