연습: Windows Forms에서 XML Web services 호출
업데이트: 2007년 11월
XML Web services는 HTTP, XML, XSD, SOAP, WSDL 등의 표준 프로토콜을 사용하여 느슨하게 결합된 환경에서 메시지를 교환하는 응용 프로그램을 개발할 수 있게 해 주는 Visual Studio의 기능입니다. 이러한 메시지는 구조화되고 형식이 지정되거나 느슨하게 정의된 것일 수 있습니다. 웹 서비스 기능은 표준 프로토콜을 기반으로 하기 때문에 웹 서비스 응용 프로그램은 광범위한 종류의 구현, 플랫폼 및 장치와 통신할 수 있습니다. 자세한 내용은 관리 코드의 웹 서비스를 참조하십시오.
웹 서비스를 사용하여 Windows Forms의 기능을 향상시킬 수 있습니다. Windows Forms를 웹 서비스에 연결하려면 간단히 웹 서비스 메서드를 호출하면 됩니다. 그러면 웹 서버에서 메서드에 대한 호출을 처리한 다음 결과를 반환합니다.
웹 서비스 메서드에는 동기적 웹 서비스 메서드와 비동기적 웹 서비스 메서드가 있습니다. 동기적 웹 서비스 메서드를 호출하는 경우, 호출자는 웹 서비스에서 응답이 올 때까지 기다린 다음에야 작업을 계속할 수 있습니다. 반면, 비동기적 웹 서비스 메서드를 호출하는 경우에는 웹 서비스의 응답을 기다리는 동안 호출 스레드에서 작업을 계속할 수 있으므로 클라이언트 응용 프로그램에서 기존 스레드의 집합을 효율적으로 사용할 수 있습니다. 동기적 및 비동기적 웹 서비스 메서드 작업에 대한 자세한 내용은 관리 코드의 웹 서비스 액세스를 참조하십시오.
동기적 웹 서비스 메서드
동기적 웹 서비스 메서드를 호출하는 경우에는 메서드를 호출하고, 서버에서 계산을 수행하고 값을 반환할 때까지 기다린 후 Windows Form의 나머지 코드를 진행합니다.
XML Web services를 만들려면
웹 서비스 응용 프로그램을 만듭니다. 자세한 내용은 관리 코드로 웹 서비스 만들기를 참조하십시오.
솔루션 탐색기에서 .asmx 파일을 마우스 오른쪽 단추로 클릭하고 코드 보기를 선택합니다.
더하기를 수행하는 웹 서비스 메서드를 만듭니다. 다음 코드 예제에서는 두 개의 정수를 선택하여 더한 다음 합계를 반환하는 웹 서비스 메서드를 보여 줍니다.
<WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer Return x + y End Function
[WebMethod] public int WebAdd(int x, int y) { return x + y; }
/** @attribute WebMethod() */ public int WebAdd(int x, int y) { return x + y; }
곱하기를 수행하는 또다른 웹 서비스 메서드를 만듭니다. 다음 코드 예제에서는 두 개의 정수를 선택하여 곱한 다음 곱한 결과를 반환하는 웹 서비스 메서드를 보여 줍니다.
<WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer Return x * y End Function
[WebMethod] public int WebMultiply(int x, int y) { return x * y; }
/** @attribute WebMethod() */ public int WebMultiply(int x, int y) { return x * y; }
빌드 메뉴에서 솔루션 빌드를 선택합니다. 이 프로젝트에서 생성된 .asmx 파일을 살펴보면 웹 서비스에 대해 좀 더 많은 것을 배울 수도 있습니다. 이제 위에서 만든 웹 서비스를 Windows Form에서 호출할 수 있습니다.
XML Web services를 동기적으로 호출하려면
새 Windows 기반 응용 프로그램을 만듭니다. 자세한 내용은 방법: Windows 응용 프로그램 프로젝트 만들기를 참조하십시오.
보안 정보:
웹 메서드를 호출하려면 WebPermission 클래스에서 부여한 권한 수준이 필요합니다. 부분 신뢰 컨텍스트에서 실행 중인 경우에는 프로세스에서 예외를 throw할 수 있습니다. 자세한 내용은 코드 액세스 보안 기본 사항을 참조하십시오.
솔루션 탐색기에서 프로젝트의 참조 노드를 마우스 오른쪽 단추로 클릭한 다음 웹 참조 추가를 클릭합니다.
웹 참조 추가 대화 상자가 열립니다.
주소 상자에 다음 XML Web services URI를 입력한 다음 Enter 키를 누릅니다.
https://localhost/WebService1/Service1.asmx
이 URI는 이전 단계에서 만든 웹 서비스의 URI입니다. 웹 참조 추가 대화 상자의 왼쪽 창에 WebAdd 및 WebMultiply 웹 메서드가 표시됩니다.
참조 추가를 클릭합니다.
도구 상자에서 TextBox 컨트롤 세 개와 Button 컨트롤 두 개를 추가합니다. 텍스트 상자는 숫자를 입력하고 표시하는 데 사용되며 단추는 계산을 시작하고 웹 서비스 메서드를 호출하는 데 사용됩니다.
컨트롤의 속성을 다음과 같이 설정합니다.
컨트롤
속성
텍스트
TextBox1
Text
0
TextBox2
Text
0
TextBox3
Text
0
Button1
Text
Add
Button2
Text
Multiply
폼을 마우스 오른쪽 단추로 클릭한 다음 코드 보기를 선택합니다.
클래스의 멤버로서 웹 서비스의 인스턴스를 만듭니다. 이전에 웹 서비스를 만든 서버의 이름을 알고 있어야 합니다.
' Replace localhost below with the name of the server where ' you created the Web service. Dim MathServiceClass As New localhost.Service1()
localhost.Service1 MathServiceClass = new localhost.Service1();
localhost.Service1 MathServiceClass = new localhost.Service1();
Button1의 Click 이벤트에 대한 이벤트 처리기를 만듭니다. 자세한 내용은 방법: 디자이너를 사용하여 이벤트 처리기 만들기를 참조하십시오.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Create instances of the operands and result. Dim x, y, z As Integer ' Parse the contents of the text boxes into integers. x = Integer.Parse(TextBox1.Text) y = Integer.Parse(TextBox2.Text) ' Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebAdd(x, y) TextBox3.Text = z.ToString End Sub
private void button1_Click(object sender, System.EventArgs e) { // Create instances of the operands and result. int x, y, z; // Parse the contents of the text boxes into integers. x = int.Parse(textBox1.Text); y = int.Parse(textBox2.Text); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebAdd(x, y); textBox3.Text = z.ToString(); }
(Visual C#) 폼의 생성자에 다음 코드를 배치하여 이벤트 처리기를 등록합니다.
this.button1.Click += new System.EventHandler(this.button1_Click); private void button1_Click (Object sender, System.EventArgs e) { // Create instances of the operands and result. int x, y, z; // Parse the contents of the text boxes into integers. x = Integer.parseInt(textBox1.get_Text()); y = Integer.parseInt(textBox2.get_Text()); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebAdd(x, y); textBox3.set_Text(""+z); }
마찬가지 방식으로 Button2의 Click 이벤트에 대한 이벤트 처리기를 만들고 아래의 코드를 추가합니다.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' Create instances of the operands and result. Dim x, y, z As Integer ' Parse the contents of the text boxes into integers. x = Integer.Parse(TextBox1.Text) y = Integer.Parse(TextBox2.Text) ' Call the WebMultiply Web service method from the instance of the Web service. z = MathServiceClass.WebMultiply(x, y) TextBox3.Text = z.ToString End Sub
private void button2_Click(object sender, System.EventArgs e) { // Create instances of the operands and result. int x, y, z; // Parse the contents of the text boxes into integers. x = int.Parse(textBox1.Text); y = int.Parse(textBox2.Text); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebMultiply(x, y); textBox3.Text = z.ToString(); }
(Visual C#) 폼의 생성자에 다음 코드를 배치하여 이벤트 처리기를 등록합니다.
this.button2.Click += new System.EventHandler(this.button2_Click); private void button2_Click (Object sender, System.EventArgs e) { // Create instances of the operands and result. int x, y, z; // Parse the contents of the text boxes into integers. x = Integer.parseInt(textBox1.get_Text()); y = Integer.parseInt(textBox2.get_Text()); // Call the WebAdd Web service method from the instance of the Web service. z = MathServiceClass.WebMultiply(x, y); textBox3.set_Text(""+z); }
F5 키를 눌러 응용 프로그램을 실행합니다.
처음 두 개의 텍스트 상자에 값을 입력합니다. Add 단추를 누르면 세 번째 텍스트 상자에 합계가 표시됩니다. Multiply 단추를 누르면 세 번째 텍스트 상자에 곱한 결과가 표시됩니다.
참고:
웹 서비스를 처음 호출하면 서버에서 웹 서비스가 인스턴스화되므로 해당 서버에서 호출을 처리하는 데 약간 시간이 걸립니다. 응용 프로그램에서 단추를 누를 때 이 점에 유의해야 합니다. 다음 단원에서는 이러한 지연을 해결합니다.
다음 단계
비동기적 웹 서비스 메서드를 호출할 경우, 응용 프로그램에서는 웹 서비스의 응답을 기다리는 동안에도 작업을 계속할 수 있으므로 클라이언트 응용 프로그램에서 리소스를 효율적으로 사용할 수 있습니다.
자세한 내용은 방법: 관리 코드의 웹 서비스 비동기 액세스를 참조하십시오.
참고 항목
작업
방법: Windows Forms BindingSource를 사용하여 웹 서비스에 바인딩
개념
Windows Forms과 Web Forms 간의 선택