Microsoft MVP성태의 닷넷 이야기
.NET Framework: 74.2. WCF로 구현하는 .NET Remoting [링크 복사], [링크+제목 복사],
조회: 22456
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
부모글 보이기/감추기
(연관된 글이 3개 있습니다.)
2. WCF로 구현하는 .NET Remoting


이번 토픽에서는 WCF의 Remoting 모드에 대해서 알아보겠습니다. 사실 "remoting 모드"라는 것은 제가 의미상으로 만들어낸 말일 뿐, 이런 용어는 없습니다. 그렇다면 왜 그런 용어를 제가 굳이 언급하는 것일까요?

원격 호출 방식은 크게 2가지로 나뉠 수 있습니다. 하나는 "웹 서비스 방식", 또 하나는 ".NET 리모팅 방식"이 그것입니다. 이 2가지는 절대 화합할 수 없을 것만 같습니다. 뭐랄까... 마치 "상대성 이론"과 "양자역학"의 관계라고나 할까요? (아마도 대통일장 이론이 나올때 쯤이면, 웹 서비스와 리모팅도 단일화될 것입니다.) 2가지 모두 나름대로의 뚜렷한 성격을 가지고 있기 때문인데요. 잘 아시는 것처럼 "웹 서비스"로 통신을 하게 되면 범용적으로 모든 프로세스들이 그 서비스를 이용할 수는 있지만 부가적인 Payload로 인해 통신이 가볍게 되지는 않습니다. 따라서 ".NET"과 ".NET" 간의 통신이라면 굳이 "웹 서비스 통신" 방식을 쓰지 않고 가볍고 최적화된 ".NET 리모팅 방식"을 같이 제공해 주게 되는 것입니다.
그런 이유로, 제가 설명을 무난하게 하기 위해서 굳이 "웹 서비스 모드"와 "Remoting 모드"라는 말로 나눠서 이야기할 것입니다.

우선, "웹 서비스 모드"는 지난번에 잠깐 살펴봤습니다. 어떻게 만드는지도 예제를 만들어 봤지요. 아직 안 읽어 보셨다면 아래의 링크를 참조하십시오.

3.1 WCF와 WSE 3.0의 활용
; https://www.sysnet.pe.kr/2/0/378

일단은 웹 서비스 모드는 그것으로 끝내고, 이번 회에서는 리모팅 모드에 대해서만 알아보도록 하겠습니다.



실제로 예제를 제작해 보면서 설명을 할 텐데요. 사실 app.config 및 자동 생성 코드를 제외하고는 위에서 링크를 걸어두었던 "3.1 WCF와 WSE 3.0의 활용"에서와 소스는 거의 비슷합니다.

[서버 측 WCF 제작]

1. 콘솔 응용 프로그램으로 서버 예제를 만들어 보겠습니다. 따라서 콘솔 프로젝트를 생성하는데, 여기서는 "WcfRemoteServer"라는 이름으로 생성하겠습니다.

2. "System.ServiceModel"에 대해 참조를 추가하고, "Program.cs"에 다음과 같이 입력합니다. (이전에 살펴본 웹 서비스로 하는 예제와 동일한 코드입니다.)

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace WcfRemoteServer
{
  class Program
  {
    static void Main(string[] args)
    {
      using (ServiceHost serviceHost = new ServiceHost(typeof(CHelloWorld)))
      {
        serviceHost.Open();

        Console.WriteLine("Press any key to exit...");
        Console.ReadLine();
        serviceHost.Close();
      }
    }
  }

  [ServiceContract(Namespace = "http://www.wcftest.com/")]
  public interface IHelloWorld
  {
    [OperationContract]
    string HelloWorld();
  }

  [ServiceBehavior]
  public class CHelloWorld : IHelloWorld
  {
    public string HelloWorld()
    {
      return "Hello World";
    }
  }
}

3. "새 항목 추가"를 통해서 "Application Configuration File" - app.config - 파일을 추가하고 다음과 같이 내용을 입력합니다.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>

    <services>
      <service 
         name="WcfRemoteServer.CHelloWorld"
         behaviorConfiguration="HelloWorldBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9091/HelloService"/>
          </baseAddresses>
        </host>
        <endpoint address="net.tcp://localhost:9092/HelloService"
           binding="netTcpBinding"
           bindingConfiguration="HelloService_TcpBinding" 
           contract="WcfRemoteServer.IHelloWorld" />

        <endpoint address="mex"
                 binding="mexHttpBinding"
                 contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="HelloWorldBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <netTcpBinding>
        <binding name="HelloService_TcpBinding">
          <reliableSession enabled="false" ordered="true"/>
          <security mode="None" >
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

  </system.serviceModel>
  
</configuration>

4. 마지막으로 빌드를 정상적으로 완료하면 서버 측 WCF 서비스는 완성된 것입니다.




클라이언트 측 제작 단계는, 이전의 웹 서비스 예제보다 다소 복잡합니다. 왜냐하면, 이제는 "웹 참조 추가" 대화창을 통한 서비스 사용 코드를 생성할 수 없기 때문입니다. 대신, Windows SDK를 설치하면 "svcutil.exe"라는 도구를 "C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin" 폴더에서 볼 수 있는데, 이 유틸리티를 이용해서 서비스 호출 코드를 생성할 수 있게 됩니다. 만약 자신의 PC에 Windows SDK가 설치되어져 있지 않다면 다음의 경로에서 다운로드 받아 설치합니다.

Microsoft® Windows® Software Development Kit (ISO) for September CTP of Windows Vista and .NET Framework 3.0 Runtime Components
; http://www.microsoft.com/downloads/details.aspx?FamilyId=28FAD42D-983E-4A57-B5A6-BF058766A9FF&displaylang=en

용량이 1GB가 넘는데, 만약 여의치 않은 상황이라면 동료중에서 Windows SDK를 설치한 이가 있다면, 그에게서 "svcutil.exe"만 복사받으셔도 무방합니다.

준비가 되었다면 시작해 볼까요? ^^

1. 서비스를 호출하는 콘솔 프로젝트를 새로 생성합니다. 여기서는 "WcfRemoteClient"라고 이름 짓겠습니다.

2. "System.ServiceModel" 어셈블리를 참조 추가합니다.

3. 자, 이제 서비스 호출 코드를 자동 생성해야 할 텐데요. 서버 콘솔 프로그램(WcfRemoteServer.exe)을 실행시키고 다음과 같이 해서 소스 생성을 합니다.

C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin>svcutil http://localhost:9091/HelloService
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.3]
Copyright (c) Microsoft Corporation.  All rights reserved.

Attempting to download metadata from 'http://localhost:9091/HelloService' using
WS-Metadata Exchange or DISCO.
Generating files...
D:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\CHelloWorld.cs
D:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\output.config

4. 위의 과정을 통해서, CHelloWorld.cs 파일과 output.config 파일이 생성되었는데, CHelloWorld.cs 파일은 WcfRemoteClient 프로젝트에 추가시키고, output.config 파일의 내용은 복사해서 "App.config" 파일에 붙여 넣습니다.

5. 마지막으로, 다음과 같이 Main 함수를 작성하고 컴파일합니다.

    static void Main(string[] args)
    {
      HelloWorldClient hwc = new HelloWorldClient();
      Console.WriteLine(hwc.HelloWorld());
    }

[첨부된 압축 파일 "ConsoleHostService.zip"은 위의 예제를 포함하고 있습니다.]



위와 같이 만들고 나서, WcfRemoteServer.exe와 WcfRemoteClient.exe를 실행시키면 실제 동작되는 것을 확인할 수 있습니다. 아래의 내용은 동일한 HelloWorld 서비스 클래스에 대해 웹 서비스와 리모팅으로 호출한 패킷 내용을 비교한 것입니다. 잠깐만 보셔도... 역시 WcfRemoteServer/WcfRemoteClient 간의 호출이 간결하다는 것을 알 수 있습니다.

"웹 서비스 모드" 에서의 hello world 패킷 통신(실제 패킷에서 보기좋게 들여쓰기 한 것입니다.)

-- 요청 1 --
POST /HelloService HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.42)
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.wcftest.com/IHelloWorld/HelloWorld"
Host: sedona2:9091
Content-Length: 292
Expect: 100-continue
Connection: Keep-Alive

-- 응답 1 --
HTTP/1.1 100 Continue

-- 요청 2 --
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
	<HelloWorld xmlns="http://www.wcftest.com/" /></soap:Body>
</soap:Envelope>

-- 응답 2 --
HTTP/1.1 200 OK
Content-Length: 215
Content-Type: text/xml; charset=utf-8
Server: Microsoft-HTTPAPI/1.0
Date: Wed, 18 Oct 2006 01:02:01 GMT

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
	<HelloWorldResponse xmlns="http://www.wcftest.com/">
		<HelloWorldResult>Hello World</HelloWorldResult>
	</HelloWorldResponse>
</s:Body>
</s:Envelope>




"리모팅 서비스 모드"에서의 hello world 패킷 통신

-- 요청 1 --
......#net.tcp://sedona2:9092/HelloService...

-- 응답 1 --
.

-- 요청 2 --
...u-http://www.wcftest.com/IHelloWorld/HelloWorld
#net.tcp://sedona2:9092/HelloService.HelloWorld.http://www.wcftest.com
/V...s...a.V.D......D...RV....F.7"...g.D,D*...D.......V.B......

-- 응답 2 --
...r5http://www.wcftest.com/IHelloWorld/HelloWorldResponse.HelloWorldResponse.
http://www.wcftest.com/.HelloWorldResultV.
..s...a.V.D......D...RV....F.7"...g.D.......V.B...B...Hello World...







[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/22/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2006-10-19 08시08분
'대통일장 이론' 비유가 압권입니다. 하하하~ ^_^
songgun
2006-10-19 11시28분
^^
kevin25
2006-10-20 01시16분
위에서 종단점주소와 client간의 패킷을 잡기 위한 좋은 간편한 툴좀 소개 해주세요..^^
unbinara
2006-10-20 10시41분
개인적으로 만들어서 사용하고 있는 툴이 있긴 한데, 버그가 많아서 ^^; 저만 사용하고 있습니다. 일일이 설명하기도 귀찮고, 그다지 UI도 좋지 않아서요. 다른 분들은 Fiddler를 많이 이용하는 것으로 알고 있습니다. HTTP가 아닌 좀 더 low-level 수준으로 하는 경우에는 Windows Server 계열에 설치할 수 있는 네트워크 모니터링이나 그 외에 공개된 네트워크 패킷 캡쳐 프로그램들이 있지요. ^^

나중에 한번 설명은 드리겠지만, WCF의 경우에는 자체적인 로그 기능도 있습니다. 그 로그를 보는 Viewer도 나름대로는 쓸만하고요.
kevin25

1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13323정성태4/16/20234564개발 환경 구성: 677. Octave에서 Excel read/write를 위한 io 패키지 설치
13322정성태4/15/20235399VS.NET IDE: 182. Visual Studio - 32비트로만 빌드된 ActiveX와 작업해야 한다면?
13321정성태4/14/20234189개발 환경 구성: 676. WSL/Linux Octave - Python 스크립트 연동
13320정성태4/13/20234066개발 환경 구성: 675. Windows Octave 8.1.0 - Python 스크립트 연동
13319정성태4/12/20234590개발 환경 구성: 674. WSL 2 환경에서 GNU Octave 설치
13318정성태4/11/20234468개발 환경 구성: 673. JetBrains IDE에서 "Squash Commits..." 메뉴가 비활성화된 경우
13317정성태4/11/20234503오류 유형: 855. WSL 2 Ubuntu 20.04 - error: cannot communicate with server: Post http://localhost/v2/snaps/...
13316정성태4/10/20233775오류 유형: 854. docker-compose 시 "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" 오류 발생
13315정성태4/10/20234093Windows: 245. Win32 - 시간 만료를 갖는 컨텍스트 메뉴와 윈도우 메시지의 영역별 정의파일 다운로드1
13314정성태4/9/20234218개발 환경 구성: 672. DosBox를 이용한 Turbo C, Windows 3.1 설치
13313정성태4/9/20234230개발 환경 구성: 671. Hyper-V VM에 Turbo C 2.0 설치 [2]
13312정성태4/8/20234292Windows: 244. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (개선된 버전)파일 다운로드1
13311정성태4/7/20234719C/C++: 163. Visual Studio 2022 - DirectShow 예제 컴파일(WAV Dest)
13310정성태4/6/20234364C/C++: 162. Visual Studio - /NODEFAULTLIB 옵션 설정 후 수동으로 추가해야 할 library
13309정성태4/5/20234477.NET Framework: 2107. .NET 6+ FileStream의 구조 변화
13308정성태4/4/20234430스크립트: 47. 파이썬의 time.time() 실숫값을 GoLang / C#에서 사용하는 방법
13307정성태4/4/20234173.NET Framework: 2106. C# - .NET Core/5+ 환경의 Windows Forms 응용 프로그램에서 HINSTANCE 구하는 방법
13306정성태4/3/20234070Windows: 243. Win32 - 윈도우(cbWndExtra) 및 윈도우 클래스(cbClsExtra) 저장소 사용 방법
13305정성태4/1/20234401Windows: 242. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (쉬운 버전)파일 다운로드1
13304정성태3/31/20234694VS.NET IDE: 181. Visual Studio - C/C++ 프로젝트에 application manifest 적용하는 방법
13303정성태3/30/20234038Windows: 241. 환경 변수 %PATH%에 DLL을 찾는 규칙
13302정성태3/30/20234643Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234791Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20234510Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20234247Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20234224Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...