开发者

running a thread from within a vb.net webservice

开发者 https://www.devze.com 2023-01-20 16:38 出处:网络
Hi all I have a webservice with a webmethod that accepts an XML from a client via POST (don\'t know if this really matters), validates XML and the data inside, and then processes the XML and returns a

Hi all I have a webservice with a webmethod that accepts an XML from a client via POST (don't know if this really matters), validates XML and the data inside, and then processes the XML and returns an answer to the client.

At moment, I have a problem with large XMLs; it takes too long for the XML preprocessing to finish, and the clients timeout expires.

Instead of increasing the timeout of the client I thought I could maybe separate the XML validation from the XML processing by starting a new thread after the validation which processes the XML and at the same time return the validation answer to the client. (the processing won't start if the XML is not valid.

Now my question is: Will opening the new thread increase memory usage? normally I would guess it will (for normal applications) but here I am returning an answer to the client and essentially ending the process of the main thread.

And another question, I actually tried to create a small webservice that does just that but for some reason, even though the webmethod was finished, the client would not get an answer back until the thread died... am I doing something wrong?

Ok, I rewrote the small webs开发者_如何学Pythonervice and it actully works (I haven't tried it on the real code yet - it's a tad more complicated).

This is the example code if anyone wants it:

Imports System.Web.Services
Imports System.Web.Services.Protocols    
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService()> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class DemoService
    Inherits System.Web.Services.WebService

       <WebMethod()> _
    Public Function XMLReceiver(ByVal XMLData As String) As Integer
        Dim retval As Integer = Validate(XMLData)
        Dim thrd As New Threading.Thread(AddressOf Process)
        thrd.Start()
        Return retval
    End Function

    Public Function Validate(ByVal XMLData As String) As Integer
        Return 0
    End Function

    Public Sub Process()
        'get time
        Dim start As Date = Date.Now
        'do work
        Threading.Thread.Sleep(1000 * 10)
        'notification (actually write to DB)
        Dim writer As New IO.StreamWriter("C:\done.txt")
        writer.WriteLine("From " & start.ToLongTimeString & " To " & Date.Now.ToLongTimeString & " done!")
        writer.Close()
    End Sub
End Class

So Now it's just a matter of the first question.

Thanks everyone


I don't think you need to change the XML process logic, but how your client calls it. You should be calling the webservice asynchronously. So you would provide a callback method that would be called when the webservice has completed its work. Now without seeing the code it is hard to propose any real answer, but I think based on what you have written so far this may be the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消