WCF ile İstemci - Sunucu İletişimi (C#)

   Windows Communication Foundation (WCF) ile sunucu ve istemci arasında iletişim kuran, sunucuya belirli parametrelerle istek gönderen, sunucuda istek sonucu alabilen bir sınıf geliştirdik. Örnek uygulamaya buradan ulaşabilirsiniz. Örnek uygulama verilen metnin tersini döndürüyor. Aşağıda sınıfın kodu var. Eğer daha iyi bir çözümünüz ya da kullanımında probleminiz varsa yorum bırakabilirsiniz.

   Örnek kullamı sunucu için bu şekilde:
clsWCFLib cloWCFServer = new clsWCFLib();
cloWCFServer.StartServer("http://localhost:8000/Reverse");

   İstemci için kullanımı:
clsWCFLib cloWCFClient = new clsWCFLib();
cloWCFClient.Connect("http://localhost:8000/Reverse");
string strSonuc = cloWCFClient.SendMessage("Merhaba Dünya");

   Şuana kadar 3 farklı işletim sistemi ve domain bağımlı ve bağımsız ortamlarda kullandık. Daha geliştirilebilecek çok yönü var ama başlangıç olarak fena değil.

class clsWCFLib
{
    #region Interface
    [ServiceContract]
    public interface IProcessor
    {
        [OperationContract]
        string Process(string value);
    }

    public class Processor : IProcessor
    {
        public string Process(string value)
        {
            char[] retVal = value.ToCharArray();
            int idx = 0;
            for (int i = value.Length - 1; i >= 0; i--)
                retVal[idx++] = value[i];

            return new string(retVal);
        }
    }
    #endregion

    #region Client
    private IProcessor httpProxy;

    public void Connect(string strURI = "http://localhost:8000/Reverse")
    {
        ChannelFactory<IProcessor> httpFactory =
            new ChannelFactory<IProcessor>(
            new BasicHttpBinding(),
            new EndpointAddress(
                strURI));

        httpProxy =
            httpFactory.CreateChannel();
    }

    public string SendMessage(string strMsg)
    {
        string strResult;
        strResult = httpProxy.Process(strMsg);
        return strResult;
    }
    #endregion

    #region Server
    ServiceHost host;
    public void StartServer(string strURI = "http://localhost:8000/Reverse")
    {
        host = new ServiceHost(
            typeof(Processor),
            new Uri[]{
                new Uri(strURI)});

            host.AddServiceEndpoint(typeof(IProcessor),
                new BasicHttpBinding(), strURI);

            host.Open();
    }

    public void StopServer()
    {
        host.Close();
    }
    #endregion
}

Yorumlar

Bu blogdaki popüler yayınlar

DevExpress ve Telerik karşılaştırması

Install Remmina on macOS for easy connection to your servers via SSH, RDP, VNC.

Logo Apps Eğitim Notları