Link to home
Start Free TrialLog in
Avatar of Eamon
EamonFlag for Ireland

asked on

Remoting

I have being trying to get this to work for ages but can't

I want to use remoting to return a Dataset from a windows service.
A windows form with a button on it should query the service and get a dataset back. It is just not working and it is because i just dont fully understand remoting.
Avatar of Eamon
Eamon
Flag of Ireland image

ASKER

This is the code from the windows service


Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class CustService
    Dim x As Integer


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        EventLog1.WriteEntry("Cust Start")
        Timer1.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub




    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        If Not System.Diagnostics.EventLog.SourceExists("CustSource") Then
            System.Diagnostics.EventLog.CreateEventSource("CustSource", "TestLog")
        End If

        EventLog1.Source = "CustSource"
        EventLog1.Log = "TestLog"




        Try
            Dim channel As New TcpChannel(9999)
            ChannelServices.RegisterChannel(channel, False)   ' eamon check this again to make sure it is correct.
            RemotingConfiguration.ApplicationName = "CustInfoName"
            'Server is the name of the root namespace. It must be the same for client and server. It is in
            'Progect properties - general          
            Dim t As Type = Type.GetType("remoCustService.CustServer")
            RemotingConfiguration.RegisterWellKnownServiceType(t, "CustServer", WellKnownObjectMode.SingleCall)

            Test = t.ToString

        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText("c:\Testing.txt", Now() & " - " & ex.ToString & vbCrLf, True)
        End Try


    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        'Just keeping the service alive
        x = 1
        'x += 1
        ' EventLog1.WriteEntry("Tick " & x)
        EventLog1.WriteEntry("Test = " & Test)
    End Sub
End Class


Public Class CustServer
    Inherits MarshalByRefObject


    Private strCnn As String = "SourceDB=\\sales_pentium\Ddrv\HEAD\DATA;PWD=;SourceType=DBF;Collate=Machine;BackgroundFetch=Yes;Exclusive=No;Driver={Microsoft dBase VFP Driver (*.dbf)};UID="
    Private strCust As String
    Private strSDate As String
    Private strEDate As String

    Private dsHead As DataSet


    Public Sub New()
        'RemotingConfiguration.ApplicationName = "getCustDocs"
        'RemotingConfiguration.RegisterWellKnownServiceType(GetType(clsCustInfo), "MyUri", WellKnownObjectMode.Singleton)


        Test = "New Ran"


    End Sub


    Public Function getCustDocs(ByVal strCustomer As String, ByVal strStartDate As String, ByVal strEndDate As String) As DataSet
        strCust = strCustomer
        strSDate = strStartDate
        strEDate = strEndDate

        GetCustDockets()

        Return dsHead

        dsHead = Nothing
    End Function

    Private Sub GetCustDockets()

        Dim Cnn As Odbc.OdbcConnection = New Odbc.OdbcConnection

        Try
            Cnn.ConnectionString = strCnn
            Cnn.Open()
        Catch ex As Exception
            'MessageBox.Show(ex.Message.ToString)
        End Try

        Dim strSQL As String = "SELECT branch, ledgr_date, type, docket, ref1, customer, value_in FROM TrHead WHERE (customer = ?) AND (ledgr_date >= ?) AND (ledgr_date <= ?)"  'Dim strSQL As String = "SELECT branch, `date`, ledgr_date, type, docket, ref1, customer, value_in FROM TrHead WHERE (customer = ?) AND (ledgr_date >= ?) AND (ledgr_date <= ?)"
        dsHead = New DataSet

        Try
            Dim daHead As Odbc.OdbcDataAdapter = New Odbc.OdbcDataAdapter(strSQL, Cnn)

            daHead.SelectCommand.Parameters.Add(New Odbc.OdbcParameter("@Cust", Odbc.OdbcType.NVarChar, 6))
            daHead.SelectCommand.Parameters.Add(New Odbc.OdbcParameter("@SDate", Odbc.OdbcType.Date))
            daHead.SelectCommand.Parameters.Add(New Odbc.OdbcParameter("@EDate", Odbc.OdbcType.Date))

            daHead.SelectCommand.Parameters.Item("@Cust").Value = strCust
            daHead.SelectCommand.Parameters.Item("@SDate").Value = strSDate
            daHead.SelectCommand.Parameters.Item("@EDate").Value = strEDate

            daHead.Fill(dsHead, "TrHead")
            daHead = Nothing
        Catch ex As Exception
            'MessageBox.Show(ex.ToString)
        End Try

        Cnn.Dispose()

    End Sub

End Class
Maybe stupid question, but do you run all the code in the same project?
Avatar of Mikal613
Why dont you just add a web reference and use that to handle the call.
Avatar of Eamon

ASKER

This is the code in my windows form


Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class Form1
    Dim HO As CustServer

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Initialize() Then

            Dim strCustomer As String = txtCustomer.Text
            Dim strStartDate As String = txtStartDate.Text
            Dim strEndDate As String = txtEndDate.Text

            Try

                DataGridView1.DataSource = HO.getCustDocs(strCustomer, strStartDate, strEndDate)

            Catch ex As Exception
                MessageBox.Show(ex.ToString)

            End Try

            HO = Nothing
            DataGridView1.Refresh()
           
        End If  'If Initialize() Then
    End Sub

    Public Function Initialize() As Boolean
        Try
            If HO Is Nothing Then
                Dim chan As New TcpChannel()
                ChannelServices.RegisterChannel(chan, False) '   not sure about ensure security

                Dim t As Type = System.Type.GetType("Form1.CustServer")    ' THIS IS REALLY WHERE I HAVE A PROBLEM
                ' HAVE NO IDEA WHAT TO PUT IN AS TYPE. HAVE TRIED LOTS OF THINHS BUT T IS ALWAYS SET TO NOTHING.

                HO = Activator.GetObject(t, "tcp://192.169.1.88:9999/CustServer")   ' this is my ip adress and the service is running on my machine

                If HO Is Nothing Then
                    MsgBox("Could not initialize the remoting client. Check your configuration.")
                    Return False
                End If
            End If

            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Error In Initialize")
            Return False
        End Try



    End Function

Public Class CustServer
        Inherits MarshalByRefObject

        Public Function getCustDocs(ByVal strCustomer As String, ByVal strStartDate As String, ByVal strEndDate As String) As DataSet
        End Function
    End Class

End Class
Avatar of Eamon

ASKER

no code in the form is run in different project to the code for the service
You have to reference the remoting object in your client as well, that will reveal your type problem.
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eamon

ASKER

Have not had a chance to fully test this, but it looks like it will get me going.
Thanks.