WCF Restful WebService

-- C# 2012. 6. 21. 16:30
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1) 프로젝트 생성

파일 > 새로 만들기 > 프로젝트 > Visual C# > WCF > WCF 서비스 라이브러리
OR
온라인 템플릿 > WCF REST Service Template 40(CS)

.NET Framework 4를 선택하고 이름(RestService)을 지정하고 확인.



2) 소스 확인

- Global.asax
- SampleItem.cs
- Service1.cs
- Web.config

* 파일삭제
- SampleItem.cs
- Service1.cs



3) 인터페이스 추가

솔루션탐색기 > 오른쪽 버튼 > 추가 > 새 항목 > 온라인 템플릿 > WCF 서비스

이름(RestServiceImpl)을 지정하고 추가.



4) 소스 확인

- IRestServiceImpl.cs
- RestServiceImpl.svc



5) IRestServiceImpl.cs에 아래와 같이 추가.

- IRestServiceImpl.cs

using System.ServiceModel.Activation;
using System.ServiceModel.Web;

[ServiceContract]

- 인터페이스에 OperationContract 추가

[OperationContract]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Xml,
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "xml/{id}")]
string XMLData(string id);


[OperationContract]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "json/{id}")]
string JSONData(string id);



6) RestServiceImpl.svc.cs에 아래와 같이 추가

using System.ServiceModel.Activation;
using System.ServiceModel.Web;


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

public string XMLData(string id)
{
    return "id: " + id;
}


public string JSONData(string id)
{
    return "id: " + id;
}


7) web.config 를 아래와 같이 수정


<?xml version="1.0"?>
<configuration>
 
  <system.web>
    <authentication mode="Windows" />
    <customErrors mode="Off" defaultRedirect="ErrorPage.htm"></customErrors>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
 
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" contract="RestService.IRestServiceImpl" binding="webHttpBinding" behaviorConfiguration="web"/>
      </service>
    </services>
   
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
   
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
   
  </system.serviceModel>
 
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 
</configuration>


8) Build

Release빌드를 하여 RestService.dll 생성


9) IIS에 웹사이트를 하나 추가.

경로는 빈폴더로 하나 생성. (D:\IISRoot\RestService), Port : 1234

해당 웹사이트의 응용 프로그램 풀 .Net framework를 4.0으로 수정


10) 게시


솔루션 탐색기 > 오른쪽 버튼 > 게시


게시 방법 : 웹 배포
서비스 URL :
http://localhost:1234
사이트 및 응용 프로그램 : RestService
대상의 IIS 응용 프로그램으로 표시 : 체크
대상의 추가파일 유지 : 체크안함



11) wsdl 확인


익스플로어에서 http://localhost:1234/RestServiceImpl.svc실행

화면에 svcutil.exe http://localhost:1234/RestServiceImpl.svc?wsdl이 보이면 성공.


12) 테스트


http://localhost:1234/RestServiceImpl.svc/xml/1


<?xml version="1.0"?>
-<XMLDataResponse xmlns="
http://tempuri.org/"><XMLDataResult>id: 1</XMLDataResult></XMLDataResponse>


http://localhost:1234/RestServiceImpl.svc/json/1

{"JSONDataResult":"id: 1"}


참고 :http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

'-- C#' 카테고리의 다른 글

Excel파일 읽기  (0) 2012.09.06
How to Initialize Hosted WCF Services  (0) 2012.07.18
Checked, Unchecked  (0) 2011.07.11
Partial class  (0) 2011.07.08
Sealed Class  (0) 2011.07.08
posted by 어린왕자악꿍