'WCF'에 해당되는 글 2건

  1. 간단한 WCF서비스의 클라이언트 생성
  2. 간단한 WCF 서비스 생성

1.  WCF 서비스를 사용할 클라이언트로서 윈도우폼 어블리케이션을 생성한다

사용자 삽입 이미지

2. Project의 Reference에 System.ServiceModel를 추가한다

3. 프로젝트에 Add Service Reference를 선택한다. 그리고 Address에 http://localhost:8000/dosc 를 입력하고 GO버튼을 클릭한다. 이때 WCF서비스는 호스트되고 있어야 한다
사용자 삽입 이미지



4. UI는 다음의 그림과 같이 구성한다
사용자 삽입 이미지


5. button Click 이벤트에 다음의 코드를 추가한다
        Dim result As Integer
        Dim ws As New CalculatorService.CalculatorClient()

        ws.Open()

        If RadioButton1.Checked = True Then
            result = ws.Add(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))
        ElseIf RadioButton2.Checked = True Then
            result = ws.Subtract(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))
        ElseIf RadioButton3.Checked = True Then
            result = ws.Multiply(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))
        ElseIf RadioButton4.Checked = True Then
            result = ws.Divide(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))
        End If

        ws.Close()

        Label1.Text = result.ToString()

간단한 WCF 서비스 생성

WCF는 SOA(Service-oriented application)기반의 어플리케이션을 위한 프레임워크이다. WCF의 개발을 위해서는 닷넷3.0 이상이 필요하다
WCF는 분산 어플리케이션을 구축하는 수단이다
WCF는 Enterprise Services처럼 트랜젝션을 처리할 수 있다
WCF는 .NET remoting과 같이 SOAP메시지의 바이너리 작용을 생성할 수 있다
WCF는 같은 장비 또는 다른 장비에 있는 다른 컴포넌트와 통신할 수 있다

SOA는 메시지 기반의 서비스 아키텍춰이다
- 경계가 명확하다
- 서비스는 자율적이다
- 서비스는 계약, 스키마 그리고 정책에 기반한다
- 서비스 호환성은 정책에 기반한다


WCF 서비스는 다음의 3가지로 구성된다
1. 서비스
2. 하나이상의 endpoint
3. 서비스를 제공할 환경

Endpoint는 다음의 3가지로 구성된다
A is for address(where)
B is for binding(how)
C is for contract(what)

** WCF서비스 생성
서비스를 생성하기 위한 2가지 주요 절차가 있다. 첫번째는  Service Contract를 생성하는 것이고, 두번째는 Data Contract를 생성하는 것이다.

다음의 순서에 의하여 WCF를 서비스하는 console 어플리케이션을 만들것이다.
1. Console application을 생성한다

사용자 삽입 이미지

2. System.ServiceModel.dll를 참조 추가한다
사용자 삽입 이미지

3. Calculator.vb를 추가 생성
4. 다음과 같이 ICalculator 인터페이스를 추가한다
Imports System.ServiceModel

<ServiceContract()> _
Public Interface ICalculator
    <OperationContract()> _
    Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
    <OperationContract()> _
    Function Subtract(ByVal a As Integer, ByVal b As Integer) As Integer
    <OperationContract()> _
    Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
    <OperationContract()> _
    Function Divide(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface

Public Class Calculator

End Class

<ServiceContract()> 속성은 서비스 클래스로써의 인터페이스 또는 클래스임을 설정한다
<OperationContract()> 속성은 WCF서비스로서의 메서드임을 정의한다


5. 서비스를 호스팅할 코드를 Module1.vb에 생성한다. 다음의 두개의 네임스페이스는 서비스를 호스팅하기 위해서 필요하다
Imports System.ServiceModel
Imports System.ServiceModel.Description

6. Module1.vb의 코딩
Imports System
Imports System.ServiceModel
Imports System.ServiceModel.Description
Module Module1
    Sub Main()

        Using serviceHost As ServiceHost = New ServiceHost(GetType(Calculator))

serviceHost.AddServiceEndpoint(GetType(ICalculator), New WSHttpBinding, New Uri("http://localhost:8000/Calculator/"))


            Dim smb As New ServiceMetadataBehavior()
            smb.HttpGetEnabled = True
            smb.HttpGetUrl = New Uri("http://localhost:8000/docs")

            serviceHost.Description.Behaviors.Add(smb)

            serviceHost.Open()

            Console.WriteLine("Press the <ENTER> key to close the host.")
            Console.ReadLine()
        End Using

    End Sub
End Module

-  Using serviceHost As ServiceHost = New ServiceHost(GetType(Calculator)): 서비스 호스트를 새엇ㅇ한다
- Dim ntb As NetTcpBinding = New NetTcpBinding(SecurityMode.None): 바인딩을 생성하며 시큐리티는 없다

7. Console 어플리케이션을 실행하면 다음과 같이 된다


8. Console 어플리케이션을 실행한 상태에서 http://localhost:8000/docs를 웹브라우져에서 보면 WSDL 문서를 볼 수 있다



** WCF서비스 호스팅
- Console applications
- Windows Forms applications
- Windows Presentation Foundation applications
- Managed Windows Services
- Internet Information Services (IIS) 5.1
- Internet Information Services (IIS) 6.0
- Internet Information Services (IIS) 7.0 and the Windows Activation Service (WAS)

** WCF 바인딩의 종류
System.ServiceModel.BasicHttpBinding
System.ServiceModel.Channels.CustomBinding
System.ServiceModel.MsmqBindingBase
System.ServiceModel.NetNamedPipeBinding
System.ServiceModel.NetPeerTcpBinding
System.ServiceModel.NetTcpBinding
System.ServiceModel.WSDualHttpBinding
System.ServiceModel.WSHttpBindingBase


WCF를 사용할 클라이언트는 http://youbeen.tistory.com/entry/간단한-WCF서비스의-클라이언트-생성 에서 참조한다