Search code examples
asp.netvb.netmultithreadingweb-serviceswcf

How I do a WCF service with two methods: one that waits and insert data into the DB and another to poll the DB?


This is the scenario:

I'm doing a web service in WCF and a client application in ASP.NET. The language is VB.NET.

  • I want a method than send "jobs" to the server. I'm trying to simulate this kind of jobs as long running processes that wait up to 20-30 seconds with thread sleeping and inserting a row into a JOBS table in the DB.
  • I want another method that polls the database every 5 seconds with client postbacks to get the jobs lists from the JOBS table (finished and currently running)

This is what i've got so far, both methods are working, except when a long running job is sleeping, then the client can't retrieve the list of jobs with another call. I've tried "PerCall" in the but it didn't work out. I suspect the thread is somewhat locking the service or maybe I have to use async calls. I'm using HttpBasicBinding at web.config ... I'm a little lost and I have not found any code doing something similar.

Here is the code in the service side:

iServiceJobs.vb

   <ServiceContract()>
    Public Interface IServiceJobs

    <OperationContract(IsOneWay:=True)>
    Sub SendJob(ByVal runTime As Integer, ByVal id As String)

    <OperationContract>
    Function GetJobsList() As List(Of ClassJob)

ServiceJobs.vb

Public Class ServiceJobs Implements IServiceJobs

    Public SendJob(ByVal runTime As Integer, ByVal id As String) Implements IServiceJobs.SendJob
        Dim connStr As String = "..."
        Dim conn As New OracleConnection(connStr)
        conn.Open()
        Dim query As String = "INSERT INTO JOBS(...)"
        Dim cmd As New OracleCommand(query, conn)
        cmd.ExecuteNonQuery()
        Threading.Thread.Sleep(runTime* 1000)
        cmd = New OracleCommand(query, conn)
        cmd.ExecuteNonQuery()
    End Sub

    Public Function GetJobList() As List(Of ClassJob) Implements IServiceJobs.GetJobsList
        Try
            Dim jobList As New List(Of ClassJobs)
            Dim connStr As String = "..."
            Dim conn As New OracleConnection(connStr)
            conn.Open()
            Dim query As String = "SELECT * FROM JOBS"
            Dim cmd As New OracleCommand(query, conn)
            Dim dr As OracleDataReader
            Dim job As ClassJob
            dr = cmd.ExecuteReader
            While dr.Read
                job = New ClassJob
                job.id = dr(0)
                job.lock = dr(3)
                ...
                jobList.Add(job)
                job = Nothing
            End While
            Return jobList
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class

The code in the client is very simple, just two buttons, one with a ws call for insert jobs and another to get the job list.

I'm open to any suggestion on how to do a better implementation of this scenario.

EDIT: I've tried

<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple, InstanceContextMode:=InstanceContextMode.PerCall)>

But it doesn't work, GetJobList() returns Nothing while SendJob() is working on this thread.

SOLVED: Apparently, I was using an outdated version of Oracle libraries. When I used Oracle.ManagedDataAccess from NuGet everything went OK.


Solution

  • SOLVED: Apparently, I was using an outdated version of Oracle libraries. When I used Oracle.ManagedDataAccess from NuGet everything went OK.