Wednesday, February 8, 2012

Creating your first WCF service- for beginners


Download

Open your Microsoft visual studio
Navigate to file
New project
Expand visual C#
Navigate to WCF
select WCF service Library


1. Delete all sample classes
2. Create interface named IEvalService.cs
3. Create contract named Eval.cs
4. Create service class named EvalService.cs





*****************************************************************************
/*IEvalService.cs*/

using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace EvalService
{
   [ServiceContract]
    public interface IEvalService
    {
        [OperationContract]
        void SubmitEval(Eval obj);
        [OperationContract]
        List<Eval> GetEvals();
        [OperationContract]
        void RemoveEval(String id);
    }
}

*****************************************************************************
/*Eval.cs*/

using System;
using System.Runtime.Serialization;

namespace EvalService
{
    [DataContract]
    public class Eval
    {
        [DataMember]
        public String Id { get; set; }
        [DataMember]
        public String Submitter { get; set; }
        [DataMember]
        public String Comments { get; set; }
        [DataMember]
        public DateTime TimeSubmitted { get; set; }
    }
}

*****************************************************************************
/*EvalService.cs*/

using System;
using System.Collections.Generic;

namespace EvalService
{

    public class EvalService:IEvalService
    {
        private List<Eval> list = new List<Eval>();
        #region IEvalService Members

        public void SubmitEval(Eval eval)
        {
            Eval evalObj = new Eval();
            evalObj.Id = Guid.NewGuid().ToString();
            evalObj.Submitter = eval.Submitter;
            evalObj.Comments = eval.Comments;
            evalObj.TimeSubmitted = DateTime.Now;
            list.Add(evalObj);
        }

        public List<Eval> GetEvals()
        {
            return list;
        }

        public void RemoveEval(string id)
        {
            list.Remove(list.Find(q => q.Id.Equals(id)));
        }

        #endregion
    }
}

*****************************************************************************
5. Configuration 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="EvalServiceBehavior1" name="EvalService.EvalService">
        <endpoint address="wshb" binding="wsHttpBinding" contract="EvalService.IEvalService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="EvalService.IEvalService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/EvalService/Service1/" />
          </baseAddresses>
          <!--
you can change this base address ex.
<add baseAddress="http://localhost:80/MyFirstService/" />
if it is not working, run your visual studio in administrator mode
-->
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EvalServiceBehavior1">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

*****************************************************************************




Post a Comment