4. WCF 클라이언트 프락시 클래스 및 config 파일 생성
WCF 서비스가 만들어졌으니 이제 Service Model Metadata Utility 툴(Svcutil.exe)을 이용하면 앞에서 말한 것처럼 클라이언트측 프록시 클래스와 설정 파일을 만들수 있다. /config 스위치를 사용하면 생성되는 설정 파일의 이름을 지정할 수 있다.
먼저 서비스 프로그램을 실행시킨다.
그런 다음, VS.NET을 설치하면 함께 설치되는 명령 프롬프트에서 다음 명령을 실행시킨다.
|
svcutil /language:C# /config:app.config http://localhost/HelloService |
C 드라이브의 루트 폴더를 보면 프락시 클래스 파일과 환경 설정 파일이 생성되어 있을 것이다.
프락시 클래스파일은 기본적으로 현재 서비스되고 있는 서비스명과 동일한 이름으로 생성된다.
|
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService))) |
필요하다면 /out 스위치를 사용해서 파일명을 변경할 수 있다. 클라이언트측 설정 파일을 보면 클라이언트에서필요한 바인딩 설정과 서비스 엔드 포인트에 대한 내용이 들어있다.
|
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IHelloService" closeTimeout=" openTimeout=" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout=" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://localhost/HelloService" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloService" contract="IHelloService" name="WSHttpBinding_IHelloService"> <identity> <userPrincipalName value="ighwang@lottenxg.com" /> </identity> </endpoint> </client> </system.serviceModel> </configuration> |
5. WCF 프록시 파일 사용하기
다음은 앞에서 생성한 설정 파일과 프락시 클래스를 사용할 클라이언트 애플리케이션을 만든다.
컨솔용 프로젝트를 추가해서 그림처럼 앞에서 생성한 두 파일을 추가한다. 그리고 “HelloClient”라는 파일을 추가한다. System.ServiceModel.dll 에대한 참조도 추가한다.
HelloClient.cs 파일에 다음 코드를 복사해 넣는다.
|
using System; using System.ServiceModel; namespace WCFClient { class HelloClient { static void { HelloServiceClient helloService = new HelloServiceClient(); try { //서비스를 호출해서 값을 리턴받는다. string strHello = helloService.Hello(); //출력 Console.WriteLine(strHello ); // 프락시의 Close()를 호출하면, 커넥션과 리소스를 우아하게 해제시킬 수 있다. helloService.Close(); } catch (TimeoutException timeout) { Console.WriteLine(timeout.Message); Console.ReadLine(); helloService.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine(commProblem.Message); Console.ReadLine(); helloService.Abort(); } Console.WriteLine("<ENTER>를 치면 종료됩니다."); Console.ReadLine(); } } } |
코드는 단순하다. 서비스의 프락시 클래스에 대한 인스턴스를 생성해서 서비스의 Hello() 오퍼레이션을 호출해서문자열을 하나 리턴받고 바로 컨솔로 출력하고 있다.
서비스를 먼저 실행시키고, 클라이언트 애플리케이션도 실행시켜보자.
썰렁한 결과가 출력된다.
6. 요약
'개발 > Communication' 카테고리의 다른 글
| [연재 06] 서비스 컨트랙트 정의하기 (0) | 2009/04/23 |
|---|---|
| [연재 05] WCF 관련 용어들 (0) | 2009/04/23 |
| [연재 04] Hello world WCF 버전 3 (0) | 2009/04/23 |
| [연재 03] Hello world WCF 버전 2 (0) | 2009/04/23 |
| [연재 02] Hello world WCF 버전 1 (0) | 2009/04/23 |
| [연재 01] WCF 연재를 시작하면서... (0) | 2009/04/23 |
