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.
LVL 1
EamonAsked:
Who is Participating?
 
PockyMasterCommented:
this guy did some work to explain it in simple words:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1284&lngWId=10
0
 
EamonAuthor Commented:
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
0
 
PockyMasterCommented:
Maybe stupid question, but do you run all the code in the same project?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mikal613Commented:
Why dont you just add a web reference and use that to handle the call.
0
 
EamonAuthor Commented:
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
0
 
EamonAuthor Commented:
no code in the form is run in different project to the code for the service
0
 
PockyMasterCommented:
You have to reference the remoting object in your client as well, that will reveal your type problem.
0
 
EamonAuthor Commented:
Have not had a chance to fully test this, but it looks like it will get me going.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.