Home > Enterprise >  Installing .NET packages for use in IIS web server (Windows Server 2019)
Installing .NET packages for use in IIS web server (Windows Server 2019)

Time:06-17

I'm very new to Windows Server and I don't fully undrestand how it works.

My task is very simple: On an existing website that's running in Windows Server 2019 using IIS, I need to add some backend C# code that makes some HTTP requests. While I'm able to add "System.Net.Http" in my Web.config file, it doesn't seem to be enough. From my understanding I need "System.Net.Http.Extensions" or "System.Net.Http.Formatting", which is not provided. From my understanding, the latter of the two is deprecated.

I came to this conclusion via the following error:

CS1061: 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' does not contain a definition for 'IsSuccessStatusCode' and no extension method 'IsSuccessStatusCode' accepting a first argument of type 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' could be found (are you missing a using directive or an assembly reference?)

default.aspx relevant code:

<script runat="server" language="C#">   
    static readonly HttpClient client = new HttpClient();
    void GetToken()
    {
        var request = new HttpRequestMessage(HttpMethod.Post, "URL");
        request.Content = new FormUrlEncodedContent(new Dictionary<string, string> {
            { "client_id", "ID" },
            { "client_secret", "SECRET" },
            { "grant_type", "client_credentials" }
        });
        var response = client.SendAsync(request);
        if(response.IsSuccessStatusCode)
        {
            var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
            var token = payload.Value<string>("access_token");
            Response.Write(token);          
        }
    }
</script>

At first, I tried to install using the dotnet command. But it does not seem to exist on this system (for some reason I can't seem to find the .NET installation at all...)

After lot's of digging, I managed to install it using powershell Install-Package Microsoft.Net.Http. It also installed a couple dependencies.

Running the following command: ([system.reflection.assembly]::loadfile("C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Extensions.dll")).FullName I get System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, so that's what I used in my Web.config.

I still get an error on the line that adds the Extensions package: Could not load file or assembly 'System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Nothing I've read on google has worked. I tried including assemblybinding to my Web.config with no difference.

I'm really lost as to how I'm supposed to install and use packages.

Web.config currently:

<configuration>
    <system.web>
        <httpRuntime targetFramework="4.5" />
        <compilation>
            <assemblies>
                 <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
                 <add assembly="System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            </assemblies>
        </compilation>
    </system.web>
</configuration>

CodePudding user response:

nor visual studio installed in the system. I don't know how people before me worked on this server,

Well, then that is most of your challenge.

you do NOT mess around with trying to install this, and that, and more on the web server.

What you do is get your hands on the source code and project. You then develop on YOUR computer the appclation. That's where new code, new dependances, and the writnig of new code and the development process will be done.

Once you happy with the results, then you will build and publish this new next great version of your applcation to the web server. And this "act" of publishing will have your code compile, all the .dll's and depenances build, created for you, and that RESULT is what gets sent to the web server.

In other words, you might have 2 or 5 different web sites runnning on that server. Each the developer teams working on their web site applcations does NOT mess around, play with, or worry about the web server. So, you need to get your developer tools, and enviorment setup. And those tools (like visual studio) are CERTAINLY not going to be placed on, or exist on some web server. The web server job is to dish out web pages - not to factality your software development process and cycle.

About the only major issue you need to determine?

Well, did the deveopment model use asp.net web site applcation, or asp.net web site?

They sound similar, but these two development (and deployment) models are VERY different.

But, to modify code, modify this web site?

You without question REALLY need to get a development environment setup, and this will without a doubt require you to setup and have and install Visual Studio on your computer that you plan to use for such software development.

Thus, you can modify, create, change and do standard development on that site and code base. When you are ready, happy, and have tested the working code to your liking?

Then, and only then would you compile, build and publish the resulting system to the web server. But, you certainly never have to muck around, mess around, and start installing things like .net libraries and bits and parts of your applcation. even using 3rd party library code - say ghostscript.net or whatever? That will be part of your build process, and during the publish operations, those libraries and code will be sent, published, and included with your resulting web applcation. As such, you should not have to do really much of anything on the web server. About the ONLY exception would be say your web site is running .net 4.0, and you decide to use the latest .net version (4.8) for this web site?

Then you may well have to install and ensure that .net 4.8 exists on that web server, but for all of the other references, library code, and references and code the web site uses? That will be resolved at build time on YOUR computer, and you only sending the resulting "build" of the web site to the web server.

So, if you start to use say some .net library (such as the zipping one), or some other library? You not be installing those libraries on the web server - the publish and build of your applcation with Visual Studio is responsible for that process.

As a result, it really next to near impossible to modify the web site that is on the web server. The only real exception of course is if they used a web site, as opposed to a web site application . In that case, it is possible from Visual Studio to open up the web site directly (assuming your on the same network), but even then, it not all that great of a idea to try and modify, debug, change, and test against the live web site, since we assume that users are using that site.

Visual studio does automatic install its "own" web server, so you can build, test, run and work on a copy of the web site. Once everything is working as you like, you then are able to publish your working applcation to the live web server.

Bottom line:

If you going to do asp.net development, then you need to have the tools designed for this purpose - any other half baked approach is just that - half baked.

So, you can't be asked to do any kind of asp.net development without the required asp.net development tools, and it really is that simple. But then again, given your responsibility to work on a asp.net site, then all of the above should be crystal clear, and without you having to give any thought to the above if you are in fact a asp.net developer. And if you are not, then you need to become a asp.net developer with experience, or find someone who has such experience.

  • Related