WCF Using service application template

add new project – template WCF Service Application
 
1) Creating the service interfaces & data contract:
 
IProductService.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyWCFServices.RealNorthwindService
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract]
    public interface IProductService
    {
        [OperationContract]
        [FaultContract(typeof(ProductFault))]
        Product GetProduct(int id);
        [OperationContract]
        bool UpdateProduct(Product product);
        // TODO: Add your service operations here
    }
    [DataContract]
    public class Product
    {
        [DataMember]
        public int ProductID { get; set; }
        [DataMember]
        public string ProductName { get; set; }
        [DataMember]
        public string QuantityPerUnit { get; set; }
        [DataMember]
        public decimal UnitPrice { get; set; }
        [DataMember]
        public bool Discontinued { get; set; }
    }
    [DataContract]
    public class ProductFault
    {
        public ProductFault(string msg)
        {
            FaultMessage = msg;
        }
        [DataMember]
        public string FaultMessage;
    }
}
 
2) implementing data contract
 
 
 
ProductService.cs:
 
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MyWCFServices.RealNorthwindEntities;
using MyWCFServices.RealNorthwindLogic;
namespace MyWCFServices.RealNorthwindService
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
    public class ProductService : IProductService
    {
        ProductLogic productLogic = new ProductLogic();
        public Product GetProduct(int id)
        {
            /*
            // TODO: call business logic layer to retrieve product
            Product product = new Product();
            product.ProductID = id;
            product.ProductName = "fake product name from service layer";
            product.UnitPrice = (decimal)10.0;
            */
            ProductEntity productEntity = productLogic.GetProduct(id);
            if (productEntity == null)
            {
                //throw new Exception("No product found with id " + id);
                if (id != 999)
                    throw new FaultException<ProductFault>(new ProductFault("No product found with id " + id), "Product Fault");
                else
                    throw new Exception("Test Exception");
            }
            Product product = new Product();
            TranslateProductEntityToProductContractData(productEntity, product);
            return product;
        }
        public bool UpdateProduct(Product product)
        {
            /*
            // TODO: call business logic layer to update product
            if (product.UnitPrice <= 0)
                return false;
            else
                return true;
            */
            ProductEntity productEntity = new ProductEntity();
            TranslateProductContractDataToProductEntity(product, productEntity);
            return productLogic.UpdateProduct(productEntity);
        }
        private void TranslateProductEntityToProductContractData(
            ProductEntity productEntity,
            Product product)
        {
            product.ProductID = productEntity.ProductID;
            product.ProductName = productEntity.ProductName;
            product.QuantityPerUnit = productEntity.QuantityPerUnit;
            product.UnitPrice = productEntity.UnitPrice;
            product.Discontinued = productEntity.Discontinued;
        }
        private void TranslateProductContractDataToProductEntity(
            Product product,
            ProductEntity productEntity)
        {
            productEntity.ProductID = product.ProductID;
            productEntity.ProductName = product.ProductName;
            productEntity.QuantityPerUnit = product.QuantityPerUnit;
            productEntity.UnitPrice = product.UnitPrice;
            productEntity.Discontinued = product.Discontinued;
        }
    }
}
 
3) Modifying app.config file
 
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="NorthwindConnectionString" value="server=seliu06877\seliu06877_2005;uid=sa;pwd=;database=Northwind" />
    ….
  </appSettings>
  <system.web>
    <compilation debug="true" />
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      ….
  </system.web>
  <!– When deploying the service library project, the content of the config file must be added to the host’s
  app.config file. System.Configuration does not support config files for libraries. –>
  <system.serviceModel>
    <services>
      <service name="MyWCFServices.NorthwindService.ProductService" behaviorConfiguration="MyWCFServices.NorthwindService.ProductServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/Design_Time_Addresses/MyWCFServices/NorthwindService/ProductService/" />
          </baseAddresses>
        </host>
          </system.serviceModel>
….
 
 
the three Layer architecture:
 
  • ProductDAL.cs (retrieve Data)
  • ProductEntity.cs (record)
  • ProductLogic.cs (transform Data)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add reference – tab projects
using MyWCFServices.RealNorthwindEntities;
using MyWCFServices.RealNorthwindDAL;
namespace MyWCFServices.RealNorthwindLogic
{
    public class ProductLogic
    {
        ProductDAL productDAL = new ProductDAL();
        public ProductEntity GetProduct(int id)
        {
            /*
            // TODO: call data access layer to retrieve product
            ProductEntity p = new ProductEntity();
            p.ProductID = id;
            p.ProductName = "fake product name from business logic layer";
            //p.UnitPrice = (decimal)20.0;
            if(id > 50) p.UnitsOnOrder = 30;
            return p;
            */
            return productDAL.GetProduct(id);
        }
        public bool UpdateProduct(ProductEntity product)
        {
            // TODO: call data access layer to update product
            // first check to see if it is a valid price
            if (product.UnitPrice <= 0)
                return false;
            // ProductName can’t be empty
            else if (product.ProductName == null || product.ProductName.Length == 0)
                return false;
            // QuantityPerUnit can’t be empty
            else if (product.QuantityPerUnit == null || product.QuantityPerUnit.Length == 0)
                return false;
            // then validate other properties
            else
            {
                ProductEntity productInDB = GetProduct(product.ProductID);
                // invalid product to update
                if (productInDB == null)
                    return false;
                // a product can’t be discontinued if there are non-fulfilled orders
                else if (product.Discontinued == true && productInDB.UnitsOnOrder > 0)
                    return false;
                else
                    return productDAL.UpdateProduct(product);
            }
        }
    }
}
 
 
 
….
Publicités
Cet article a été publié dans DotNet - WCF. Ajoutez ce permalien à vos favoris.

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s