简体   繁体   中英

Repeated web service calls via a proxy web service - performance

Here's my scenario, I have a nightly batch job that inserts records into a 3rd party system/database. All of these import calls get routed through a proxy service that I created, mainly because my nightly batch job does not have a direct connection to the 3rd party service because of firewalls.

So, I import 10000 records per job run. I establish a connection to my proxy service, where the proxy service establishes a static reference/connection to the 3rd party web service. Right now (don't laugh), I establish the connection for EACH record in my proxy service. That means, I have 10000 "OpenConnection" and "CloseConnection" method calls in my proxy service to accomplish this 1 job run. I run this job twice daily.

My question is, if I establish the connection on the first web service call, but don't close the connection when that first call is made, will subsequent web service calls re-use the existing connection (since it's static)? Or will it re-create another session with the 3rd party service?

If this does work (leaving the connection open for subsequent calls), how do I close the connection when my job is done so I can free my managed (or is it unmanaged) resources? I don't want to add a method in my own web service called "CloseConnection"...

In addition to that, our servers do automatic IIS RESETS on a nightly basis, if the connection is still open at that time, will it dispose of my connection properly?

Sorry, LOTS of questions here, just looking for a "best practice" approach to accomplishing my task of speeding up this process.

I'll try and give a code example of this, here's the batch job code:

ws.EstablishConnection();

for (int counter = 0; counter < 10000; counter++)
{
   ws.ImportRecord(myRecords[counter]);
}

ws.Dispose();

Web service code:

[WebMethod]
public static void ImportRecord(MyRecord myRecord)
{
   try
   {
      OpenConnection();
      _3rdPartyWS.ImportRecord(myRecord);
   }
   catch (Exception)
   {
      throw;
   }
   finally
   {
      CloseConnection();
   }
}

My question is, if I establish the connection on the first web service call, but don't close the connection when that first call is made, will subsequent web service calls re-use the existing connection (since it's static)?

If it is static, the same object will be used. But you won't gain much speed. Watch out because "open" connection may time-out.

Or will it re-create another session with the 3rd party service?

Web Services are "session-less" each call will be in it's own session.

If this does work (leaving the connection open for subsequent calls), how do I close the connection when my job is done so I can free my managed (or is it unmanaged) resources? I don't want to add a method in my own web service called "CloseConnection"...

Your connection object is Static... you can't close connection without calling explicitly "closeConnection.

In addition to that, our servers do automatic IIS RESETS on a nightly basis, if the connection is still open at that time, will it dispose of my connection properly?

The "pool idle" timeout may pass before the nightly base IISReset. The garbage collector will dispose every memory used on your side. The 3rd party may never received the close() request but it would certainly close the connection after some time.

The best thing you could do in your case is implement a web service method that receive an array of MyReccord. You will save a lot of time there.

[WebMethod]
public static void ImportRecord(MyRecord[] myRecords)
{
   try
   {
      OpenConnection();
      for (int counter = 0; counter < myRecords.Length; counter++)
      {
         3rdPartyWS.ImportRecord(myRecords[counter]);
      }
   }
   finally
   {
      CloseConnection();
   }
}

A better case would be, if possible, ask your 3rd party to implement a web method that receive an array of object or a file.

Web Services loop are very slow you should never do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM