WCF 응답시 객체를 JSON 또는 Xml로 시리얼라이즈
WCF로 REST 웹서비스를 만들어 테스트해봤습니다. 테스트 과정에서 json 포멧으로 응답을 처리했는데 단순히 스트링 하나만 처리했었는데 json 값들을 여러개 채워서 응답할 수 있는 것을 정리해봅니다. 사실은 간단한데 WCF 처음이다보니 정리하게 되네요.
피들러로 테스트해보면 json으로 잘 넘어온 것을 확인할 수 있습니다.
Responseformat 을 Webmessageformat.Xml 로 해주면 xml로 되구요.
추가적으로 자세한 사항은 밑에 msdn 링크를 참고하세요.
msdn - 독립 실행형 JSON Serialization
msdn - JSON 데이터 시리얼라이즈 및 디시리얼라이즈
msdn - JSON과 XML 간의 매핑
msdn - 메시지 수준의 프로그래밍으로 JSON으로 시리얼라이즈
[ServiceContract]
public interface IService1
{
// TODO: Add your service operations here
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
GameUserProfile GetGameProfile(int Server_Type, string FBID);
}
[DataContract]
public class GameUserProfile
{
int gameUserNo;
string gameUserId;
string codename;
[DataMember]
public int GameUserNo
{
get { return gameUserNo; }
set { gameUserNo = value; }
}
[DataMember]
public string GameUserID
{
get { return gameUserId; }
set { gameUserId = value; }
}
[DataMember]
public string CodeName
{
get { return codename; }
set { codename = value; }
}
}
IService1.cs 선언부입니다. 추가된건 [DataContract]라는 부분입니다. Response할 객체를 저런식으로 선언해주고 사용합니다. public class Service1 : IService1
{
public GameUserProfile GetGameProfile(int Server_Type, string FBID)
{
GameUserProfile response = new GameUserProfile();
response.GameUserNo = 9;
response.GameUserID = "miglous";
response.CodeName = "Hani";
return response;
}
}
Service1.svc.cs 파일입니다. 기존에 string으로 리턴하던 것을 객체 인스턴트를 만들어 내용을 채운후에 객체를 리턴하면 WCF가 자동으로 닷넷 인스턴스를 지정된 json 또는 xml 인코딩 데이터로 시리얼라이즈 처리하는 것 같네요.피들러로 테스트해보면 json으로 잘 넘어온 것을 확인할 수 있습니다.
Responseformat 을 Webmessageformat.Xml 로 해주면 xml로 되구요.
추가적으로 자세한 사항은 밑에 msdn 링크를 참고하세요.
msdn - 독립 실행형 JSON Serialization
msdn - JSON 데이터 시리얼라이즈 및 디시리얼라이즈
msdn - JSON과 XML 간의 매핑
msdn - 메시지 수준의 프로그래밍으로 JSON으로 시리얼라이즈


댓글
댓글 쓰기