If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Creating and consuming your first WCF service

by Farooq Kaiser 24. September 2009 14:57

In this article, i will examine how to create and consume a WCF service. WCF is next-generation programming platform and runtime system for building, configuring and deploying service-oriented applications. For more details please see here.

Creating a WCF service

I will create a stock service to demonstrate WCF service. To create a WCF service please follow these steps.

  1. Launch a visual studio 2008
  2. Click on File �> new -> project then select WCF service application.
  3. It will create a WCF service application template.

I will delete the default contract and then create a IStock contract as shown below.

[ServiceContract]
    public interface IStock
    {
        [OperationContract]
        Stock GetStock(string Symbol);   
    }

The above contract has one method that returns stock object for a given symbol. Here is our Stock class that has Symbol, Date, Company and Close properties.

[DataContract]
    public class Stock
    {
        [DataMember]
        public string Symbol { get; set; }
        [DataMember]
        public DateTime Date { get; set; }
        [DataMember]
        public string Company { get; set; }
        [DataMember]
        public decimal Close { get; set; }
    }

Next, i will delete the default service and create a Stock service that will implement the Istock contract as shown below.

 public class Stocks : IStock
    {
        #region IStock Members
        public Stock GetStock(string Symbol)
        {
            Stock st = null;
            switch (Symbol.ToUpper())
            { 
                case "GOOG":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, Company = "Google Inc.", Close = 495 };
                    break;
                case "MSFT":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, Company = "Microsoft Corporation", Close = 25 };
                    break;
                case "YHOO":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, Company = "Yahoo! Inc.", Close = 17 };
                    break;
                case "AMZN":
                    st = new Stock { Symbol = Symbol, Date = DateTime.Now, Company = "Amazon.com, Inc.", Close = 92 };
                    break; 
            }
            return st;
        }
        #endregion
    }

In above service, I implemented IStock contract that has a GetStock method which returns stock object for a given Symbol.

Now, I will have the following endpoints in my web.config.

<service behaviorConfiguration="WcfSample.Service1Behavior" name="WcfSample.Stocks">
<endpoint address="" binding="wsHttpBinding" contract="WcfSample.IStock">
<identity>
 <dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

In above configuration, we have address="" which is localhost, binding="wsHttpBinding" and contract="WcfSample.IStock"

Now i will compile the service and build a client to consume the service.

Creating a client to consume service

To create a client i will create a web application please follow these steps.

  1. Right Click on Solution �> Add �> new project then select ASP.net web application.
  2. It will create a web application template.
  3. Now, i will add the service reference. To add service reference, select client application, then add and service reference. Since our client is in a same solution, i will click discover and service in solution as shown below.

     wcf1

  4. In default.aspx, i will create a simple UI, a textbox to enter the stock symbol and a button to call the service to get stock information. Here is our code behind.
    		
    	
    		ServiceReference2.StockClient sc = new ServiceReference2.StockClient();	
    	
    		ServiceReference2.Stock st = sc.GetStock(TextBox1.Text.Trim());	
    	
    		StringBuilder sb = new StringBuilder();	
    	
    		sb.AppendFormat("<B>Company:</B> {0}<br />", st.Company);	
    	
    		sb.AppendFormat("<B>Date: </B>{0}<br />", st.Date);	
    	
    		sb.AppendFormat("<B>Close: </B>{0}<br />", st.Close);	
    	
    		sb.AppendFormat("<B>Symbol: </B>{0}<br />", st.Symbol); 	
    	
    		Label1.Text = sb.ToString(); 	
    	
  5. Here are few screenshots from our final application.
    wcf2
    wcf3

Summary

In this article, we examined how to create and consume a WCF service. As you can see, creating and consuming WCF service with Visual studio 2008 is a pretty simple. To download complete source code please visit to http://www.codeproject.com/KB/WCF/your_first_WCF_service.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | C#

Comments

Jobs Autos Real estate Videos Power by Google