locked
WCF Service Pattern RRS feed

  • Question

  • User-431020342 posted

    Hello All,

    I am new to WCF and wanted to know why we need to create interface  in WCF?

    Can we access  methods  directly outside instead  of creating an Interface.

    This may be very simple question but as a begginer i  want to know this concept!!!!

    Any help on this would be appreciated.

    Thanks in advance

    Saturday, September 14, 2013 10:44 AM

Answers

  • User2019981500 posted

    WCF does NOT require you to have an interface and implement it.It's considered as best practice to do so - but you don't have to, if you don't want to use. you can put your [ServiceContract] on a concrete class that has a number of [OperationContract] service methods - nothing stops you for doing so.

    But let me tell you this is generally accepted as best practice to use an interface to separate out the actual contract as an interface .

    Use of Interface

    from stackoverflow

    Benefits of Using Interfaces

    October 21, 2011 2 Comments

    You might wonder why you’d define an interface, have a class implement that interface and then access the class through the interface instead of just using the class directly.

    One benefit of interfaces is that it allows you to treat different types of objects in the same way, provided that they implement a common interface.

    For example, assume that we have Dog, Seal and DrillSergeant classes, all of which implement the IBark interface (which contains a Bark method).  We can now store a collection of instances of these classes and ask all objects in our collection to Bark by using the IBark interface.

                Dog kirby = new Dog("Kirby", 12);
                Seal sparky = new Seal("Sparky");
                DrillSergeant sarge = new DrillSergeant("Sgt. Hartman", "Tough as nails");
    
                List<IBark> critters = new List<IBark>() { kirby, sparky, sarge };
    
                // Tell everyone to bark
                foreach (IBark barkingCritter in critters)
                {
                    barkingCritter.Bark();
                }
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 14, 2013 2:08 PM