Xml Web Service Session Kullanımı

Hiç yorum yok
Merhaba Arkadaşlar

Bu bölümde Xml Web Service'lerde  Session kullanımına bakıyor olacağız.örneğimizi basit bir uygulama üzerine indirgeyerek uygulamayı geliştiryor olacağız.

Öncelikle Xml Web Service'lerde Session kullanacaksak aşağıdaki gibi WebMethod attribute içinde sessiona izin veriyor olacağız .Daha sonrasında config dosyamızı açıp aşağıdaki gibi cookies izin veriyor olacağız.

Service içinde yazılan method:

Config Dosyası :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="Service1Soap" allowCookies="true" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1078/Service1.asmx" binding="basicHttpBinding"
                bindingConfiguration="Service1Soap" contract="ServiceReference1.Service1Soap"
                name="Service1Soap" />
        </client>
    </system.serviceModel>
</configuration>
 
 Şimdi ise uygulammızda yazmış olduğumuz kodlara bakalım 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.IO;
 
namespace WebServiceUygulama2
{
    /// <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(EnableSession=true)]
        public string  LoginOlKeyli(string KullaniciAdi,string Paralo)
        {
            string sonuc="";
            if (KullaniciVarmi(KullaniciAdi))
            {
                sonuc = Guid.NewGuid().ToString();
                Session["kul"] = sonuc;
            }
 
            return sonuc;
        }
 
        [WebMethod(EnableSession=true)]
        public bool SiparisGec(string UrunAdi, string miktar, string key)
        {
            if (Session["kul"] != null && Session["kul"].ToString() == key)
            {
                StreamWriter sw = new StreamWriter(@"C:\SessionSiparis.txt",true);
                sw.WriteLine(DateTime.Now + " Tarihinde " + UrunAdi + "  Adlı Urunden" + miktar + "  Adet Sipariş Geçildi");
                sw.Close();
                return true;
            }
            return false;
 
        }
 
        public bool KullaniciVarmi(string Adi)
        {
            bool sonuc = false;
            SqlConnection cnn = new SqlConnection("Server=.;Database=DuffDunnAyakkabi;trusted_connection=true");
            SqlCommand cmd = new SqlCommand("Select * from Musteri where Adi=@Adi", cnn);
            cmd.Parameters.AddWithValue("@Adi", Adi);
            if (cnn.State == ConnectionState.Closed)
            { cnn.Open(); }
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                sonuc = true;
            }
            rdr.Close();
            cnn.Close();
 
            return sonuc;
        }
    }
}
 
Daha Sonrasında References bölümüne tıklayıp bu service kullanacak uygulama 
üzerinde webservice'mizi  refere edelim
 
 
 
 

Daha sonra servicemizi  kullanacak uygulama içersinde servicemizi çağırıp kulllanalım 


private void btnSiparisGec_Click(object sender, EventArgs e)
{
            ServiceReference1.Service1SoapClient sw = new ServiceReference1.Service1SoapClient();
            string donendeger= sw.LoginOlKeyli(txtKulAdi.Text, txtSifre.Text);
             
            bool sonuc=sw.SiparisGec(txtUrunAdi.Text, txtMiktar.Text, donendeger);
            if (sonuc)
            {
                MessageBox.Show("Siparis Gecildi");
            }
            else
            {
                MessageBox.Show("Login olunuz");
            }
        }
 Video Anlatım :
 



 


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.