WCF SOAP, ASP.NET Core & Angular application - daaren-urthling/tutorials GitHub Wiki

How to consume a WCF SOAP Web Service in an ASP.NET Core Angular application

This tutorial shows how to build an ASP.NET Core web app with an Angular front-end which consumes WCF SOAP Web Services in the back-end, exposing them as REST WS to its front-end.

Everything is done with Visual Studio Code and from the command line, using the Angular and the .NET Core CLI.

Prerequisites

Create a ASP.NET Core service

In VS Code installs the "C# for Visual Studio Code" plugin.

To create an ASP.NET Core service which consumes the WCF SOAP Web services:

Create the project

dotnet new webapi -o MyWCFApp

open the folder with VS Code: code MyWCFApp.

Add to the project the references to the following nuget packages:

dotnet add package System.ServiceModel.Duplex --version 4.5.*
dotnet add package System.ServiceModel.Http --version 4.5.*
dotnet add package System.ServiceModel.NetTcp --version 4.5.*
dotnet add package System.ServiceModel.Security --version 4.5.*

and then run dotnet restore to install them.

Install the dotnet-svcutil NuGet package as a CLI tool:

  dotnet tool install --global dotnet-svcutil

Create a folder named ConnectedServices.
Open a command prompt and create the wrapper for the service using the dotnet-svcutil tool:

dotnet svcutil http://localhost/development/loginmanager/loginmanager.asmx

rename the folder ServiceReference1 created by the tool with something more meaningful (i.e.: M4LoginManager), and move it under the ConnectedServices folder.

Open the Reference.cs file generated by the tool and embrace all the code into a namespace named after the folder, i.e.:

namespace M4LoginManager 
{
...
}

consume the service within the controllers, in this way:

[HttpGet("isAlive")]
public async Task<bool> IsAlive()
{
    using (M4LoginManager.MicroareaLoginManagerSoapClient m4Login = new M4LoginManager.MicroareaLoginManagerSoapClient(M4LoginManager.MicroareaLoginManagerSoapClient.EndpointConfiguration.MicroareaLoginManagerSoap))
    {
        var data = await m4Login.IsAliveAsync();
        return data;
    }
}

We do not need to support, at the beginning, HHTPS, so edit Startup.cs file and remove the following line in the Configure method:

...
app.UseHttpsRedirection();
...

Edit then the Properties\LaunchSettings.json file, locate the section named after your project (i.e. MyWCFApp) and change it in this way:

"MyWCFApp": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "/",
  "applicationUrl": "http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

We can test our service: first build and run it, opening a VS Code Terminal window and launching:

dotnet run

Then, open a browser and navigate to:

http://localhost:5000/api/values/isalive

We should get the answer from the invoked method of our service.

Improving the development cycle

VS Code offers a series of functionalities to speed up the development activities.

Build

Ctrl+Shift+B trigger the Build task. It can be freely configured to use any tool, and we will do it for the .NET Core compilation.
Press Ctrl+Shift+B, VS Code offers to create a default configuration file; choose those for .NET Core and replace the proposed contents with this one:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/${workspaceFolderBasename}.csproj"
            ],
            "problemMatcher": "$msCompile",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Now hitting Ctrl+Shift+B will launch the .NET Core project build (providing that the .csproj name matches those of the project folder, that is the usual case).

Debugging

Hitting F5 starts the debugger. Again VS Code allows debugging with a number of tools, and it has a default configuration for .NET Core Services. We need a configuration which runs just the service, without opening any web page; to configure it, add this at the end of the proposed launch.json file:

{
    "name": "Server only",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "build",
    // If you have changed target frameworks, make sure to update the program path.
    "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/${workspaceFolderBasename}.dll",
    "args": [],
    "cwd": "${workspaceFolder}",
    "stopAtEntry": false,
    "internalConsoleOptions": "openOnSessionStart",
    "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
    },
    "sourceFileMap": {
        "/Views": "${workspaceFolder}/Views"
    }
}

In the Debug page of VS Code it will be possible to choose which launch confiruration to use when hitting F5, "Server only" will appear in the list.

⚠️ **GitHub.com Fallback** ⚠️