Microsoft MVP성태의 닷넷 이야기
.NET Framework: 74.2. WCF로 구현하는 .NET Remoting [링크 복사], [링크+제목 복사],
조회: 22453
글쓴 사람
정성태 (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)
13298정성태3/27/20234224Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234791Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20234171Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234418Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234575.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234584오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234775Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20235107.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234621.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233859Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233989Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20234150Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234618Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20234143Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
13284정성태3/13/20234382Windows: 226. Win32 C/C++ - Dialog에서 값을 반환하는 방법파일 다운로드1
13283정성태3/12/20233816오류 유형: 852. 파이썬 - TypeError: coercing to Unicode: need string or buffer, NoneType found
13282정성태3/12/20234130Linux: 58. WSL - nohup 옵션이 필요한 경우
13281정성태3/12/20234152Windows: 225. 윈도우 바탕화면의 아이콘들이 넓게 퍼지는 경우 [2]
13280정성태3/9/20234902개발 환경 구성: 670. WSL 2에서 호스팅 중인 TCP 서버를 외부에서 접근하는 방법
13279정성태3/9/20234372오류 유형: 851. 파이썬 ModuleNotFoundError: No module named '_cffi_backend'
13278정성태3/8/20234427개발 환경 구성: 669. WSL 2의 (init이 아닌) systemd 지원 [1]
13277정성태3/6/20235209개발 환경 구성: 668. 코드 사인용 인증서 신청 및 적용 방법(예: Digicert)
13276정성태3/5/20234795.NET Framework: 2102. C# 11 - ref struct/ref field를 위해 새롭게 도입된 scoped 예약어
13275정성태3/3/20235021.NET Framework: 2101. C# 11의 ref 필드 설명
13274정성태3/2/20234599.NET Framework: 2100. C# - ref 필드로 ref struct 타입을 허용하지 않는 이유
13273정성태2/28/20234346.NET Framework: 2099. C# - 관리 포인터로서의 ref 예약어 의미
1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...