SQL CLR function stops working sometime after processing 10,000 web requests....

The CLR functon below works fine up to around 12,000 requests and then stops working. Am I missing something? If I remove then redeploy the function it again will work for another 12,000 requests. I don't see a memory leak but then my C# skills are rusty. Thanks!-----------------------------------------------------------------------------------------------
-- Web Request CLR functions.
USE MyDb
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetWebRequest]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetWebRequest]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PostWebRequest]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[PostWebRequest]
GO
BEGIN TRY
DROP ASSEMBLY [SqlClrLibrary]
END TRY
BEGIN CATCH
SELECT 'SqlClrLibrary not found.... creating'
END CATCH
CREATE ASSEMBLY [SqlClrLibrary]
FROM 'c:\\SqlClrLibrary.dll'
WITH PERMISSION_SET=UNSAFE;
GO
-- PostWebRequest
USE MyDb
GO
/****** Object: UserDefinedFunction [dbo].[PostWebRequest] Script Date: 07/18/2014 12:03:32 ******/
CREATE FUNCTION [dbo].[PostWebRequest](@uri [nvarchar](256), @postdata [nvarchar](max), @UserAgent [nvarchar](256), @ContentType [nvarchar](256))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlClrLibrary].[SqlClrLibrary.Functions].[POST]
GO
// Below is the CLR code....
using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.SqlServer.Server;
namespace SqlClrLibrary
/// <summary>
/// CLR Functions.
/// </summary>
    public static class Functions
/// <summary>
/// Send an Http Post request and return a scrubbed result.
/// NOTE: The response is always set to lowercase... This is due to performance issues related to sql injection scrubbing.
/// See SqlInjectionScubber.Scrub.
/// </summary>
/// <param name="Uri">Web address for the POST request.</param>
/// <param name="PostData">Payload name value pairs for request parameters.</param>
/// <param name="UserAgent">Http user agent sending this request.</param>
/// <param name="ContentType">Mime type.</param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static string POST(string Uri, string PostData, string UserAgent, string ContentType)
var postByteArray = Encoding.UTF8.GetBytes(Convert.ToString(PostData));
// Set up the request
var req = WebRequest.Create(Uri);
req.Method = "POST";
req.ContentType = ContentType;
((HttpWebRequest)req).UserAgent = UserAgent;
// Submit the POST data
var dataStream = req.GetRequestStream();
dataStream.Write(postByteArray, 0, postByteArray.Length);
dataStream.Close();
// Collect the response, put it in the string variable "document"
var resp = req.GetResponse();
dataStream = resp.GetResponseStream();
if (dataStream != null)
var rdr = new StreamReader(dataStream);
var document = rdr.ReadToEnd();
// Close up and return
rdr.Close();
dataStream.Close();
resp.Close();
// Scrub the result.
return document;
return String.Empty;
Scott Mitchell

Hi Scott,
There's not enough information there to troubleshoot the problem. Try looking in the forum for similar problem postings (there have been a few, IIRC). First thing to try would be issuing the same number of requests in a batch program, to isolate the behavior
to "code is running in SQLCLR". That would also help you isolate memory leaks, if any. Also:
Need version of SQL Server (version, service pack, and cumulative updates, is it 32 or 64 bit version). It matters.
Define "stops working". Does it produce an error message or appear to hang or something else? Does it bring down the appdomain (there's messages for that in SQL Server log). And does "redeploy" mean just DROP/CREATE assembly? Or bounce
SQL Server, or something else?
And since it's a web service you're calling... if you trace to network traffic, is the message actually sent? Is the message received at the service? Does the service return a response? And does the SQL function process the response? Is it "your"
service or an "outside" service (perhaps there's a limit on number of requests or number of requests in a certain time period. Is it exactly 12000 requests or about 12000, and how many requests per minute? Isolate where in the intersystem communication
it fails.
It could be a memory shortage or fragmentation problem. SQLCLR memory handling differs in SQL Server 2012 vs. other releases and also differs in 32 vs. 64 bit SQL Server. You can look at memory consumed by SQLCLR (sys.dm_os_memory_objects where
type like '%CLR%'). You can look at perfmon counter(s) for SQL Server or .NET perfmon counters for the SQL Server process to track CLR garbage collections (which does work differently in SQLCLR). You can look at sys.dm_os_wait_stats if it's hanging. And in
SQL Server log for SQL Server errors.  SQLCLR can fragment memory in older versions so that there's not a big enough fragment left to allocate. If it's pre-2012 read up on "mem-to-leave or mem-to-reserve" (two different terms for the same
thing).
Hope this helps, Bob

Similar Messages

  • The image sequence function stopped working correctly after recent update?

    I have been using this function in quicktime 7 pro several times a week on a large project for over a year. After the most recent update to mavericks, the DSLR and gopro jpegs now open in digitized chunks of random colours. I have not changed my workflow or the gear in any way. I used the exact same files on my macbook air, without the recent OS update and was able to open and export the files like normal.
    The updated machine is an iMac running OS X 10.9.2 with 3.4GHz i7 and 32gb

    I'm still experiencing this problem. Is there anyone else who uses this function and has recently run into trouble?
    What image resolution(s) are you using for your image sequences?

  • My iPod nano 6G stop working suddenly; After i reset it and even afetr put it in "stand by" mode by accessing deagnostic mode, it is still not working; Now i can access it sometimes (the main screen), but is not playing and is too slow. How can i fix it

    My iPod nano 6G stop working suddenly; After i reset it and even afetr put it in "stand by" mode by accessing deagnostic mode, it is still not working; Now i can access it sometimes (the main screen), but is not playing and is too slow. How can i fix it

    If you do not find a solution in the iPod nano (6th generation) - User Guide, suggest that you call AppleCare if still under warranty or take a trip to your local Apple Store or AASP.

  • Audio stops working right after login

    Hi everyone, I have a macbook pro 13inch mid 2009 running mac osx 10.7 lion. Few weeks ago the audio stopped working but after few moments and 2 reboots it started to work fine again. Now are 2 days that the problem came back. I tried to reboot multiple times, to reset the nvram and pram, to use headphones (they dont work either) and to kill the process coreaudiod. Still nothing! I know that it is not a hardware problem since the sounds stops working just right after I login (the sound at the start works and on the login screen the audio controllers still emit the sound), the only sound that seems work after the login is the sound of the trash folder when I empty it (weird isnt it?). I know that a osx reinstallation would probably solve the problem but I dont have time for that right now and I dont want to risk to lose some file. Does any of you have any solution?

    System Preferenes > Sound > Output > Internal Speakers
    Make sure that Mute is not enabled.
    Reset SMC.     http://support.apple.com/kb/HT3964
    Choose the method for:
    "Resetting SMC on portables with a battery you should not remove on your own".
    If you see red light in the headphone port, plug the headphone
    in and out of the port 5 or 6 times to flip the microswitch inside.
    Sound output may be stuck in the digital mode.
    Best.

  • My MacBook Pro is crashing and Mac OS stops working sometimes so I have to restart it. And sometimes it stays on the first white-ish screen with the Apple and won't start further. What to do?

    my MacBook Pro is crashing and Mac OS stops working sometimes so I have to restart it. And sometimes it stays on the first white-ish screen with the Apple and won't start further.
    What to do?
    Does that mean that the is the end of my Mac Book 5bought in the summer of 2009) ? Or is there someting to do to fix it?
    Thanks
    V

    Try this:
    How to Perform an Archive and Install
    An Archive and Install will NOT erase your hard drive, but you must have sufficient free space for a second OS X installation which could be from 3-9 GBs depending upon the version of OS X and selected installation options. The free space requirement is over and above normal free space requirements which should be at least 6-10 GBs. Read all the linked references carefully before proceeding.
    1. Be sure to use Disk Utility first to repair the disk before performing the Archive and Install.
    Repairing the Hard Drive and Permissions
    Boot from your OS X Installer disc. After the installer loads select your language and click on the Continue button. When the menu bar appears select Disk Utility from the Installer menu (Utilities menu for Tiger, Leopard or Snow Leopard.) After DU loads select your hard drive entry (mfgr.'s ID and drive size) from the the left side list. In the DU status area you will see an entry for the S.M.A.R.T. status of the hard drive. If it does not say "Verified" then the hard drive is failing or failed. (SMART status is not reported on external Firewire or USB drives.) If the drive is "Verified" then select your OS X volume from the list on the left (sub-entry below the drive entry,) click on the First Aid tab, then click on the Repair Disk button. If DU reports any errors that have been fixed, then re-run Repair Disk until no errors are reported. If no errors are reported click on the Repair Permissions button. Wait until the operation completes, then quit DU and return to the installer. Now restart normally.
    If DU reports errors it cannot fix, then you will need Disk Warrior and/or Tech Tool Pro to repair the drive. If you don't have either of them or if neither of them can fix the drive, then you will need to reformat the drive and reinstall OS X.
    2. Do not proceed with an Archive and Install if DU reports errors it cannot fix. In that case use Disk Warrior and/or TechTool Pro to repair the hard drive. If neither can repair the drive, then you will have to erase the drive and reinstall from scratch.
    3. Boot from your OS X Installer disc. After the installer loads select your language and click on the Continue button. When you reach the screen to select a destination drive click once on the destination drive then click on the Option button. Select the Archive and Install option. You have an option to preserve users and network preferences. Only select this option if you are sure you have no corrupted files in your user accounts. Otherwise leave this option unchecked. Click on the OK button and continue with the OS X Installation.
    4. Upon completion of the Archive and Install you will have a Previous System Folder in the root directory. You should retain the PSF until you are sure you do not need to manually transfer any items from the PSF to your newly installed system.
    5. After moving any items you want to keep from the PSF you should delete it. You can back it up if you prefer, but you must delete it from the hard drive.
    6. You can now download a Combo Updater directly from Apple's download site to update your new system to the desired version as well as install any security or other updates. You can also do this using Software Update.

  • Photoshop Elements 10 Stops Working just after start.

    I updated to the new insperation browser.  The ogrinizer starts but stops working just after the start.  I'm using Windows 7 Home permium.  It worked before the update.
    Doug Van Dusen
    [email protected]

    Try downloading and installing outside of the Organizer or Welcome Screen. Uninstall AIR and IB via the control panel. Then do ctrl+alt+delete and open the  processes tab in task manager. Highlight any running Adobe application or process and end it. Use the link below and it will install both IB and AIR.
    N.B. when using with Premiere Elements on Windows 64 bit systems, the inspiration browser needs to be installed in both locations C: Program Files/Adobe and C: Program Files (x86)/Adobe
    Inspiration Browser Get Update Now

  • Need help with magic trackpad.  some touch functions stopped working.  battery full and all touch options checked appropriately.  whats the deal?

    i have tried everything but the tap functions stopped working.  any ideas?

    Update: I mucked about more after making this post; I installed gnome-alsa-mixer and checked the "internal mic" option and "Auto-Mute mode" (both were off), after this my headphone output works again, and does the built-in microphone (!)
    I have no idea how I ended up in this "broken" situation, or even what this option does, or how to use it without gnome-alsa-mixer (I had to install 15 packages to get this, as I normally don't use gnome)... Sound on Linux still seems no better than when I started using it 15 years ago :-( (I spent about 5 hours on this...)

  • My iPhone5 lock screen stops working sometimes

    My iPhone5 lock screen stops working sometimes.I have to restart the phone every time I encounter this problem. Sometime even when phone is locked and there's incoming call I couldn't answer simply because the touch stops working. This frequency of occurrence is increasing now.
    Is this some kind of software issue because I noticed this after updating to 7.0.2 iOS ?

    Because something is wrong with your iphone.
    Try the basics from the manual: restart, reset, restore.
    If these fail, it is likely a hardware issue.  Make an appointment at the genius bar.

  • My bluetooth suddenly stopped working soon after I upgraded to OS 10.9.4. I rebooted SMC, didn't work. Any other ideas?

    My bluetooth suddenly stopped working soon after I upgraded to OS 10.9.4. I rebooted SMC, didn't work. Any other ideas? Don't know if it's related to the upgrade. I see other complaints about bluetooth in the thread but none of the solutions offered there have worked for me.

    Make a "Genius" appointment at an Apple Store, or go to another authorized service provider. You may have to leave the machine there for several days.
    Back up all data on the internal drive(s) before you hand over your computer to anyone. There are ways to back up a computer that isn't fully functional—ask if you need guidance.
    If privacy is a concern, erase the data partition(s) with the option to write zeros* (do this only if you have at least two complete, independent backups, and you know how to restore to an empty drive from any of them.) Don’t erase the recovery partition, if present.
    Keeping your confidential data secure during hardware repair
    Apple also recommends that you deauthorize a device in the iTunes Store before having it serviced.
    *An SSD doesn't need to be zeroed.

  • IPod functions stops working.

    The iPod functions stops working after 5 seconds. I have set the phone with no luck.

    Same thing happened with my mine after downloading a new song from my iphone. I just plugged it into the computer and even before it synced it was working again. Hopefully this helps you.

  • My IPhone stopped working right after I did the most current update, yesterday.  The screen has a picture of the usb cord, pointing towards the ITunes logo. I cannot use the phone at all.  I have tried turning it off and on.ITunes won't recognize device

    Hi there,
    Can someone please help.  My IPhoneS stopped working right after I did the latest update which was yesterday.  Now, the phone does not work at all.  When turned on, the screen shows a picture of USB cable pointing to the ITunes logo.  I have tried just about everything, and cannot get it working. It's like it is frozon.  My ITunes does not recognize my device.  Cannot dial out either. I desperately need to get it working.  Can someone please help me. 
    Thanking you in advance for your help.  :-)

    The update did not do anything.
    AT&T cannot and will not do anything, they do not provide support for iPhones, Apple does.
    The issue is not with the iPhone or iOS if you've reset network settings and tried forgetting the Wi-Fi Network, it is specific to the network.
    Check the settings on the router, try disabling the security settings first... that will clarify the issue is not with the device but rather the Network.

  • Getting itunes has stopped working message after downloading to Windows 8.1

    Getting ITunes has stopped working message after 10 seconds after downloading to Windows 8.1

    For general advice see Troubleshooting issues with iTunes for Windows updates.
    The steps in the second box are a guide to removing everything related to iTunes and then rebuilding it which is often a good starting point unless the symptoms indicate a more specific approach. Review the other boxes and the list of support documents further down the page in case one of them applies.
    Your library should be unaffected by these steps but there is backup and recovery advice elsewhere in the user tip.
    You could also try working through
    TS1717: iTunes for Windows Vista, Windows 7, or Windows 8: Fix unexpected quits or launch issues
    tt2

  • ITunes has stopped working message after having it open for one minute.

    It's fully updated to iOS 5 and was working fine until now. Running on Windows 7 and worked flawlessly till this point. Any tips? I already uninstalled and reinstalled. Only security software I run is Microdoft Security Essesntials and that has never been an issue. At a loss here.

    iTunes has stopped working message after having it open for one minute.
    I open itunes and a message comes up after a minute "itunes has stopped working". I got the update automatically to my Windows 7 64 bit pc 10.5.0.142.  After reading tons of probelms online about version 10.5
    I have decided to do nothing for now.  Seems people are putting hours into trying to fix it.  I was going to uninstall and reinstall but seems that isn;t working for to many people.
    How do we get some help from apple? 
    Anyone have any answers???  APPLE HELP please!!!!!!!!!!!!!!!

  • Upon sync or backup, I receive the message, "iTunes could not back up the iPhone "iPhone name" because and error occurred. I have deleted my backup file and tried another computer, with no success.  Also, the bluetooth function stopped working.

    Upon sync or backup, I receive the message, "iTunes could not back up the iPhone "iPhone name" because and error occurred. I have deleted my backup file and tried another computer, with no success.  Also, the bluetooth function stopped working.

    Hi, EJ13. 
    I see you are having issues with backing up you iPhone and have tried a couple troubleshooting steps.  There are a couple more steps I could recommend in the article below. 
    iOS: Troubleshooting backup issues in iTunes
    http://support.apple.com/kb/ts2529
    Regards,
    Jason H. 

  • Copy and paste function stopped working?

    The copy and paste function stopped working on my laptop (ibook g4, OSX 10.3.9). It does not work in any applications, tried MS word, safari, preview.
    How do I recover this function?
    Thanks.

    Hi Magno2002
    Your Finder may have lost contact with the clipboard, so try this.
    In ~/library/preferences locate these two files and trash them
    com.apple.finder.plist
    com.apple.sidebarlists.plist
    To re-establish them, log out and log back in again.
    You will need to reset your finder and sidebar as you had them before.
    regards roam

Maybe you are looking for

  • How do I change the color of the area next to the Tab Bar

    I've got Firefox 8.0, and ever since they went to the new layout I've had problems with the area at the right of the Tab Bar. When FF is maximized, the buttons up there ( > + ▼ X ) are black on a dark blue background, and I almost can't see them - so

  • How to add name from text file to jtable?

    hello all, how to add name (string) from the notepad file(.txt) into jTable rows. I am not able to add it. try {              FileInputStream fis = new FileInputStream("Devices/Devices_Lst.txt");              BufferedReader br = new BufferedReader(ne

  • In the DSP, get/set the data of SCXI1162 through Multifunction E series board

    I am integrating DSP board(cPCI) with your scxi1162, pxi-1052, pxi-6040e. For the configuration of scxi-1162, the sbc will do that and DSP read the data through pxi-6040e. Can I do this ? Because you made the driver for pxi-6040e with scxi-1162 and I

  • Trouble to add a printed shared in windows

    I'm trying to add a shared printer on Windows 7 on my macbook with leopard, but I get an error concerning the CIFS protocol and tries to connect in 60 seconds but nothing happens. I read that I have activated the SMB / CIFS Directories Settings, but

  • Portal tab as a input to WD aplication

    Hi Experts, I am developing an application in NWDS 7.11, this application will be integrated to EP. This application will be used under three different portal tabs (Ex: US, India, UK), this single webdynpro application should display the data based o