Microsoft MVP성태의 닷넷 이야기
.NET Framework: 74.2. WCF로 구현하는 .NET Remoting [링크 복사], [링크+제목 복사],
조회: 22387
글쓴 사람
정성태 (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)
13613정성태5/1/2024102닷넷: 2253. C# - Video Capture 장치(Camera) 열거 및 지원 포맷 조회파일 다운로드1
13612정성태4/30/202488오류 유형: 902. Visual Studio - error MSB3021: Unable to copy file
13611정성태4/29/2024239닷넷: 2252. C# - GUID 타입 전용의 UnmanagedType.LPStruct - 두 번째 이야기파일 다운로드1
13610정성태4/28/2024353닷넷: 2251. C# - 제네릭 인자를 가진 타입을 생성하는 방법 - 두 번째 이야기
13609정성태4/27/2024475닷넷: 2250. PInvoke 호출 시 참조 타입(class)을 마샬링하는 [IN], [OUT] 특성파일 다운로드1
13608정성태4/26/2024856닷넷: 2249. C# - 부모의 필드/프로퍼티에 대해 서로 다른 자식 클래스 간에 Reflection 접근이 동작할까요?파일 다운로드1
13607정성태4/25/2024978닷넷: 2248. C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/2024967닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/2024959닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024966오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/20241012닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/20241003닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/20241018닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/20241035닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024959닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/20241006닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/20241019닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241129닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241082닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241105닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241104닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241257C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241226닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241095Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241205닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...