Answered by:
Why Interface is required?

Question
-
Any one please tell me why we need interface while creating WCF service?
Friday, July 2, 2010 1:44 PM
Answers
-
Hello,
interface is really required only when you want to provide contract to your client in the form of .NET code. .NET client can consume service in two ways. First is to create proxy from WSDL (metadata). If you choose this way you generally don't need to use interface (new one will be created on the client) and you can mark as a service contract directly your service implementation (but it is ugly). If you don't want to expose metadata and you want to consume the service by creating channel from ChannelFactory<T> you need the interface because you provide the interface type as generic argument.
But best practice is to always use interface. It also follows OOP best practices - code against abstraction not implementation.
Best regards,
Ladislav- Marked as answer by Mog Liang Monday, July 12, 2010 3:05 AM
Friday, July 2, 2010 2:55 PM -
Hi Ganesh,
an interface defines some component's exposed functionality. You can also think of them as contracts. WCF service interfaces play the same role - they just say, hey I'm a service and this is my functionality. Without interfaces, clients wouldn't know how to 'talk' to a service...
Hope this made things clearer.
Regards,
Stipe Ivan
- Proposed as answer by Frank Xu Lei Monday, July 5, 2010 6:53 AM
- Marked as answer by Mog Liang Monday, July 12, 2010 3:05 AM
Friday, July 2, 2010 1:54 PM
All replies
-
Hi Ganesh,
an interface defines some component's exposed functionality. You can also think of them as contracts. WCF service interfaces play the same role - they just say, hey I'm a service and this is my functionality. Without interfaces, clients wouldn't know how to 'talk' to a service...
Hope this made things clearer.
Regards,
Stipe Ivan
- Proposed as answer by Frank Xu Lei Monday, July 5, 2010 6:53 AM
- Marked as answer by Mog Liang Monday, July 12, 2010 3:05 AM
Friday, July 2, 2010 1:54 PM -
Hello,
interface is really required only when you want to provide contract to your client in the form of .NET code. .NET client can consume service in two ways. First is to create proxy from WSDL (metadata). If you choose this way you generally don't need to use interface (new one will be created on the client) and you can mark as a service contract directly your service implementation (but it is ugly). If you don't want to expose metadata and you want to consume the service by creating channel from ChannelFactory<T> you need the interface because you provide the interface type as generic argument.
But best practice is to always use interface. It also follows OOP best practices - code against abstraction not implementation.
Best regards,
Ladislav- Marked as answer by Mog Liang Monday, July 12, 2010 3:05 AM
Friday, July 2, 2010 2:55 PM