?
Solved

Remoting

Posted on 2006-04-05
8
Medium Priority
?
319 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:Eamon
  • 4
  • 3
8 Comments
 
LVL 1

Author Comment

by:Eamon
ID: 16383200
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16383265
Maybe stupid question, but do you run all the code in the same project?
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16383272
Why dont you just add a web reference and use that to handle the call.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Eamon
ID: 16383284
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
 
LVL 1

Author Comment

by:Eamon
ID: 16383295
no code in the form is run in different project to the code for the service
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16383308
You have to reference the remoting object in your client as well, that will reveal your type problem.
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 2000 total points
ID: 16383348
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
 
LVL 1

Author Comment

by:Eamon
ID: 16476241
Have not had a chance to fully test this, but it looks like it will get me going.
Thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question