티스토리 뷰

반응형

WCF에서 제공하는 서비스를 클라이언트에서 제공되기 위해서는 런타임 환경이 필요하게되는데 이를 호스팅 어플리케이션이라고 합니다.

앞에서는 IIS에 서비스를 배치함으로 IIS가 이러한 런타임을 제공하였던 것으로 이를 IIS 호스팅이라고 합니다.

호스팅 어플리케이션은 서비스의 시작과 종료, 클라이언트의 요청 대기, 서비스에서 클라이언트로 메세지를 전송하는 역할을 합니다.


1. 호스팅 어플리케이션


호스트 어플리케이션은 클라이언트가 서비스를 이용할 수 있도록 endpoint 를 제공하고 있는데 이 endpoint 는 ABC(Address, Binding, Contract)로 구성되어 있습니다.


Address

전송 프로토콜에 따라 서비스가 제공되는 주소입니다. 주소는 클라이언트가 서비스를 이용할 수 있도록 서비스의 정보(메타 데이터)를 클라이언트로 보내주는 base-address 와 서비스 주소로 구성되어 있습니다.

클라이언트는 base-address 로 부터 메타정보를 받아 프록시 클래스를 생성하게 됩니다. IIS에서는 서비스의 주소가 base-address 로 이용될 수 있으며 앞선 예제처럼 주소를 지정하지 않을 경우 IIS의 주소가 기본주소가 됩니다.

Binding

접속 프로토콜 및 데이터의 포멧등을 정의하는 것으로 전송프로토콜, 메세지 인코딩 포멧, 보안 요구사항, 트랜잭션 처리, 메세지의 신뢰성등이 Binding 정보에 포함되게 됩니다.

Contract

서비스에서 제공하는 서비스와 데이터의 정의로 앞선 예제에서는 상품정보를 제공하기 위한 데이터로 DataContract로 4가지 기능을 제공하기 위해 ServiceContract를 정의하였습니다.


2. WPF 호스팅 어플리케이션


   2.1 솔루션 >> 추가 > 새 프로젝트 > Windows Application (WPF)

   

이름

ProductsServiceWPFHost

위치

 ...\WCF Step by Step


https://tistory1.daumcdn.net/tistory/87291/skin/images/blank.png   


   

   2.2 Windows1.xaml 파일을 HostController.xaml 로 이름을 변경하고 관련 프로그램을 수정합니다.

   

  HostController.xaml

<Window x:Class="ProductsServiceWPFHost.HostController"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="ProductsServiceWPFHost" Height="300" Width="300"

    >

    <Grid>

       

    </Grid>

</Window>


  HostController.xaml.cs

namespace ProductsServiceWPFHost

{


    public partial class HostController : System.Windows.Window

    {


        public HostController()

        {

            InitializeComponent();

        }


    }

}



   App.xaml

<Application x:Class="ProductsServiceWPFHost.App"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="HostController.xaml"

    >

    <Application.Resources>

         

    </Application.Resources>

</Application>



   2.3 서비스를 시작하고 중료할 수 있는 두개의 버튼과 상태를 나타내는 텍스트 박스를 다음과 같이 구성합니다.


https://tistory1.daumcdn.net/tistory/87291/skin/images/blank.png   

   

   다음과 같은 xaml 코드를 붙여넣어도 됩니다.


HostController.xaml

<Grid>

  <Button Height="23" HorizontalAlignment="Left" Margin="51,60,0,0" Name="start" VerticalAlignment="Top" Width="75">Start</Button>

  <Button Height="23" Margin="0,60,56,0" Name="stop" VerticalAlignment="Top" HorizontalAlignment="Right" Width="75" IsEnabled="False">Stop</Button>

  <Label Height="23" HorizontalAlignment="Left" Margin="19.37,0,0,109" Name="label1" VerticalAlignment="Bottom" Width="87.63">Service Status:</Label>

  <TextBox IsReadOnly="True" Margin="121,0,39,101" Name="status" Text="Service Stopped" Height="26" VerticalAlignment="Bottom"></TextBox>

</Grid>


   

   2.4 ProductsServiceWPFHost 프로젝트 >> 참조 추가 > 프로젝트 > ProductsService 추가

   

https://tistory1.daumcdn.net/tistory/87291/skin/images/blank.png

   

   2.4 ProductsServiceWPFHost 프로젝트 >> 참조 추가 > .NET > System.ServiceModel 추가

   

https://tistory1.daumcdn.net/tistory/87291/skin/images/blank.png

   

   2.5 ProductsServiceWPFHost 프로젝트 > HostController.xaml.cs 수정


using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;


using System.ServiceModel;

using Products;


namespace ProductsServiceWPFHost

{


    public partial class HostController : System.Windows.Window

    {


        private ServiceHost productsServiceHost;


        public HostController()

        {

            InitializeComponent();

        }


        void onStartClick(object sender, EventArgs e)

        {

            productsServiceHost = new ServiceHost(typeof(ProductsServiceImpl));

            productsServiceHost.Open();

 

            stop.IsEnabled = true;

            start.IsEnabled = false;

 

            status.Text = "Service Running";

        }

 

        void onStopClick(object sender, EventArgs e)

        {

            productsServiceHost.Close();

            stop.IsEnabled = false;

            start.IsEnabled = true;

            status.Text = "Service Stopped";

        }

    }

}



   서비스를 시작하고 종료하는 메서드를 위와 같이 작성합니다.

   

   WCF 서비스를 제공하기 위해서는 System.ServiceModel 네임스페이스의 ServiceHost 클래스를 이용해야 하며 서비스의 리스너를 시작하기 위해서는 ServiceHost 클래스의 Open 메소드를 이용합니다.


   2.6 HostController.xaml 의 내용을 다음과 같이 수정합니다.


<Button Height="23" HorizontalAlignment="Left" Margin="51,60,0,0" Name="start" VerticalAlignment="Top" Width="75" Click="onStartClick">Start</Button>

<Button Height="23" Margin="0,60,56,0" Name="stop" VerticalAlignment="Top" HorizontalAlignment="Right" Width="75" IsEnabled="False" Click="onStopClick">Stop</Button>

   

   

   2.7 ProductsServiceHost 프로젝트의 web.config 파일을 ProductServiceWPFHost 프로젝트로 복사한 후 이름을 app.config 로 변경하고 다음과 같이 수정합니다.


<service behaviorConfiguration="" name="Products.ProductsServiceImpl">

    <endpoint address="http://localhost:8000/ProductsService/ProductsService.svc" binding="basicHttpBinding" bindingConfiguration=""

        contract="Products.IProductsService" />

</service>



   메타정보를 제공하는 기본주소가 없으므로 메타정보의 설정을 삭제하고 서비스의 주소를 http://localhost:8000/ProductsService/ProductsService.svc 로 수정합니다.

   설정된 주소는 호스트 어플리케이션에서 사용할 가상의 주소이므로 ProductsService.svc 파일은 존재하지 않아도 정상으로 동작합니다.

   실제 파일이름까지를 포함하는주소는 http나 https 프로토콜에서는 가능하나 tcp등 다른 프로토콜에서는 파일이름이 주소에 포함되서는 안됩니다.

   WCF의 ABC에서 Contract은 이전글과 이글의 앞부분을 통해 소개하였습니다.

   Address는 WCF에서 서비스를 제공하는 가상의 주소라고 이해하시면 됩니다.


   또한 호스팅 어플리케이션은 WCF 서비스를 제공하는 어플리케이션으로 IIS를 통해 호스팅을 제공(IIS호스팅)할 수도 있지만 별도의 프로그램을 통해 서비스를 제공할 수 있는데 이를 Self호스팅 이라고 하며 호스팅을 위해서는 System.ServiceModel 네임스페이스의 ServiceHost 를 통해 이러한 서비스를 제공하게 됩니다.



본 자료는 자유롭게 이용하실 수 있으며 사용시 출처를 밝혀 주시기 바랍니다. (Devpia NET Framework 3.0)

메일로 문의 : saeparam@naver.com

Posted by [새파람]


반응형
댓글