Professional Documents
Culture Documents
.NET Framework 4 15 out of 21 rated this helpful Rate this topic This is the first of six tasks required to create a basic Windows Communication Foundation (WCF) service and a client that can call the service. For an overview of all six of the tasks, see the Getting Started Tutorial topic. When creating a basic WCF service, the first task is to define a contract. The contract specifies what operations the service supports. An operation can be thought of as a Web service method. Contracts are created by defining a C++, C#, or Visual Basic (VB) interface. Each method in the interface corresponds to a specific service operation. Each interface must have the ServiceContractAttribute applied to it and each operation must have the OperationContractAttribute applied to it. If a method within an interface that has the ServiceContractAttribute does not have the OperationContractAttribute, that method is not exposed. The code used for this task is provided in the example following the procedure.
C#
namespace Microsoft.ServiceModel.Samples { class Program { static void Main(string[] args) { } } }
6. Add a reference to System.ServiceModel.dll to the project: 1. In the Solution Explorer, right-click the References folder under the project folder and choose Add Reference. 2. Select the .NET tab in the Add Reference dialog and scroll down until you see System.ServiceModel (version 4.0.0.0), select it, and click OK. Note: When using a command-line compiler (for example, Csc.exe or Vbc.exe), you must also provide the path to the assemblies. By default, on a computer running Windows Vista for example, the path is: "Windows\Microsoft.NET\Framework\v4.0". 7. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace. C#
using System.ServiceModel;
8. In Program.cs (Module1.vb for VB), define a new interface called ICalculator and apply the ServiceContractAttribute attribute to the interface with a Namespace value of "http://Microsoft.ServiceModel.Samples". Specifying the namespace explicitly is a best practice because it prevents the default namespace value from being added to the contract name. Note: When using attributes to annotate an interface or class, you can drop the "Attribute" part from the attribute name. So ServiceContractAttribute becomes [ServiceContract] in C#, or <ServiceContract> in Visual Basic. 9. C#
10. [ServiceContract(Namespace = http://Microsoft.ServiceModel.Samples")] 11. public interface ICalculator
12.
13. Declare a method for each of the operations the ICalculator contract exposes (add, subtract, multiply, and divide) within the interface and apply the OperationContractAttribute attribute to each method that you want to expose as part of the public WCF contract. C# VB
[OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2);
Example
The following code example shows a basic interface that defines a service contract. C# VB
using System; // Step 5: Add the using statement for the System.ServiceModel namespace using System.ServiceModel; namespace Microsoft.ServiceModel.Samples { // Step 6: Define a service contract. [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] public interface ICalculator { // Step7: Create the method declaration for the contract. [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); } }
Now the interface is created, proceed to How to: Implement a Windows Communication Foundation Service Contract to implement the interface. For troubleshooting information, see Troubleshooting the Getting Started Tutorial.