b

Wednesday 27 February 2013

Self Hosting WCF Service VB.NET


Self Hosting WCF Service in Console Applictaion Vb.NET


1) Create Contract and Implementation
     Here I use ADO.NET connecting to Northwind Database to get Number of Orders.

2) In Program Entry.
              Create Service Host  
3) Using ChannelFactory query above WCF Service

ServiceContract

    <ServiceContract> _
    Public Interface IOrder
        <OperationContract> _
        Dim GetOrderCount() As Integer
    End Interface
Implementation Class for Service Contract
Service Contract has single method called GetOrderCount from NorthWind Database.
 
    Public Class Orders
     Implements IOrder
        Public Function GetOrderCount() As Integer
            SqlConnection conn = New
                SqlConnection("Data Source=localhost\sqlexpress2012;Initial Catalog=northwind;trusted_connection=yes;")
            conn.Open()

            Dim cmd As SqlCommand =  New SqlCommand("select count(*) from orders",conn)
            Dim obj As Object = cmd.ExecuteScalar()

           Return Int32.Parse(obj.ToString())
        End Function
    End Class
 
In Program Entry Point

                1) Create/Host a WCF Service  assign some port number(unused) here

                 2) Consume same service using ChannelFactory

                1) Create/Host a WCF Service  assign some port number(unused) here
                'Create or Host WCF Service Here
                Dim baseAdd() As Uri =  New Uri() {New Uri("http://localhost:7070/")}

                Dim host As ServiceHost =  New ServiceHost(Type.GetType(Orders),baseAdd)
                host.AddServiceEndpoint(Type.GetType(IOrder), New WSHttpBinding(),
                "http://localhost:7070/selfhost")

                Dim mb As ServiceMetadataBehavior =  New ServiceMetadataBehavior()
                mb.HttpGetEnabled = True
                host.Description.Behaviors.Add(mb)

                'Open the Service
                host.Open()

 
                 2) Consume same service using ChannelFactory
               //Consume the above WCF Service using ChannelFactory
                 'Consume the above WCF Service using ChannelFactory
                '
                Dim binding As WSHttpBinding =  New WSHttpBinding()
                Dim address As EndpointAddress =  New EndpointAddress(
                "http://localhost:7070/selfhost")

                Dim channel As selfhostwcf.IOrder =  New ChannelFactory
                <selfhostwcf.IOrder>(binding,address).CreateChannel()
                Console.WriteLine("Order Count={0}",channel.GetOrderCount())

                Console.ReadLine()
That's it ,Hosting and consuming WCF service in console application without web.config settings.


Type :  
http://localhost:7070/  in your browser u will see metadata.

as long as console application is running you can access service metdata and other applications also can consume this service.
After Excuting Console Application U will get Output as
Order Count=830


Complete Source Code
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports System.ServiceModel.Description
Imports System.Data
Imports System.Data.SqlClient
Imports System.ServiceModel.Channels
Namespace selfhostwcf

    <ServiceContract> _
    Public Interface IOrder
        <OperationContract> _
        Dim GetOrderCount() As Integer
    End Interface

    Public Class Orders
     Implements IOrder
        Public Function GetOrderCount() As Integer
            SqlConnection conn = New
                SqlConnection("Data Source=localhost\sqlexpress2012;Initial Catalog=northwind;trusted_connection=yes;")
            conn.Open()

            Dim cmd As SqlCommand =  New SqlCommand("select count(*) from orders",conn)
            Dim obj As Object = cmd.ExecuteScalar()

           Return Int32.Parse(obj.ToString())
        End Function
    End Class

    Class Program
        Shared  Sub Main(ByVal args() As String)
            Try
                'Create or Host WCF Service Here
                Dim baseAdd() As Uri =  New Uri() {New Uri("http://localhost:7070/")}

                Dim host As ServiceHost =  New ServiceHost(Type.GetType(Orders),baseAdd)
                host.AddServiceEndpoint(Type.GetType(IOrder), New WSHttpBinding(),
                "http://localhost:7070/selfhost")

                Dim mb As ServiceMetadataBehavior =  New ServiceMetadataBehavior()
                mb.HttpGetEnabled = True
                host.Description.Behaviors.Add(mb)

                Dim 'Open the Service As host.Open()


                'Consume the above WCF Service using ChannelFactory
                '
                Dim binding As WSHttpBinding =  New WSHttpBinding()
                Dim address As EndpointAddress =  New EndpointAddress("http://localhost:7070/selfhost")

                Dim channel As selfhostwcf.IOrder =  New ChannelFactory<selfhostwcf.IOrder>(binding,address).CreateChannel()
                Console.WriteLine("Order Count={0}",channel.GetOrderCount())

                Console.ReadLine()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

        End Sub
    End Class
End Namespace
 
Tags:Self Hosting WCF Service VB.NET,ado.net in wcf service,
Hosting and consuming WCF service in console application without web.config,WCF Service contract,WCF Operation contract,Host service in console applictaion,Consume WCF service using ChannelFactory,Consume WCF service using ChannelFactory in console application