ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [WCF] 1. Helloworld
    .NET/개념 및 유용한 팁 2015. 2. 18. 20:50
    반응형
    WCF는 .NET Core 및 새로운 .NET 에서 소개하는 gRPC로 대체되었습니다. 유지보수 외 신규 개발에선 추천하지 않습니다.
    참조 : https://docs.microsoft.com/ko-kr/aspnet/core/grpc/why-migrate-wcf-to-dotnet-grpc?view=aspnetcore-5.0#grpc-as-a-migration-path-for-wcf-to-net-core-and-net-5

     

    WCF 학습을 위해 작성한 소스이다. 콘솔 프로젝트에서 작성하였다.
    (해당 소스를 실행하기위해선 Visual Studio가 관리자모드로 실행되어야 한다. 완성된 프로그램이 관리자권한을 요청할 수 있도록 제작하려면 app.manifest를 프로젝트에 추가하여 파일 내용중 requestedExecutionLevel 의 level값을 requireAdministrator로 바꿔주면 된다.)

     

    [Service 프로그램의 IWCFService.cs]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    
    namespace WCF_Helloworld_Service.Services
    {
        [ServiceContract]
        interface IWCFService
        {
            [OperationContract]
            string sayHi();
        }
    
        class WCFService : IWCFService
        {
            public string sayHi()
            {
                return "Hello WCF!";
            }
        }
    
    }
    
    

    [Service 프로그램의 Program.cs]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    using WCF_Helloworld_Service.Services;
    
    namespace WCF_Helloworld_Service
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(WCFService), 
                    new Uri("http://localhost/wcf/")); // 클라이언트가 서비스에 접근하게 할 Base 주소
                host.AddServiceEndpoint(typeof(IWCFService), new BasicHttpBinding(), "WCFService");
                host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled =  true }); // 안해주면 Client에서 서비스 참조 추가를 할 수가 없다.
                host.Open();
                bool isRun = true;
                while (isRun)
                {
                    //서비스 종료 방지를 위한 while 문. ESC 키 눌렀을 때 프로그램이 종료됨.
                    var key = Console.ReadKey();
                    switch (key.Key)
                    {
                        case ConsoleKey.Escape:
                            isRun = false;
                            break;
                    }
                }
            }
        }
    }
    


    해당 프로그램을 실행하면 아무것도 뜨지 않는 까만 콘솔화면이 실행된다.

    서비스 확인을 위해선 웹 브라우저를 띄워 http://localhost/wcf/로 접속하면 된다.

    [그림 1] WCFService가 잘 실행된 모습


    서비스를 실행한 상태에서 새로운 프로젝트를 생성한 뒤, 솔루션 탐색기의 "참조"에 마우스 오른쪽 버튼을 눌러 "서비스 참조 추가"를 실행한다. 그 후 서비스를 열어 주소란에 http://localhost/wcf/ 입력하고 이동을 누르면 사용할 수 있는 서비스가 자동으로 추가된다. 

    [그림 2] 서비스 참조 추가


    레퍼런스가 추가되었다면 App.config에 다음과 같이 추가된다.

    <!--?xml version="1.0" encoding="utf-8" ?-->
    <configuration>
        <startup> 
            <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5">
        </supportedruntime></startup>
        <system.servicemodel>
            <bindings>
                <basichttpbinding>
                    <binding name="BasicHttpBinding_IWCFService">
                </binding></basichttpbinding>
            </bindings>
            <client>
                <endpoint address="http://localhost/wcf/WCFService" binding="basicHttpBinding" bindingconfiguration="BasicHttpBinding_IWCFService" contract="ServiceReference1.IWCFService" name="BasicHttpBinding_IWCFService">
            </endpoint></client>
        </system.servicemodel>
    </configuration>

    소스를 작성한 후 실행해본다.

     

    [Client 프로그램의 Program.cs]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    using WCF_Helloworld_Client.ServiceReference1;
    
    namespace WCF_Helloworld_Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                WCFServiceClient client = new WCFServiceClient();
                Console.WriteLine(client.sayHi());
            }
        }
    }
    

     

    [그림 3] Client의 구동결과

     

    참조 : http://taeyo.net/columns/View.aspx?SEQ=338&PSEQ=23&IDX=4

    소스 :

    WCF_Helloworld.zip
    다운로드

     

    반응형

    댓글

Designed by Tistory.