What is the practical usage of sharing service contract assemblies in WCF?

Answer by Sharmilli Priyadarsini:

Straight forward answer is, If you do not mark a class/interface as a service contract, it cannot be consumed as the service. The function of service contract is to serialize the class, in order to be consumed from other applications written in various platforms.
A practical example:
Just follow the below steps,
1) File >> New >> Project
2) Select WCF project as shown in the image below.

3) If you open the IService.cs file, you will see the service contract attribute added to your interface.
4) Now set your svc.cs as start page by right clicking and setting it as start up page. If you run your project, WCF test client will open, where you will be able to see the service contract under which the methods written inside it and test the service methods by passing the desired parameters.

Let us come to the actual problem, what happens if we do not have the service contract attribute over the interface?

5) Go to your IService.cs and remove the ServiceContract attribute.

6) While running the project it, the WCF test client throws a error saying that “Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata”.

Contracts are meant for serialization purpose. If you want to expose a class/interface, you need to attribute it as service contract.

Hope it solves your query, Happy Learning!

What is the practical usage of sharing service contract assemblies in WCF?