.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Operation Overloading in Services of WCF

Posted By:Dhananjay Kumar       Posted Date: March 27, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

Operation Overloading in Services of WCF. This article will explain, What is Service Contract? How to achieve Operation overloading in Service. How to achieve operation overloading at client side.
 

Objective:

This article will explain,
  1. What is Service Contract?
  2. How to achieve Operation overloading in Service.
  3. How to achieve operation overloading at client side.
  4. It explained the entire concept with supporting code as well.
  5. This will explain in detail of all the aspect of method overloading and manually configuration at client side.

    What is Service Contract?

    image1.gif

    ServiceContractAttribute class is defined as
    using System;
    using System.Net.Security;
    
    namespace System.ServiceModel
    {
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
        public sealed class ServiceContractAttribute : Attribute
        {
            
            public ServiceContractAttribute();
            public Type CallbackContract { get; set; }
            public string ConfigurationName { get; set; }
            public bool HasProtectionLevel { get; }
            public string Name { get; set; }
            public string Namespace { get; set; }
            public ProtectionLevel ProtectionLevel { get; set; }
            public SessionMode SessionMode { get; set; }
        }
    }

     
    1. It is defined under namespace System.ServiceModel.
    2. It got 6 properties.
    3. It could be applied either on service contract interface or service contract class.
       
    Could we achieve Operation overloading in Service Contract?

    image3.gif

    How to achieve operation overloading?

    By using Name property of OperationContractAttribute class.

    using System;
    using System.Net.Security;
    
    namespace System.ServiceModel
    {
        
    
        [AttributeUsage(AttributeTargets.Method)]
        public sealed class OperationContractAttribute : Attribute
        {
            
            public OperationContractAttribute();
            
            
            public string Action { get; set; }
            
            public bool AsyncPattern { get; set; }
            public bool HasProtectionLevel { get; }
            public bool IsInitiating { get; set; }
            public bool IsOneWay { get; set; }
            public bool IsTerminating { get; set; }
            public string Name { get; set; }
            public ProtectionLevel ProtectionLevel { get; set; }
            public string ReplyAction { get; set; }
        }
    }


    image5.gif

    OperationContractAttribute class

    Code Sample
     

    1. Service is having 3 overloaded operations.
    2. Add operation is taking different parameters.
    3. Name properties are being used to achieve overload operation.
    4. We will give aliased name to achieve overload using Name properties of OperationContractAttribute class.

              [OperationContract(Name="AddString")]
              string Add(string param1, string param2);
      

       
    Service Contract (IAddCalculator.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace MethodOverLoadingSample
    {
        
        [ServiceContract]
        public interface IAddCalculator
        {
            [OperationContract(Name="AddString")]
            string Add(string param1, string param2);
            [OperationContract(Name="AddInt")]
            int Add(int param1, int param2);
            [OperationContract(Name="AddDouble")]
            double Add(double param1, double param2);       
        }
    }


    Service Implementation (AddCalculator.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace MethodOverLoadingSample
    {
      
        public class Calculator : IAddCalculator
        {
            public string Add(string param1, string param2)
            {
                return param1 + param2;
            }
    
            public int Add(int param1, int param2)
            {
                return  param1+param2 ;
            }
    
            public double Add(double param1, double param2)
            {
                return param2 + param1; 
            }
        }
    }


    When the client imports the contract and generates the proxy, the imported operations will have the aliased name.

    At client side, the below methods will be imported.

    image9.gif

    The client can use the generated proxy at it is. Sample code is given below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using TestingService.ServiceReference1;
    
    namespace TestingService
    {
        class Program
        {
            static void Main(string[] args)
            {
                AddCalculatorClient proxy = new AddCalculatorClient();
                Console.WriteLine(proxy.AddInt(7, 2).ToString());
                Console.WriteLine(proxy.AddDouble(7, 2).ToString());
                Console.WriteLine(proxy.AddString("7", "2"));
                Console.ReadKey(true);
    
            }
        }
    }


    Output

    image11.gif

    Few problems in above approach

    So, up to here, we have achieved Operation Overloading. But there is one problem here that client side is importing alias name rather than the same name for all overloaded operation. By below code , we could see that client proxy is importing alias name.


    image12.gif
    image12.1.gif

    Solution of above problem
     

    1. Click on Object browser at client side. Click on ServiceReference1.

      image13.gif
       

    2. Click on AddCalculatorClient at object browser. Double click to open the code browser.

      image14.gif
       
    3. In AddCalculatorClient partial class, modify the imported operation contracts.

              public string AddString(string param1, string param2) 
              {
                  return base.Channel.AddString(param1, param2);
              }
              
              public int AddInt(int param1, int param2)
              {
                  return base.Channel.AddInt(param1, param2);
              }
              
              public double AddDouble(double param1, double param2)
              {
                  return base.Channel.AddDouble(param1, param2);
              }
      

    Just modify the code as,

    public string Add(string param1, string param2) {
                return base.Channel.Add(param1, param2);
            }
            
            public int Add(int param1, int param2) {
                return base.Channel.Add(param1, param2);
            }
            
            public double Add(double param1, double param2) {
                return base.Channel.Add(param1, param2);
            }

    And modify the interface at client side as

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
        [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IAddCalculator")]
        public interface IAddCalculator {
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAddCalculator/AddString", ReplyAction="http://tempuri.org/IAddCalculator/AddStringResponse",Name="AddString")]
            string Add(string param1, string param2);
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAddCalculator/AddInt", ReplyAction="http://tempuri.org/IAddCalculator/AddIntResponse",Name="AddInt")]
            int Add(int param1, int param2);
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAddCalculator/AddDouble", ReplyAction="http://tempuri.org/IAddCalculator/AddDoubleResponse",Name="AddDouble")]
            double Add(double param1, double param2);
        }


    Save and compile the code.

    Now at the client side, we could see that Add method is overloaded with 3 different parameters.


    image18.gif

    So, now we could modify the client code as,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using TestingService.ServiceReference1;
    
    namespace TestingService
    {
        class Program
        {
            static void Main(string[] args)
            {
                AddCalculatorClient proxy = new AddCalculatorClient();
                Console.WriteLine(proxy.Add(7, 2).ToString());
                Console.WriteLine(proxy.Add(7, 2).ToString());
                Console.WriteLine(proxy.Add("7", "2"));
                Console.ReadKey(true);
            }
        }
    }

    Output

    image20.gif

    Conclusion:

    This article explained,
     
  6. What is Service Contract?
  7. How to achieve Operation overloading in Service.
  8. How to achieve operation overloading at client side.
  9. It explained the entire concept with supporting code as well.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend