According to the ServiceNow wiki, each SOAP call into a ServiceNow web service will generate a separate user session. If your user sessions set to long durations of time, and if you are performing lots of separate SOAP calls into a ServiceNow instance, you may be racking up a huge number of active user sessions.
The wiki gives a brief example of how to persist a user session in subsequent SOAP calls using Perl. I have had a few people ask me how the same thing would be done in other languages, such as C#.
I am no C# guru, but I played around with this and got it to work with a C# client by using the ServiceNow tutorial on creating a SOAP web service consumer to ServiceNow using C#.
I followed the wiki example exactly, using the Web References method.
Once I had the code set up and working with the Demo ServiceNow instance, I started to play around with the persistant session capability.
Along with doing this, I also wanted to make sure that the session ID’s were staying consistent. So, I created an additional object on the C# Form called textBoxSessionId.
To set up the soapClient object to capture and reuse cookies from the SOAP Session, we simply need to initialize a CookieContainer on the object:
1 |
Once the “insert” is performed in the example on the wiki, the cookies associated with that SOAP connection are stored in the soapClient object. The next time it performs an insert (or other action), it will reuse the existing JSESSIONID cookie to persist the session.
In order to display the Session ID between each call, I iterated through the soapClient object’s CookieContainer to find the JSESSIONID cookie. I then displayed that value in the Form’s text field.
Here is my C# code for the entire form below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace ExampleWebServiceForWiki { public partial class Form1 : Form { public com.servicenow.demo.ServiceNow_incident soapClient; public Form1() { InitializeComponent(); this.soapClient = new ExampleWebServiceForWiki.com.servicenow.demo.ServiceNow_incident(); soapClient.CookieContainer = new System.Net.CookieContainer(); this.textBoxSessionId.Text = soapClient.CookieContainer.Count.ToString(); } private void buttonResult_Click(object sender, EventArgs e) { // WEB REFERENCE-SPECIFIC CODE System.Net.ICredentials cred = new System.Net.NetworkCredential("admin", "admin"); soapClient.Credentials = cred; com.servicenow.demo.insert insert = new com.servicenow.demo.insert(); com.servicenow.demo.insertResponse response = new com.servicenow.demo.insertResponse(); // END OF WEB REFERENCE CODE insert.category = "Category"; insert.comments = "Comments"; insert.short_description = "My short description"; try { response = this.soapClient.insert(insert); //System.Net.CookieCollection cookies = new System.Net.CookieCollection(); this.textBoxSessionId.Text = this.soapClient.CookieContainer.Count.ToString(); this.richTextBoxResult.Text = "Incident Number: " + response.number + "\n"; this.richTextBoxResult.Text += "Sys_id: " + response.sys_id + "\n"; Uri uri = new Uri("https://demo.service-now.com"); foreach (System.Net.Cookie cookie in this.soapClient.CookieContainer.GetCookies(uri)) { if (cookie.Name.ToString() == "JSESSIONID") { this.textBoxSessionId.Text = "Session ID: " + cookie.Value.ToString(); break; } else { this.textBoxSessionId.Text += "Warning: Could not find the Session ID\n"; } } } catch (Exception error) { this.richTextBoxResult.Text = "JJAERROR: :" + error.Message; } } } } |