Windows Communication Foundation Uygulaması

Dökümanın Forum Konusu Linktedir : http://www.tahribat.com/Forum-Dokuman-Windows-Communication-Foundation-Uygulamasi-140134/2/

1.    Önce interface imizi hazırlıyoruz.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.ServiceModel;
        using System.Text;
        namespace WelcomeServiceLibrary
{
    [ServiceContract]
    public interface IWelcomeService
    {
       [OperationContract]
        string GetWelcome(string mesaj);
        [OperationContract]
        DateTime Getdate();
        [OperationContract]
        int topla(int x, int y);
        [OperationContract]
        int Carp(int x, int y);
    }
}

2.İnterface deki metodların ne yapacağını tasarlıyoruz.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WelcomeServiceLibrary
{
    public class WelcomeService:IWelcomeService
    {
        public string GetWelcome(string mesaj)
        {
            if (mesaj == null) throw new ArgumentNullException("mesaj");
            mesaj = "Ziverbey Bey Hoşgeldiniz";
            return mesaj;
            //mesaj = "Ziverbey Bey Hoşgeldiniz";
            //return mesaj;
        }
        public DateTime Getdate()
        {
            return DateTime.Now;
        }
        public int topla(int x, int y)
        {
            int z = x + y;
            return z;
        }
        public int Carp(int x, int y)
        {
            int z = x*y;
            return z;
        }
    }
}

3.Host uygulamasını geliştirmeye başlıyoruz.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using WelcomeServiceLibrary;
using System.ServiceModel.Description;
namespace HostApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ServiceHost _host;
        private void btn_SrvStart_Click(object sender, EventArgs e)
        {
            Uri adres = new Uri("http://localhost:1245/ismail_Services/Wservice");
            //_host = new ServiceHost(typeof(WelcomeService), new Uri("http://localhost:1245/ismail_Services/Wservice"));
           _host =new ServiceHost(typeof(WelcomeService),adres);
            //Servisin abc si yani endpoint tanımlaması yapılıyor.
            //_host.AddDefaultEndpoints(); default endpoint tanımlaması fakat ben manuel yapacağım..
           _host.AddServiceEndpoint(typeof (IWelcomeService), new WSHttpBinding(), adres.ToString());
           //istemciler http üzerinden bu servise talepte bulunup wsdl içeriklerini alabilir default olarak kapalı
           ServiceMetadataBehavior MDBeh =new ServiceMetadataBehavior();
           MDBeh.HttpGetEnabled = true;
           //host a metadatabehavior özelliğinin true olduğu bildiriliyor.
           _host.Description.Behaviors.Add(MDBeh);
           _host.Open();
           lbl_SrvDurum.Text = "Servis açıldı ve çalışıyor...";
           lbl_Durum2.Text = _host.State.ToString();
        }
       private void btn_SrvStop_Click(object sender, EventArgs e)
       {
           _host.Close();
           lbl_SrvDurum.Text = "Servis Durduruldu ve Çalışmıyor";
           lbl_Durum2.Text = _host.State.ToString();
      }
   }
}

Servisimizin çalışıp çalışmadığını kontrol ediyoruz..

4.Client için Proxy ve Konfigürasyon dosyası üretiyoruz..

Bu iki dosyayı istemci uygulamamıza ilave edeceğiz.

5.İstemci (Client) uygulamamızı oluşturup app.config ve Proxy.cs dosyalarını ekleyelim.

6.  Class diyagramda Görelim

Gördüğünüz üzere servisimizi yazarken kullandığımız methodların başına  [OperationContract] yazmazsak burada o metodu göremeyiz ve kullanamayız.

7.  İstemcimizi (CLIENT) kodlayalım.

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 ClientApp	
{
    public partial class Form1 : Form	
    {	
        public Form1()	
        {	            InitializeComponent();	
        }	
        private void button1_Click(object sender, EventArgs e)	
        {	
            WelcomeServiceClient client = new WelcomeServiceClient("WSHttpBinding_IWelcomeService");	
            lbl_durum1.Text = "Servise bağlandım";	
            client.Close();	
        }	
        private void btn_tarih_Click(object sender, EventArgs e)	
        {	
            WelcomeServiceClient client = new WelcomeServiceClient("WSHttpBinding_IWelcomeService");	
            string result = client.Getdate().ToString();	
            lbl_tarih.Text = result;	
            client.Close();	
        }	
    }	
}

Client ı sadece getdate() metodu için yazdım.

8.  Ve mutlu son uygulamamızı çalıştırıyoruz. Metodu çağırabildiğimizi görüyoruz.

Not: Bazı açıklamalarım muadil olabilecek kodlar örn; _host nesnesinin yaratıldığı bölüm..

Not2: Tüm referanslarda System.ServiceModel mutlaka olmalıdır! Ek olarak ta Hostapplication a WelcomeServiceLibrary referans geçilmelidir.

Bu doküman TBT adına tarafımdan hazırlanmıştır.

Tarih:
Hit: 3358
Yazar: rappermcs



Yorumlar


Siftahı yapan siz olun
Yorum yapabilmek için üye girişi yapmalısınız.