tạo webservice giải phương trình bậc hai

Posted: 15/06/2011 in ASP.NET, C# Window Form
Tags: , ,

bài này tôi sẽ hướng dẫn các bạn tạo 1 webservice đơn giản , giải phương trình bậc 2 : aX*X+bX+c=0

1.Tạo webservice

đầu tiên tôi tạo 1 solution với tên WebservicePTBH

https://i2.wp.com/nzoqla.bay.livefilestore.com/y1pZZeWB-vFhbjVIHBFzX8b92FrBnklLiymnBfvjOrWm07ff0aKwvb9JbAxRPdkv9Gpf02hpJiGn_VKQoIMUaOn9wf1OWGwuyWx/1.png

File Service1.asmx.cs tôi chỉnh sửa lại như sau


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServicePTBH
{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string GiaiPT(float a, float b, float c)
{
string ketluan = "";
float delta = b * b - 4 * a * c;
if (a == 0)
{
if (b == 0)
{
if (c == 0) ketluan = "Vô số nghiệm";
else ketluan = "Vô nghiệm";
}
else ketluan = "Có 1 nghiệm : X=" + (c / b);
}
else
{
if (delta < 0) ketluan = "Vô nghiệm";
else if (delta == 0) ketluan = "Có nghiệm kép X1=X2=" + (-b / 2 / a);
else ketluan = "Có 2 nghiệm phân biệt :  X1=" + ((-b + Math.Sqrt(delta)) / 4 / a)
                   + " , X2=" + ((-b - Math.Sqrt(delta)) / 4 / a);
}
return ketluan;
}
}
}

khi nhấn F5 để run service này , tôi được địa chỉ sau : http://localhost:9810/Service1.asmx
ghi nhớ lấy địa chỉ này

2.Test webservice vừa tạo

tôi gõ địa chỉ trên ( http://localhost:9810/Service1.asmx ) vào trình duyệt web

https://i2.wp.com/nzoqla.bay.livefilestore.com/y1pbKtjY_kyn3TWCmlYXBVxusUQsENA2ibf2tSoxazg5IoMX7VZvBFh8pNB-FOCFwWKA19gzo6M5d7sestvtHzL-fyfdVxJcVaF/2.png

bấm vào GiaiPT

https://i0.wp.com/nzoqla.bay.livefilestore.com/y1pmTcl6tN-JkMH-QQR3mkDxy97HncmC8OotB3uMILJ846LdEuyG11P3d7ShvsWoSf_JQInWl0X0AvQMpBQXyCKaR7FtbJLKM17/3.png

điền tham số a,b,c nhấn Invoke ta được kết quả

https://i2.wp.com/nzoqla.bay.livefilestore.com/y1ppkzjagJOKU87elZ7UWoKb1hvpgZkiZdY9IMj9SPVXVSDfqD2wqBEtgZaybWFV6CDr_yGdVx_abn__Xr-X0pHoBjxvS4eI11u/4.png

ok ! đã xây dựng thành công service

3.Sử dụng webservice vừa tạo trong window form , webform

ta tạo 2 project winform,webform trong solution

https://i2.wp.com/nzoqla.bay.livefilestore.com/y1pIrwxYh6AQKDhD0pN3yqIqCGeRirqQ_WE2itZUFyHtp0PCGM0F-c0DGmCvXtpV1c9IC4SY_qGS8oKmDU9yTaPHEoUPLuTzcH-/5.png

tiếp theo tôi add reference webservice lần lượt cho 2 project này

chuột phải vào Reference của mỗi project chọn Add Service Reference sẽ được bảng sau

https://i2.wp.com/nzoqla.bay.livefilestore.com/y1ptPbvC-iOhVR0vT_NAB3xzR1WYbsrId76dRjzOmNHpzYlRFsjdadkK-4tpgn-Nzd9SQK29qcd1VKndFadhusfCN9Sgqk-aws-/6.png

điền địa chỉ mà tôi đã lưu ý ở trên , sau đó đặt tên là ZstarService và nhấn OK

trong Form1.cs tôi code như sau :


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

namespace test_service
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ZstarService.Service1SoapClient soap;
private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
try
{
if (soap == null) soap = new ZstarService.Service1SoapClient();
float a, b, c;
a = float.Parse(this.textBox1.Text);
b = float.Parse(this.textBox2.Text);
c = float.Parse(this.textBox3.Text);
this.label5.Text = "Kết quả : " + soap.GiaiPT(a, b, c);
}
catch { }
}
}
}

trong Default.aspx.cs tôi code như sau :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test_service_Webform
{
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

ZstarService.Service1SoapClient soap = new ZstarService.Service1SoapClient();
float a, b, c;
a = float.Parse(this.TextBox1.Text);
b = float.Parse(this.TextBox2.Text);
c = float.Parse(this.TextBox3.Text);
this.Label1.Text = "Kết quả : " + soap.GiaiPT(a, b, c);

}
}
}

Kết quả :

khi run project winform

https://i0.wp.com/nzoqla.bay.livefilestore.com/y1pkx8Z4uG6vcgIjZbWeR_8f272jEVECFPO656-F4tZoWByN0lUrvzi4YxgfT-8l9cbxi5ljgyjID5IClbkJ8VYFte3zjcIpadH/7.png

khi run project webform

https://i0.wp.com/nzoqla.bay.livefilestore.com/y1pmCvOUMTcmmsXCOgwvr66iAhCRT7YgjQmpRcbJ9NYK9tKFTzA8U2dYCQHiCld87xwvxDyYvzfgGHuVcAb7sUg2MosFHOPxrop/8.png

vừa rồi tôi đã hướng dẫn các bạn tạo 1 webservice,test webservice,sử dụng webservice

lưu ý : webservice vừa tạo ở trên , có thể sử dụng cho các ngôn ngữ khác như java,C++…

nếu dùng ngôn ngữ khác thì ko thể gọi tường minh như trên được mà phải dựa vào giá trị xml mà webservice trả về để đọc kết quả

Comments
  1. I have been exploring for a little for any high-quality articles or weblog posts in this sort of space . Exploring in Yahoo I ultimately stumbled upon this site. Reading this information So i’m satisfied to exhibit that I have a very excellent uncanny feeling I found out just what I needed. I so much no doubt will make sure to do not fail to remember this web site and provides it a look regularly.

  2. Unquestionably consider that that you stated.
    Your favourite justification seemed to be on the internet the simplest factor to
    keep in mind of. I say to you, I certainly get irked whilst
    people consider worries that they plainly do not recognize about.
    You managed to hit the nail upon the top and also defined out the
    entire thing without having side-effects , folks could take a signal.
    Will probably be again to get more. Thanks

  3. I’m no longer certain where you are getting your information, however good topic. I must spend some time finding out more or understanding more. Thanks for excellent info I was looking for this info for my mission.

  4. Hi just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading correctly. I’m not
    sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same outcome.

  5. An intriguing discussion is definitely worth comment. There’s no doubt that that you should publish more on this issue, it might not be a taboo matter but generally people do not discuss such subjects. To the next! Best wishes!!

  6. Emily says:

    Your current report offers verified helpful to me. It’s very useful and you’re clearly very knowledgeable of this type. You possess exposed our eyes in order to numerous thoughts about this specific matter with interesting and sound content.

  7. Xanax says:

    Very shortly this site will be famous amid all blogging and site-building users,
    due to it’s good posts

  8. viagra says:

    Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely excellent. I actually like what you have acquired here, really like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can’t wait to read
    far more from you. This is really a terrific web site.

  9. Although this blog is dedicated mainly to
    the paleo/low-carb diet nor explain in what consist this type paleo food of diet.

    I am now honored to say that paleo food it was delicious and couldn’t believe it was made from avocados.

  10. I love what you guys tend to be up too. This type of clever work and reporting!
    Keep up the great works guys I’ve included you guys to my personal blogroll.

  11. Cannoli can even be purchased at an Italian bakery.
    You will only invite your best friends and closest relatives in
    this case. If you are having a destination wedding, why not treat your guests with
    an indulgent gift basket delivered right to their hotel rooms.

  12. Margie says:

    What’s up, for all time i used to check blog posts here early in the dawn, because i love to learn more and more.

  13. Be sure the DJ is dressed appropriately during set-up and takedown of
    equipment. Brides and grooms are increasingly finding that the standard
    wedding program doesn’t work for them, and they’re experimenting with
    new options. Make sure they provide standard universally accepted payment methods that provide consumer protection such as Pay – Pal, Money – Gram or with most credit cards.

  14. Excellent way of explaining, and pleasant paragraph to obtain data regarding my presentation subject matter, which i am
    going to present in university.

  15. 2010 bank holidays in India are a blend of national events, spiritual festivals and community celebrations.
    This happy-making holiday offers everyone free rein for plastering
    cheery grins on our faces and hugging those grumpy, grimacing
    folks in our lives. A festive potluck meal, tree trimming party or other holiday
    event would probably be much appreciated.

  16. seo book says:

    Thanks , I’ve just been looking for info approximately this topic for ages and yours is the greatest I’ve
    came upon so far. However, what about the bottom line?
    Are you positive concerning the source?

  17. You can definitely see your enthusiasm within the article you write.
    The sector hopes for more passionate writers like you who aren’t afraid
    to mention how they believe. All the time go after your heart.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s