Wednesday 12 October 2011

step 2 to 5

Step 2: Create the Contract by creating interface IMathService and add ServiceContract attribute to the interface and add OperationContract attribute to the method declaration.
IMathService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

 [ServiceContract]
public interface IMathService
{

    [OperationContract]
    int Add(int num1, int num2);

    [OperationContract]
    int Subtract(int num1, int num2);

}
Step 3: Implementation of the IMathService interface is shown below.
MathService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class MathService : IMathService
{
    public int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    public int Subtract(int num1, int num2)
    {
        return num1 - num2;
    }
}
Step 4: Service file is shown below.
MathService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="MathService" 
CodeBehind="~/App_Code/MathService.cs" %>
Step 5: In web.Config file, create end point with 'netTcpBinding' binding and service metadata will be published using Metadata Exchange point. So create the Metada Exchange end point with address as 'mex' and binding as 'mexTcpBinding'. Without publishing the service Metadata we cannot create the proxy using net.tcp address (e.g svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc )
Web.Config
<system.serviceModel>
<services>
 <service name="MathService" behaviorConfiguration="ServiceBehavior">
 <!-- Service Endpoints -->
 <endpoint binding="netTcpBinding" 
 contract="IMathService" >
 </endpoint>
  <endpoint address="mex" 
  binding="mexTcpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below 
to false and remove the metadata endpoint above before deployment -->
 <serviceMetadata httpGetEnabled="true"/>
 <!-- To receive exception details in
  faults for debugging purposes, set the value below to true. 
   Set to false before deployment to avoid disclosing 
   exception information -->
 <serviceDebug includeExceptionDetailInFaults="false"/>
 </behavior>
</serviceBehaviors></behaviors>
</system.serviceModel>

No comments:

Post a Comment