0

I'm following the Microsoft guide found here on how to added a named HTTPClient to my SQL Server with SQLCLR. It all seems straight forward once the HTTPClient is added and I've even got the code to add, but I cannot figure out WHERE this code is supposed to go:

services.AddHttpClient("github", c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    // Github API versioning
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
    // Github requires a user-agent
    c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample");
});

Apparently this code is somehow supposed to end up in the Startup class. How it ends up there, I don't understand. I can't even find this class.

I tried adding this code to a C# script but of course it complains that it doesn't know what services is. I don't even know what it is, so I don't expect the SQL Server to know either...

Can someone point me in the right direction please? I'm completely stuck at this point.

EDIT

So I'm struggling because I've misunderstood the way a SQL Server is supposed to bridge a database and an external source. There needs to be a web app between them. The web app handles the HTTPClient, not the database....

10
  • 3
    You don't. HttpClient isn't supported in SQLCLR. What you linked to has nothing to do with SQLCLR, it's a guide on using HttpClientFactory in ASP.NET Core. Commented Dec 3, 2019 at 15:39
  • Well that would explain it... I've been told this is the standard way to do things. If it isn't, then what is? Commented Dec 3, 2019 at 15:41
  • SQLCLR can only work with a limited set of assemblies that's more or less at the level of .NET 3.5. Frankly, if you need HttpClientFactory, you should create a separate Web API or tool that talks to the database instead trying to call others from inside the database Commented Dec 3, 2019 at 15:42
  • That is the standard way of doing things for ASP.NET Core web apps. Just not inside the database. SQLCLR is useful in very specific scenarios, and calling REST APIs using pooling, retries etc from inside the database isn't one of them. Do you want to create a custom type? SQLCLR can do that. A custom calculation function, or a custom aggregation? OK. Async code? No way, it doesn't even know about tasks Commented Dec 3, 2019 at 15:43
  • In fact, if you need remote access and have access to SQL Server 2016 and later, it's far easier to use scripts in R or Python scripts than SQLCLR. Again though, it's even easier to run those scripts outside the database Commented Dec 3, 2019 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.