Imports Microsoft.VisualBasic
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data
Imports System.Collections.Generic
Imports System.Web
Imports System.Runtime.Remoting.Channels

Public Class Services
  Public Sub GetServerConfig()
    Dim lsAppServer As New List(Of String)
    Dim lsWebServer As New List(Of String)

    Dim appserverkey As String = ConfigurationManager.AppSettings(ApplicationKey.AppServers)
    Dim webserverkey As String = ConfigurationManager.AppSettings(ApplicationKey.WebServers)

    Dim ds As DataSet
    Dim DB As Database
    Dim CMD As Data.Common.DbCommand
    Try

      DB = DatabaseFactory.CreateDatabase("CharterDotCom_CMSConnectionString")
      CMD = DB.GetSqlStringCommand("SELECT ServerLocation,ServerName FROM ADMIN.SERVERCONFIG WHERE ServerLocation IN ('" & appserverkey & "','" & webserverkey & "')")
      ds = DB.ExecuteDataSet(CMD)
      If Not ds Is Nothing AndAlso ds.Tables.Count = 1 Then
        For Each dr As DataRow In ds.Tables(0).Rows
          Select Case dr.Item(0)
            Case appserverkey
              lsAppServer.Add(dr.Item(1))
            Case webserverkey
              lsWebServer.Add(dr.Item(1))
          End Select
        Next
        With HttpContext.Current
          .Application.Lock()
          .Application.Add(ApplicationKey.AppServers, lsAppServer)
          .Application.Add(ApplicationKey.WebServers, lsWebServer)
          .Application.UnLock()
        End With

      End If
    Catch ex As Exception
      Throw ex
    Finally
      lsAppServer = Nothing
      lsWebServer = Nothing
        CMD = Nothing
      If Not CMD Is Nothing Then
        CMD.Dispose()
      End If
    End Try
  End Sub


  Public Shared Sub RegisterChannels()
    Dim prop As IDictionary
    Dim objChannel As Http.HttpChannel
    Try
      'Reg channel only when one is not registered.
      If ChannelServices.RegisteredChannels.Length = 0 Then
        prop = New Hashtable()
        prop("name") = "Channel1"
        'prop("port") = "0"

        objChannel = New Http.HttpChannel(prop, New BinaryClientFormatterSinkProvider(), New BinaryServerFormatterSinkProvider())
        ChannelServices.RegisterChannel(objChannel, False)
      End If
    Catch ex As Exception
      Throw ex
    Finally
    prop = Nothing
      If Not prop Is Nothing Then
        prop = Nothing
      End If
      objChannel = Nothing
      If Not objChannel Is Nothing Then
        objChannel = Nothing
      End If
    End Try

  End Sub
End Class
