简体   繁体   中英

WCF Rest coed for Json

Usually I see the code of WCF REST JSON is likely:

// Start the service and browse to <a href="http://:/SampleService/help[ServiceContract]  public">http://<machine_name>:<port>/SampleService/help [ServiceContract]
 public interface ISampleService {
[WebGet(UriTemplate = "")]
List<SampleItem> GetCollection();

That means interface is introduced. But in some occurence I saw in the code interface is never used at all. Such as

 [ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AlertService
{
    AlertContext alertProxy = new AlertContext();
    AlertDetailsContext alertDetailProxy = new AlertDetailsContext();
    Analytics analyticsProxy = new Analytics();

    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="/SearchAlerts")]
    public List<Alert> SearchAlerts(AlertFilter filter)
    {
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache, must-revalidate");
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");

Here class is used directly, why?

Because the ServiceContract doesn't have to be an interface contract. Using an interface allows you to share the same contract among multiple implementations and can make unit testing possible, but it is not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM