ASP.NET

68.C# ASP.NET - Xml 웹 서비스

Godffs 2009. 10. 26. 08:48
반응형
XML 웹 서비스
- 로직구현
[1] 메서드 내에서 직접 구현
[2] 따로 클래스화 시켜서 호출
[3] DLL 파일로 만들어서 호출
 = GAC에 DLL파일 보환 후 호출
-----------------------------------
-원격에서 호출 ( 테스트 )
[4] Xml WebService 호출
 = .Net Remoting 호출

덧셈을 구하는 간단한 예제
FrmMethod.aspx

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>       

        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    </div>

    </form>

</body>


[그림68-1]


[1] 메서드 내에서 직접 구현
FrmMethod.aspx.cs

protected void Button1_Click(object sender, EventArgs e)

{

   int a = Convert.ToInt32(TextBox1.Text);

   int b = Convert.ToInt32(TextBox2.Text);

 

   //메서드화

   int r = MyMath(a, b);

   TextBox3.Text = r.ToString();

}


private int MyMath(int a, int b)

{

   return (a + b);

}


[2] 클래스화 시켜서 호출
해당 웹 프로젝트 - ASP.NET 폴더 추가 - App_Code 폴더 추가
App_Code폴더 -> Class 클래스 파일 추가 ( MyMath.cs )
MyMath.cs

public class MyMath

{

    public int Sum(int a, int b)

    {

        return (a + b);

    }

}


FrmMethod.aspx.cs

protected void Button1_Click(object sender, EventArgs e)

{

   int a = Convert.ToInt32(TextBox1.Text);

   int b = Convert.ToInt32(TextBox2.Text);

 

   MyMath mm = new MyMath();

   int r = mm.Sum(a, b);

 

   TextBox3.Text = r.ToString(); 

}


[3] DLL 파일로 호출
해당 웹 프로젝트에서 - 추가 - 새 프로젝트 - 클래스라이브러리 (MethodClass) - 클래스 추가 (MathMethod.cs)
MathMethod.cs

namespace MethodClass

{

    public class MyMath

    {

        public int Sum(int a, int b)

        {

            return (a + b);

        }

    }

}


빌드를 하게 되면 해당 프로젝트에 Bin폴더가 자동 추가 되면서 MethodClass 이름으로

DLL파일이 추가됩니다.


FrmMethod.aspx.cs

protected void Button1_Click(object sender, EventArgs e)

{

   int a = Convert.ToInt32(TextBox1.Text);

   int b = Convert.ToInt32(TextBox2.Text);

 

   MethodClass.MyMath my = new MethodClass.MyMath();

   int r = (a + b);

 

   TextBox3.Text = r.ToString();

}


[4] Xml WebService 호출
해당 웹 프로젝트에서 - 새항목 추가 - 웹 서비스 (MyWebService.asmx)
추가하게 되면 App_Code 폴더에 MyWebService.cs가 추가되어져 있습니다.
MyWebService.cs

public class MyWebService : System.Web.Services.WebService

{

    [WebMethod]

    public int Sum(int a, int b)

    {

        return (a + b);

    }

}


해당 웹 프로젝트에 웹 서비스 참조하기

[그림68-2]



FrmMethod.aspx.cs

protected void Button1_Click(object sender, EventArgs e)

{

   int a = Convert.ToInt32(TextBox1.Text);

   int b = Convert.ToInt32(TextBox2.Text);

 

//[4] 서비스화

   localhost.MyWebService mws = new localhost.MyWebService();

  

   int r = mws.Sum(a, b);

   TextBox3.Text = r.ToString();

}


반응형