2012-07-19 6 views
0

또 다른 오히려 큰 제목 첫 번째 엔티티 프레임 워크 데이터베이스 내 모델 데이터를 사용하여 ...WCF REST 서비스 (JSON) 및

첫번째 그냥 몇 가지 기본 정보 : 사용하여 .NET 4/EF 4.x의/비주얼 스튜디오 2010 SP1.

JSON을 데이터 전송으로 사용하는 WCF REST 기반 서비스가 있습니다. 지금은

public class DiaryEvent 
{ 
    public Int64 ID { get; set; } 
    public Int64 CalendarResourceID { get; set; } 
    public string Subject { get; set; } 
    public string Location { get; set; } 
    public DateTime Start { get; set; } 
    public DateTime End { get; set; } 
    public string Description { get; set; } 
    public string Colour { get; set; } 
    public string AllDay { get; set; } 
} 

최소한의 WCF의 스티브의 예를 다시 : 나는 다음과 같이 http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx

내가 아주 기본적인 모델/클래스가 스티브 Mitchelotti에서 예 (확인도이 문제를 설명하는 다른 사람이있다)를 사용했다/REST, I는 아래 방법/데이터 서비스 계약을 정의하는 인터페이스를 가지고

[의 ServiceContract] 인터페이스 IDiaryService {[OperationContract를] // 목록을 GetEvents(); string GetEvents();

[OperationContract] 
DiaryEvent GetEvent(string id); 

[OperationContract] 
void InsertEvent(DiaryEvent NewDiary); 

[OperationContract] 
string UpdateEvent(string id, DiaryEvent UpdatedEventData); 

}

나는 나 또한 너무 인터페이스에 다른 WebGet이/WebInvoke의 장식 재료를 추가 할 수 있지만 지금은 어떻게 보이는지 때문이다있을 것 같아요.

는 그럼 실제 서비스 코드, 지금은 그렇게처럼 getevents 방법에 파이프에 테스트 데이터를 기본 목록 <>를 사용하고 위해에 :

WebGet(UriTemplate = "GetEvents", ResponseFormat=WebMessageFormat.Json) 
public DiaryEvent GetEvents() 
{ 
    List<DiaryEvent> EventList = new List<DiaryEvent>(); 
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 1, CalendarResourceID = 1, Start = new DateTime(2012, 07, 18, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 1 Description", Subject = "Event 1" }); 
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 2, CalendarResourceID = 2, Start = new DateTime(2012, 07, 19, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 2 Description", Subject = "Event 2" }); 
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 3, CalendarResourceID = 3, Start = new DateTime(2012, 07, 20, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 3 Description", Subject = "Event 3" }); 

    return EventList; 
    //return JsonConvert.SerializeObject(EventList); 
} 

(단지주의 유래에 문제가 장식 서식을 둘러싼 []로)!

클라이언트에게 JSON 데이터를 가져 오는 위의 아주 기본적인 예제가 있습니다.

요점은 EF 4.x를 데이터베이스 (테이블이 이미 만들어져 데이터베이스가 먼저 내 옵션이 될 수 있도록 만들어졌습니다)에 연결하는 데 사용하고 싶다는 것입니다. 저는 DiaryEvents 모델을 그 EF 데이터로 또는 그 EF 데이터로 채우는 연결 코드를 작성하는 것이 최선의 방법일까요?

누군가가 몇 가지 예/생각을 위해 올바른 방향으로 나를 지적 할 수 있다면 ??

매우 감사드립니다! 데이비드.

답변

-1
ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [System.ServiceModel.Web.WebInvoke(Method = "GET",ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Xml, BodyStyle =System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")] 
     string XMLData(string id); 
     [OperationContract] 
     [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")] 
     string JSONData(string id); 
    } 

public class RestServiceImpl : IRestServiceImpl 
    { 
     #region IRestService Members 
     public string XMLData(string id) 
     { 
      return "You Request Porduct" + ":"+id; 

     } 
     public string JSONData(string id) 
     { 
      return "Yor Request Product" +":"+ id; 
     } 
     #endregion 
    }