Monday, 23 June 2014

Create a WCF Service in C#

First Take

IService.cs
...............................................................................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISimple" in both code and config file together.
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Getname(string name);
    }
}
.............................................................................................................

Service.cs
----------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Simple" in both code and config file together.
    public class Service: IService
    {

        public string Getname(string name)
        {
            return "My Name Is " + name;
        }
    }
}

------------------------------------------------------------------------------
Take new Project Console appllication for HOST a WCF SERVICE
----------------------------------------------------------------------------------
IN program.cs
---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Simplehost
{
    class Program
    {
        static void Main()
        {
            using (ServiceHost host = new ServiceHost(typeof(WCFService.Simple)))
            {
                host.Open();
                Console.WriteLine(DateTime.Now.ToString());
                Console.ReadLine(); 
            }
        }
    }
}

-----------------------------------------------------------------------------------------------
IN web.config
---------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
   
    <services>
      <service name="WCFService.Simple">
        <endpoint address="" binding="basicHttpBinding" contract="WCFService.ISimple">
          
        </endpoint>
        <endpoint address="" binding="netTcpBinding" contract="WCFService.ISimple">

        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/WCFService"/>
            <add baseAddress="net.tcp://localhost/WCFService1/"/>
          </baseAddresses>
        </host>
       
        
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  
  
  
</configuration>
-------------------------------------------------------------------------------------
run this webservice.
--------------------------------------------------------------------------------------

AFTER THAT TAKE A NEW WEB APPLICATION FOR USER WCF SERVICE
--------------------------------------------------------------------------------------

Firstly add web reference of WCF SERVICE
and 
Add Reference of  System.ServiceModel;
and 

add
using System.ServiceModel;
---------------------------------------------------------------------------------------
CALL 
ON BUTTON CLICK
-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;

namespace use_wcf
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            localhost.Simple client = new localhost.Simple("BINDING NAME");
          //web config binding name if more than one endpoint available
            Label1.Text= client.Getname(TextBox1.Text);
        }
    }
}



No comments:

Post a Comment