Spiga

Windows Communication Foundation - First Service

Mayıs 26, 08 by aligorkem

This article describes “How to develop basic distributed service and client.” WCF provides runtime environment for your services. Distributed applications have two common section, server and client.

This article contains :         

  1. Service
  2. Client
  3. Create WCF Service Library Project
  4. Service Contract
  5. Create WCF Client
  6. Test
  7. Download Source Code

1. Service
WCF Services are publishing to contracts via HTTP or TCP. We can define a service, and we have to publish to service for clients. Services have a unique address and client using this address via connect to service. All services have to endpoints. If  we publish to object via service, we will use to endpoint. 

2. Client
Clients are connecting to WCF service but clients don’t know how to connect service and use to methods. We have to design an interface for a client. This interface describes to how to using methods and have return values.Many articles describe endpoint, ABC or such as same technology, but we want just a basic WCF service and we want object send to a client from WCF service.

3. Create WCF Service Library Project
My development environment is Visual Studio 2008 and .Net Framework 3.5. I opened Visual Studio 2008 and selected “New Project”, I saw WCF Service Library when selected WCF Project Type.


If we select “WCF Service Library” template and create a project, Visual studio will prepare WCF Service Library.



Visual Studio created some classes. We have to an Interface, client and server will use this Interface.
 
4. Service Contract
The service contract describes a particular service endpoint and its supported operations. To create a service contract we will define an interface with related methods. Describe which operations the client can perform on the service, these attribute maps a CLR interface.We have a CompositeType class, this class is a custom class and used by server and client.

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

// Use a data contract as illustrated in the sample below to add composite types to service operations
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }


If we want use this class by service or client, we will mark as [DataContract].

Service1 class and IService1 Interface :
This service class is automatic created by Visual Studio. This class implement IService1 interface and describe methods.

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

 

If we use GetData method and pass the integer value, this service will return string value as “You entered passedValue”.

5. Create WCF Client
This example include two methods and we will use this service from Console Application. I selected Create New Console Application and entered “WcfClient”.


I selected WcfClient project then right click and selected “Add Service References” I entered WCF Service URL addres and click to “Go” and i view two methods for Service1. Default namespace is “ServiceReference1”


Program.cs

private Program()
        {
            try
            {
                Console.WriteLine( String.Format("Client connecting : {0}",GetCurrentNowTimte()));
                ServiceReference1.Service1Client client = new Service1Client();
                client.Open();
                Console.WriteLine(String.Format("Client connected : {0}", GetCurrentNowTimte()));

                Console.WriteLine("Please input your age.");
                string readFromUser = Console.ReadLine();
                if (readFromUser != null)
                {
                    string returnValue = client.GetData(int.Parse(readFromUser));
                    Console.WriteLine(returnValue);
                    Console.ReadLine();
                }
            }
            catch (EndpointNotFoundException ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Please input validate number.");
                Console.ReadLine();
            }
        }

        private string GetCurrentNowTimte()
        {
            return DateTime.Now.ToLongTimeString();
        }
}


6. Test

I opened WCF-Service solution with Visual Studio 2008 and pressed F5 or selected Start Debugging from debug menu.

I saw WCF Test window when I pressed F5 button. If we want to test wcf application, we will double click to service project.



WCF First Service project started and waiting for any connection from 8731 port. IService1 interface has two methods and these methods useable from any WCF Client.

Client Test
If you run a client project before opened wcf server, you will see under screen. I entered my age and i saw response from server.


7. Download Source Code

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • eKudos
  • E-mail this story to a friend!
Add your comment

One response for this post

  1. Volkan KARAKAYA Says:

    insan erp felan yazar. anca int al geri hellö de.

    :) eline sağlık karşim.

Leave a Reply