banner



How To Create Wcf Service In Visual Studio 2019

  • Download sample - 40.36 KB

Introduction

In this article, I will examine how to create and consume a WCF service. WCF is a 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 a WCF service. To create a WCF service, please follow these steps:

  1. Launch 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 an IStock contract as shown below.

Using the Code

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

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

          [          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;        }          

0 Response to "How To Create Wcf Service In Visual Studio 2019"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel