0

Summary:

I am using Hangfire to handle running background jobs at set intervals, like checking for expired Widgets nightly. I want to know the best way to hit an ASP.NET Web Api 2 endpoint (Likely just GET and POST) from a job that is running.

Versions of stuff (if this matters)

  • .NET 4.5 MVC 5
  • Web Api 2(.1?)
  • Hosting sites Windows azure
  • 8GB of RAM see footnote (1)
  • local dev using IIS express (or whatever the builtin one is in VS2013)

My specific questions

  1. Is this implementation the proper way to open/close a connection to my api from something like a background task?
  2. In the code below, how can I dynamically get the client.BaseAddress string (http://whatever.xxx)? It would be incredibly handy to be able to do some like what I have in the second psuedo-code snippet.

Here is the code I got from this blog post

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:43736/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // HTTP GET
    HttpResponseMessage response = await client.GetAsync("api/do/someshiz/");
    if (!response.IsSuccessStatusCode)
    {
        //todo: stuff
    }
}

This is pretty clean already I suppose but I am woefully inexperienced with using the HttpClient class and most of my api interactions are from javascript.

What I want to do for getting the HttpClient.BaseAddress string

if(IAmRunningThisOnMyLocalMachine) {
    baseAddress = "http://localhost:1337/"
} else {
    //I Already have a way to determine if I am on the staging server or live site
    if(IsStagingServer){
        // staging server base address
        baseAddress = "http://staging.whatever.com/"
    } else {
        // live base address
        baseAddress = "http://whatever.com/"
    }
}

For the root else branch it is less of a big deal, those strings are more or less constant. When I am running locally though I want it to work regardless of if I am localhost:AnyPort or any other variations of IIS Express.


1: Just kidding about the RAM, just wanted to see if I could make the read whisper 'wtf' out loud. Please advise if successful

ledgeJumper
  • 3,560
  • 14
  • 45
  • 92
  • How do you handle other settings different for each environments (dev, staging, production)? Do you use `web.config` transformations? – odinserj Dec 09 '14 at 10:07
  • 1
    You can use this approach to call your api: http://stackoverflow.com/questions/13200381/asp-net-mvc-4-application-calling-remote-webapi/13207679#13207679 and then have different endpoint names depending on which url you want to hit. – peco Dec 09 '14 at 10:09
  • @odinserf - I'm not using config transformations. I probably should be but I hadn't used them before and my brief google search didn't turn up any walkthroughs that my brain wanted to grok :). Moving to transformations is on the shortlist, for now I am using a hack solution involving what this question discusses:http://stackoverflow.com/questions/3788605/if-debug-vs-conditionaldebug – ledgeJumper Dec 11 '14 at 13:53

0 Answers0