2010年7月5日 星期一

Component / Web Service層 / API層

原出處
=====================Component======================

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ClassLibrary1
{
public class Class1
{
public string fname(int name,DateTime date)
{
StreamWriter sw = File.AppendText("c:\\a.txt");
sw.WriteLine(name.ToString() + "," + date);
sw.Close();
return name.ToString();
}
}
}

=====================WebService======================
//當然這邊是要先Add Reference上面的.dll檔,然後在架上IIS上

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using ClassLibrary1;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//如果使用設計的元件,請取消註解下行程式碼
//InitializeComponent();
}

[WebMethod(Description = "test")]
public string fname(int name, DateTime date)
{
ClassLibrary1.Class1 aa = new ClassLibrary1.Class1();
return (aa.fname(name,DateTime.Now).ToString());
}
}


====================== API ========================
//而這邊是要Web Reference上面的所發佈Web Service IP位置

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DateTime DTime = new DateTime();
int j = Convert.ToInt32(this.textBox2.Text);
for (int i = 1; i <= j; i++)
{
TestWebSite.Service service = new TestWebSite.Service();
service.fname(i, DTime).ToString();
textBox1.Refresh();
Thread.Sleep(Convert.ToInt32(this.textBox3.Text));
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

沒有留言:

張貼留言