XML Web Service Giriş Uygulaması

Hiç yorum yok
Merhaba Arkadaşlar,

Bu bölümde xml web servicelere bakıyor olacağız.Amacımız örnek bir senaryo üzerinde ilerleyerek siparis gecebildiğimiz bir uygulama yazabilmek .Bu aşama da bir market olduğumuzu farz edelim ve ürünleri aldığımız tedarikcilerden elimizdeki satmış olduğumuz ürünler tam anlamıyla bitmeden mutlaka stoğumuzu artırabilmek için sipariş gecebilmemiz gerekmektedir.Biz bu senaryoyu ele alıp örnek uygulamımızı yazalım.

Öncelikle tedarikciden satmış olduğu ürünlerin listesini alalım .Bunun için tedarikçinin bize yolladığı web service adresini kullanarak elde edebiliriz.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Xml;
using System.Data;
 
namespace Tedarikci
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
 
        [WebMethod]
        public string UrunListesi()
        {
            SqlConnection cnn;
            XmlReader rdr = null;
            string donendeger = null;
            string baglantim = @"Server=.;Integrated Security =SSPI;Initial Catalog=KuzeyRuzgari";
            cnn = new SqlConnection(baglantim);
 
            try
            {
                if (cnn.State == ConnectionState.Closed)
                {
                    cnn.Open();
                }
                string sorgu = "Select UrunID ,UrunAdi ,Fiyat from Urunler where SatilmiyorMu=1 order by UrunAdi desc for xml auto,root('Urunler')";
                SqlCommand cmd = new SqlCommand(sorgu, cnn);
                rdr = cmd.ExecuteXmlReader();
                rdr.Read();
                while (rdr.ReadState != ReadState.EndOfFile)
                {
                    donendeger += rdr.ReadOuterXml();
                }
            }
            catch
            {
 
            }
            finally
            {
                cnn.Close();
                rdr.Close();
            }
            return donendeger;
        }
    }
}
 
Şimdi ise bu yazılan web service kullanmak gerekmekte öncelikle uygulamamıza 
add service referans bölümünden tedarikcinin web service ni alalım.Daha Sonra
uygulamamız üzerinde web servicedeki ürün listesi döndüren metodu kullanalım 
 
 

 protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                VeriGetir();
            }
        }
 
        private void VeriGetir()
        {
            UrunSipList.Service1 sw = new UrunSipList.Service1();
            XmlDataSource1.Data = sw.UrunListesi();
            drpUrun.DataSource = XmlDataSource1;
            drpUrun.DataTextField = "UrunAdi";
            drpUrun.DataValueField = "UrunID";
            drpUrun.DataBind();
        }
 Daha Sonra siparis gecmek için bu aşamada (video uzamasın diye Streamwriter Kullandım)
veri tabanına sipariş gececek bölümü yazalım
 
 protected void drpUrun_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtAdi.Text = drpUrun.SelectedItem.Text;
        }
 
        protected void btnSiparis_Click(object sender, EventArgs e)
        {
            TedarikSiparisGec.Service1 tds = new TedarikSiparisGec.Service1();
            tds.SiparisGec(txtAdi.Text,Convert.ToInt32(txtMiktar.Text));
 
        } 

Tabi birde SiparisGec Webservicene ihtiyacımız var onu yazalım .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
 
namespace MigrosService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
 
        [WebMethod]
        public void SiparisGec(string UrunAdi,int Adet)
        {
           
            StreamWriter stw = new StreamWriter(@"C:\siparisler.txt");
            stw.WriteLine(UrunAdi + "Adlı Urunden " + Adet + "Adet sipariş Gecildi");
            stw.Close();
            
        }
    }
}
 


Hiç yorum yok :

Yorum Gönder

Soru Görüş önerileriniz için gmail plus üzerinden + layın müsait olduğumda mutlaka yanıt dönüyor olacağım.