2011-05-17 53 views
2

我有一個Service.svc類。我有:爲什麼我需要界面(用[ServiceContract]裝飾)?

public class CommentsWCFService : Objects.ISessionIDCommentWCFContract 
    { 
     public List<Comment> GetComments(string SessionId) 
     { 
      //dummy data first 
      List<Comment> dummyData = new List<Comment>(); 

      if (SessionId == "123") 
      { 
       dummyData.Add(new Comment("Comment 1", DateTime.Now)); 
       dummyData.Add(new Comment("Comment 2", DateTime.Now)); 
       dummyData.Add(new Comment("Comment 3", DateTime.Now)); 
      } 

      return dummyData; 
     } 
    } 

我已經實現了接口,因爲我被告知這樣做。我想知道爲什麼我需要繼承裝飾有[ServiceContract]屬性的界面?

.net會知道嗎?如果我一起刪除了這個界面,我還可以調用服務嗎?

+0

這取決於你的服務如何被調用。谷歌的「WCF共享接口」,這裏有一些[鏈接](http://blogs.crankygoblin.com/blogs/geoff.appleby/archive/2006/06/08/Sharing-Interfaces-in-WCF.aspx)[另一個一個](http://weblogs.asp.net/avnerk/archive/2006/05/17/Share-your-interfaces-_2D00_-avoid-static-proxies_2E00_.aspx) – BenCr 2011-05-17 10:52:04

回答

0

該屬性告訴WCF該接口定義了一個合約並應該公開。
查看documentation或關於WCF的任意教程。

+0

嗨是的,我看了那篇文章真的很有幫助。我現在明白,要實現一個WCF服務,NET。要求你confugure ...地址,合同和綁定類型....乾杯。 – Exitos 2011-05-17 11:15:20