Friday, 28 June 2019

MVC - WCF - Create and consume WCF Service with data from EF

Watch this example on YouTube


1. SQL 

USE [Company]
GO
/****** Object:  Table [dbo].[Product]    Script Date: 2019-06-28 9:34:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
    [ProductID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[Product] ON

GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (1, N'Table')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (2, N'Something Else')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (3, N'Sofa')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (4, N'Chair')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (5, N'Laptop')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (6, N'Mouse')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (7, N'Keyboard')
GO
INSERT [dbo].[Product] ([ProductID], [Name]) VALUES (8, N'Monitor')
GO
SET IDENTITY_INSERT [dbo].[Product] OFF
GO

2. IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        List<Product> GetAllProducts();
        [OperationContract]
        string GetSomething();
    }


    // 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; }
        }
    }
}
3. Service1.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetSomething()
        {
            return "something";
        }
        public List<Product> GetAllProducts()
        {
            List<Product> prodList = new List<Product>();
            CompanyEntities db = new CompanyEntities();
            var res = from p in db.Products select p;
            foreach(var item in res)
            {
                Product pr = new Product();
                pr.Name = item.Name;
                pr.ProductID = item.ProductID;
                prodList.Add(pr);

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

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
4. Consumer (home controller)

        public ActionResult Index()
        {
            List<Product> lstProd = new List<Product>();
            ServiceReference1.Service1Client s = new ServiceReference1.Service1Client();
            var res2 = s.GetSomething();
            var res = s.GetAllProducts();
            foreach(var item in res)
            {
                Product pr = new Product();
                pr.ProductID = item.ProductID;
                pr.Name = item.Name;
                lstProd.Add(pr);
            }
            return View(lstProd);
        }

No comments:

Post a Comment