Number of ways to develop WCF Services

Here we take an example by which we can understand some of these.

  1. In Configuration File: We can define our WCF service in web.config file, here we specify a lot of things regarding the attributes and behavior of the service. And if we want to change something in the web.config file, it is not necessary to recompile this again and again.

    <?xml version="1.0"?>
    <configuration>
    <system.serviceModel>
         <
services>
               <
service name="Service" behaviorConfiguration="ServiceBehavior">
           <endpoint address="" binding="wsHttpBinding" contract="IService">         
              <
identity>
                    <
dns value="localhost"/>
              </identity>
            </
endpoint>
              <
endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>     
        </
services>   
              <
behaviors>
                <
serviceBehaviors>
                  <
behavior name="ServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
              </behavior>
            </
serviceBehaviors>
           </
behaviors>
        </
system.serviceModel>
    </
configuration>

  1. The Second Way of programming is if we can do this by defining the attribute. These attribute is used to define the Contracts and it is also used to
    define the behavior of the Service.

    using System.ServiceModel;
    namespace Calc
    {
        [
    ServiceContract]
        
    public interface ICalcService
       {
           [
    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);
        }
    }
    namespace Calc
    {
        [
    ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
        
    public class CalcService:ICalcService
        {
            public double Add(double n1, double n2)
            {
                
    return n1 + n2;
            }
            public double Subtract(double n1, double n2)
            {
                
    return n1 - n2;
            }
            public double Multiply(double n1, double n2)
            {
                return n1 * n2;
            }
            public double Divide(double n1, double n2)
            {
                return n1 / n2;
            }
        }
    }


    3. The Third way of programming in which we will use the object model. This is more beneficial as we have an idea of object oriented programming.

    static void main()
    {
          Uri BA=new Uri(ConfigurationManager.AppSettings["add"]);
          AddressHeader AH = new AddressHeader.CreateAddressHeader("myservice1", "http://localhost:6363/service");
          EndpointAddress EA = new EndpointAddress(new Uri("http://localhost:6363/myfirstservice/service"));
          ServiceEndPoint SE=new ServiceEndPoint(ContractDescription.GetContract(typeof(StudentService)),new WSHttpBinding),EA);
          ServiceHost SH new ServiceHost(typeof(StudentService),BA);
          SH.Description.Endpoints.Add(SE);
          SH.Open();
          SH.Close();   
    }
Next Recommended Reading Create WCF Service In Visual Studio 2017