본문 바로가기
프로그래밍/C#

WCF 로그인 서버 공부 및 만들기

by 뽀도 2019. 6. 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Web;
 
namespace WCFLoginServer
{
    public class LoginServer
    {
        public static List<UserInfo> userList = new List<UserInfo>();
 
        /// <summary>
        /// 유저 정보 검색 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pw"></param>
        /// <returns></returns>
        public static string FindUser(string id, string pw)
        {
            var result = userList.Find(w => w.Id == id && w.Pw == pw);
 
            if (result == null)
                return "data is null check your id or pw";
            else
                return result.NickName;
        }
 
        /// <summary>
        /// 유저 데이터 초기화
        /// </summary>
        private void InitUserData()
        {
            userList.Add(new UserInfo("seola""1234","설아짱"));
            userList.Add(new UserInfo("bona""4321","보나짱"));
            userList.Add(new UserInfo("ruda""5678","루다짱"));
        }
        /// <summary>
        /// 유저 데이터 확인 
        /// </summary>
        private void ShowUserData()
        {
            int number = 0 ;
            Console.WriteLine("-------------------------------------------");
            foreach (var r in userList)
            {
                Console.WriteLine(string.Format("User No  : {2} -> Id {0} : Pw :{1}", r.Id, r.Pw, number++));
            }
            Console.WriteLine("-------------------------------------------");
        }
        
        public void Start()
        {
            InitUserData();
 
            //서비스 설명에서 엔드포인트를 찾지 못하면
            // 서비스의 기본 주소에 Http 및 Https 기본 주소를 위한
            // 기본 엔ㄷ포인트를 자동으로 만듭니다.
            //  사용자가 기본 주소에서 명시적으로 엔드포인트를 구성한 경우
            // 엔드포인트를 자동으로 만들지 않습니다.
 
            WebServiceHost wshost = new WebServiceHost(typeof(LoginService), new Uri("http://localhost:1017/"));
 
            /*
             soap 메시지 대신 http요청을 통해 노출되는 
             WCF windows communication foundation 웹 서비스에 대한
             엔드포인트를 구성하는데 사용되는 바인딩입니다.
 
             */
            WebHttpBinding bind = new WebHttpBinding();
 
            // 바인딩에서 처리할 수 있는 최대 크기를 가져오거나 설정
            bind.MaxReceivedMessageSize = 100;
            // 메시지 버퍼 관리자가 사용하는 최대 메모리 
            bind.MaxBufferSize = 100;
            //  MaxReceivedMessageSize 와 MaxBufferSize 가 다르면 에러나고
            // 두개의 숫자가 작으면 동작 안함 
 
            //AddServiceEndpoint : 지정된 계약, 바인딩, 엔드포인트 주소를 사용하여 서비스 엔드포인트를 호스팅된 서비스에 추가합니다.
                                                            // 계약,            //바인딩 //endpoint  주소
 
            // ServiceEndPoint: 클라이언트가 서비스를 찾아서 통신하게 해주는 서비스의 endpoint 
            ServiceEndpoint sep = wshost.AddServiceEndpoint(typeof(ILoginService), bind, "http://localhost:1017/MyServer");
 
            // Behaviors : 서비스에 대한 동작 설정
            // WebHttpBehavior 웹 프로그래밍 모델을 활성화 
            WebHttpBehavior whp = sep.Behaviors.Find<WebHttpBehavior>();
            if(whp != null)
            {
                // 메시지 형식을 자동으로 설정할지 말지
                whp.AutomaticFormatSelectionEnabled = true;
            }
            else
            {
                WebHttpBehavior webBehavior = new WebHttpBehavior();
                webBehavior.AutomaticFormatSelectionEnabled = false;
                // help page 사용 할지 말지 
                webBehavior.HelpEnabled = true;
                sep.EndpointBehaviors.Add(webBehavior);
            }
 
            // 서비스 성능 조절 할 수있는 런타임 처리량
            ServiceThrottlingBehavior stb = wshost.Description.Behaviors.Find<ServiceThrottlingBehavior>();
            if(stb == null)
            {
                stb = new ServiceThrottlingBehavior();
 
                //하나가 최대 수락하는 세션수
                stb.MaxConcurrentSessions = 1000;
                // 현재 처리되는 메시지 수 제한
                stb.MaxConcurrentCalls = 10;
                wshost.Description.Behaviors.Add(stb);
            }
 
            try
            {
                wshost.Open();
 
                ShowUserData();
 
                Console.WriteLine($"=== 로그인 서버 시작 : {sep.Address} === ");
            }
            catch(Exception e)
            {
                Console.WriteLine("{0}",e.Message.ToString());
                Console.WriteLine("=== 로그인 서버 정지 ===");
            }
            finally
            {
                while (true) ;
            }
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

WebServiceHost : 

https://docs.microsoft.com/ko-kr/dotnet/api/system.servicemodel.web.webservicehost?view=netframework-4.8

 

WebServiceHost Class (System.ServiceModel.Web)

WCF(Windows Communication Foundation) REST 프로그래밍 모델을 보완하는 파생된 클래스입니다.A derived class that compliments the Windows Communication Foundation (WCF) REST programming model.

docs.microsoft.com

WebHttpBehavior : 

https://docs.microsoft.com/ko-kr/dotnet/api/system.servicemodel.description.webhttpbehavior?view=netframework-4.8

반응형

'프로그래밍 > C#' 카테고리의 다른 글

GetLocalIP()  (0) 2019.11.13
Reflection : 객체 이름으로 객체 생성하기.  (0) 2019.07.29
Wcf : binding  (0) 2019.05.26
string , string builder 차이  (0) 2019.03.05
[c#] Mutex , semaphore, monitor  (0) 2019.02.17

댓글