locked
How to merge WCF services into a 3 tier Architecture web application RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Recently I have developed a web application which is based on 3 tier Architecture.

    There is a Data access layer, Business Logic Layer and User Interface.

    Now I want update this whole application by using WCF services in it without touching the Code I already written.

    Is it possible? If yes then how and if no then what I should do?

    Thanks

    Gaurav

    Tuesday, September 13, 2016 10:22 AM

Answers

  • User-484054684 posted

    WCF is generally used for distributed applications. Generally when the service is expected to use by more than one party Or when you want to make AJAX or asynchronous calls from your java script. This concept should be understood when we design the WCF services. 

    Coming to the coding - WCF services are just like normal class libraries. It is just that we use specific attributes to hint the framework that these are the classes used by WCF i.e. - the main service class which contains method will be decorated with [ServiceContract], your methods will be decorated with [OperationContract] and your request/response classes will be decorated with [DataContract] and the class properties will be with [DataMember] etc. 

    Another step is to define the configuration to host these services. This is where you will define the Endpoint (address, binding, contract, etc) in the web.config.

    Above is to create the WCF services. Once you create the WCF service and host it in IIS - you will have an url as defined in your configuration. 

    Consuming part is different.

    1. From C# code to consume it, You would need to add the 'Service Reference' (instead of Add reference as you do with other dlls). And that will create the proxy class (cs class) that will have all the methods, request and response classes you defined in your service. You can make use of the types generated in this cs file to do your coding. (i.e., to invoke the service methods, etc). 

    2. If you want to call it from javascript, it is different approach. You will use some libraries like jQuery and make a $.ajax request. 

    Hope this helps. I would suggest you to first practice a simple tutorial/exercise with WCF - Simply to create the WCF service and consume it (C#). That would really give a good idea about the concept.

    Some articles:

    http://www.codeproject.com/Articles/406096/A-beginners-tutorial-for-understanding-Windows

    http://www.codeproject.com/Articles/566691/WCF-From-a-Beginners-perspective-a-Tutorial

    You can find plenty by searching google with "wcf tutorial for beginners"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 9:19 AM

All replies

  • User-484054684 posted

    Are you planning to create WCF services or consume the WCF services available elsewhere?

    If you are creating WCF services:

    1. You can probably create one more tier for WCF services - that way, you are isolating the remaining layers from it - You may call it a Service Layer. 

    2. Other option is, to create a separate solution altogether - and expose these WCF services in another site, if it's completely different application.

    If you want to JUST consume WCF services which are available elsewhere:

    All you need is to add Service Reference (or equivalent dll for proxy) - and consume it in the required layer as per your design. Remember, you might also need to add the required configuration (endpoints, etc) in your web.config - though technically you can avoid all configurations and make it dynamic in your code.

    Tuesday, September 13, 2016 1:12 PM
  • User1052662409 posted

    1. You can probably create one more tier for WCF services - that way, you are isolating the remaining layers from it - You may call it a Service Layer. 

    I request you, could you please elaborate a bit just for some idea. Do I need to redefine all my data access layer's functions in my WCF layer?

    Tuesday, September 13, 2016 1:40 PM
  • User-484054684 posted

    Hi Gaurav, 

    If you want to create separate layer for WCF services - that means, it will be 3+1 tier/layer. 

    Now that you have UI (web app), BAL and DAL are Class Library projects with .cs files in them.

    WCF will be another project with it's .cs files (so basically 4th folder in your solution). If you want to call BAL methods from this layer's cs files - you would right click on WCF project and add reference to BAL csproj. Or if you want to directly access DAL method - you would right click and add DAL csproj reference.

    Ideally, you would not directly call DAL methods from WCF layer - Instead you may want to call just BAL methods from WCF layer.

    Please let me know if any questions. 

    I would advise to again think, if you are planning to CREATE the service or CONSUME the service written by someone else - as  both of these are different.

    Tuesday, September 13, 2016 1:49 PM
  • User-2103114970 posted

    ttp://www.codeproject.com/Articles/434282/A-N-Tier-Architecture-Sample-with-ASP-NET-MVC-WCF

    http://www.aspdotnet-suresh.com/2010/05/introduction-to-3-tier-architecture-in_17.html

    Tuesday, September 13, 2016 3:35 PM
  • User1052662409 posted

    Siva Krishna Macha

    If you want to create separate layer for WCF services - that means, it will be 3+1 tier/layer. 

    As I am new to WCF so please could you please let me know the idea. Lets suppose I have a function in my data access layer

    // data access layer
     #region Login For User
        public bool Get_Login(string User_Id, string Password)
        {
    
            bool boolReturnValue = false;
            SqlCommand com = new SqlCommand("Get_Login", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@User_Id", SqlDbType.NVarChar, 50).Value = User_Id;
            com.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = Password;
            SqlDataReader dr;
            con.Open();
            dr = com.ExecuteReader();
            while (dr.Read())
            {
                if ((User_Id == dr["User_Id"].ToString()) & (Password == dr["Password"].ToString()))
                {
                    boolReturnValue = true;
                }
                dr.Close();
                return boolReturnValue;
            }
            return boolReturnValue;
    
        }
        #endregion

    later I call it in my BLL (Business logic layer)

    // Business logic layer
    
     #region User login
        public bool Get_Login(string User_Id, string Password)
        {
            DataAccessLayer dal = new DataAccessLayer();
            try
            {
    
                return dal.Get_Login(User_Id, Password);
    
            }
            catch
            {
                throw;
            }
            finally
            {
                dal = null;
            }
    
        }
        #endregion

    then later I use it in my UI by making a new instance of BLL.

    for using WCF, what steps I need to take? If you could make me understand by using above function, I would be very thankful to you.

    I would advise to again think, if you are planning to CREATE the service or CONSUME the service written by someone else - as  both of these are different.

    I am creating my own services.

    Thanks

    Gaurav

    Wednesday, September 14, 2016 3:46 AM
  • User-484054684 posted

    WCF is generally used for distributed applications. Generally when the service is expected to use by more than one party Or when you want to make AJAX or asynchronous calls from your java script. This concept should be understood when we design the WCF services. 

    Coming to the coding - WCF services are just like normal class libraries. It is just that we use specific attributes to hint the framework that these are the classes used by WCF i.e. - the main service class which contains method will be decorated with [ServiceContract], your methods will be decorated with [OperationContract] and your request/response classes will be decorated with [DataContract] and the class properties will be with [DataMember] etc. 

    Another step is to define the configuration to host these services. This is where you will define the Endpoint (address, binding, contract, etc) in the web.config.

    Above is to create the WCF services. Once you create the WCF service and host it in IIS - you will have an url as defined in your configuration. 

    Consuming part is different.

    1. From C# code to consume it, You would need to add the 'Service Reference' (instead of Add reference as you do with other dlls). And that will create the proxy class (cs class) that will have all the methods, request and response classes you defined in your service. You can make use of the types generated in this cs file to do your coding. (i.e., to invoke the service methods, etc). 

    2. If you want to call it from javascript, it is different approach. You will use some libraries like jQuery and make a $.ajax request. 

    Hope this helps. I would suggest you to first practice a simple tutorial/exercise with WCF - Simply to create the WCF service and consume it (C#). That would really give a good idea about the concept.

    Some articles:

    http://www.codeproject.com/Articles/406096/A-beginners-tutorial-for-understanding-Windows

    http://www.codeproject.com/Articles/566691/WCF-From-a-Beginners-perspective-a-Tutorial

    You can find plenty by searching google with "wcf tutorial for beginners"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 9:19 AM