Question

Environment: C#, .Net 3.5, Sql Server 2005

I have a method that works in a stand-alone C# console application project. It creates an XMLElement from data in the database and uses a private method to send it to a web service on our local network. When run from VS in this test project, it runs in < 5 seconds.

I copied the class into a CLR project, built it, and installed it in SQL Server (WITH PERMISSION_SET = EXTERNAL_ACCESS). The only difference is the SqlContext.Pipe.Send() calls that I added for debugging.

I am testing it by using an EXECUTE command one stored procedure (in the CLR) from an SSMS query window. It never returns. When I stop execution of the call after a minute, the last thing displayed is "Calling GetResponse() using http://servername:53694/odata.svc/Customers/". Any ideas as to why the GetResponse() call doesn't return when executing within SQL Server?

private static string SendPost(XElement entry, SqlString url, SqlString entityName)
{
    // Send the HTTP request
    string serviceURL = url.ToString() + entityName.ToString() + "/";
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serviceURL);
    request.Method = "POST";
    request.Accept = "application/atom+xml,application/xml";
    request.ContentType = "application/atom+xml";
    request.Timeout = 20000;
    request.Proxy = null;

    using (var writer = XmlWriter.Create(request.GetRequestStream()))
    {
        entry.WriteTo(writer);
    }

    try
    {
        SqlContext.Pipe.Send("Calling GetResponse() using " + request.RequestUri);
        WebResponse response = request.GetResponse();
        SqlContext.Pipe.Send("Back from GetResponse()");

        /*
        string feedData = string.Empty;

        Stream stream = response.GetResponseStream();
        using (StreamReader streamReader = new StreamReader(stream))
        {
            feedData = streamReader.ReadToEnd();
        }
        */

        HttpStatusCode StatusCode = ((HttpWebResponse)response).StatusCode;
        response.Close();

        if (StatusCode == HttpStatusCode.Created /* 201 */ )
        {
            return "Created @ Location= " + response.Headers["Location"];
        }

        return "Creation failed; StatusCode=" + StatusCode.ToString();
    }
    catch (WebException ex)
    {
        return ex.Message.ToString();
    }
    finally
    {
        if (request != null)
            request.Abort();

    }
}
Was it helpful?

Solution

The problem turned out to be the creation of the request content from the XML. The original:

using (var writer = XmlWriter.Create(request.GetRequestStream()))
{
    entry.WriteTo(writer);
}

The working replacement:

    using (Stream requestStream = request.GetRequestStream())
    {
        using (var writer = XmlWriter.Create(requestStream))
        {
            entry.WriteTo(writer);
        }
    }

OTHER TIPS

You need to dispose the WebResponse. Otherwise, after a few calls it goes to timeout.

You are asking for trouble doing this in the CLR. And you say you are calling this from a trigger? This belongs in the application tier.

Stuff like this is why when the CLR functionality came out, DBAs were very concerned about how it would be misused.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top