How to incorporate a third party api call during b

2019-08-19 07:42发布

问题:

Ok, here is my story.

I have a ASP.Net MVC Web Application which works fine. But it needs some .resx file (some translated culture/language) file which comes by making an API Call to a third party end point. These .resx file needs to be brought in the application's corresponding folder everytime my web project builds. Because these resx file may change at the third party location so I need to get fresh file every time I run a build on my web application.

Project 1: So till now I have a MVC Web application which runs just fine. (Main Application)

Project 2: I have created another small console application (just a basic one) which has one end point which makes call to third party and gets the .resx file and saves to a location.

Question 1:

In order to accomplish what I want do I need to have a separate Project 2, which will make an third party call and copy the file to the Project 1 corresponding directory.

Question 2:

Project 2 which calls the third party needs to be console application or mvc application or what? I am not sure

Question 3:

Am I going in the right direction. All I want it when my project 1 builds, I want to call a third party API and download some files to a particular folder.

Question 4:

Do I need to play with MS-Build to execute Project 2. exe and then Project 1.exe. so that files are there when project 1.exe starts building?

Question 5:

Do I really needan altogether separate project just to make the API CALL to get the resx file?

Please guide me.

Project 2 Code:

     class Program
{
    static void Main(string[] args)
    {
        Task.Run(() => CallHttp());
        System.Console.ReadLine();
    }
    // Simple async function returning a string...
    static public async void CallHttp()
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("https://api.crowdin.com/");
        HttpResponseMessage response = await client.GetAsync("api/project/refele/download/all.zip?key=f23e2c91833c0de737");
        using (Stream stream = await response.Content.ReadAsStreamAsync())
        using (FileStream fs = new FileStream(@".\..\..\alpha\test.zip", FileMode.Create))
            await stream.CopyToAsync(fs);
        System.IO.Compression.ZipFile.ExtractToDirectory(@".\..\..\alpha\test.zip", @".\..\..\beta");
    }

}

回答1:

In your project1 csproj file you can specify a post-built event in the follwing manner:

<PropertyGroup>
  <PostBuildEvent>
    Call "FullyResolvedPathToYour.exe" "Arg1" "Arg2"
  </PostBuildEvent>
</PropertyGroup>

I don't know how your exe is called, so you'll have adjust to meet your requirements. Also note you have Visual Studio environment variables and system environment variables available to you in this format: $(Variable)

For more information on this concept, you can read here: https://docs.microsoft.com/en-us/visualstudio/ide/how-to-specify-build-events-csharp?view=vs-2017



回答2:

You can make API calls to an endpoint using CURL if you want. So you don't have to write a custom exe file to download your resx. The usage of that is beyond this answer. I'm no expert in that. Anyways, you can call it either before your after your csproj file builds.

If you call the API after your build, don't use a <PostBuildEvent>. It's like taking a hammer to surgery. Overkill. Use a Target instead.

<Target Name="Call your Stuff" AfterTargets="Build">
    <Exec Command="FullyResolvedPathToYour.EXE arg1 arg2 etc" WorkingDirectory="..." />
</Target>

If you need to call this target before your build, just change AfterTargets to BeforeTargets There is plenty of documentation for msbuild on msdn for Targets.