Hello World API
This tutorial will show how to create a myAvatar ScriptLink-compatible API with .NET 8.0.
Before You Begin
You will need the following to complete this tutorial:
- Visual Studio
- Visual Studio Code
When using Visual Studio you will want the following workloads and components installed.
- ASP.NET and web development workload.
- .NET 8.0 Runtime (if not already included with the install of the above workload)
The following extensions are recommended when using Visual Studio Code.
Create Project
- Visual Studio
- Visual Studio Code
- Launch Visual Studio 2022.
- Create a new project.
- Select C# and then search for ASP.NET.
- Select ASP.NET Core Empty and then select Next.
- Configure your new project.
- Set your project name.
- Select the location to store your project.
- Set your solution name.
- Don't check Place solution and project in the same folder. we will be adding additional projects to this solution in other tutorials.
- Select Next.
- Set the Framework to .NET 8.0 (Long Term Support).
- Select Configure for HTTPS.
- Select Create.
- Open Windows Terminal.
- Navigate to the directory you wish to store the solution and project in.
- Create the solution and then change to that directory
dotnet new sln --output ScriptLinkHelloWorldDemo
cd ScriptLinkHelloWorldDemo
dotnet new web --output ScriptLinkHelloWorldDemo
dotnet sln add ScriptLinkHelloWorldDemo/ScriptLinkHelloWorldDemo.csproj
code .
Add Dependencies
We will add two NuGet packages to our project.
- Visual Studio
- Visual Studio Code
- Right-click on the soution in Solution Explorer and select Manage NuGet Packages for Solution....
- Select Browse and search for AvatarScriptLink. Select RarelySimple.AvatarScriptLink by Scott Olson Jr and install this in your project.
- Select Browse and search for SoapCore. Select SoapCore by Digital Design and install this in your project.
- Select the Install tab, clear the search text and confirm you now have two packages installed in your project.
- Change to the project directory.
- Select View > Terminal (Ctrl +`) to show the Terminal.
- Install RarelySimple.AvatarScriptLink with
dotnet add package RarelySimple.AvatarScriptLink
. - Install SoapCore with
dotnet add package SoapCore
.
cd ScriptLinkHelloWorldDemo
dotnet add package RarelySimple.AvatarScriptLink
dotnet add package SoapCore
Create the Service Contract
- Add a new class to your project named IScriptLinkService2015.
- Implement the GetVersion and RunScript operation contracts as shown below.
using RarelySimple.AvatarScriptLink.Objects;
using System.ServiceModel;
namespace ScriptLinkHelloWorldDemo
{
[ServiceContract]
public interface IScriptLinkService
{
[OperationContract]
string GetVersion();
[OperationContract]
OptionObject RunScript(OptionObject optionObject, string parameter);
}
}
Create ScriptLink Service
- Add a new class to your project named ScriptLinkService.
- Inherit this class from IScriptLinkService2015.
- Implement the GetVersion and RunScript methods as shown below.
using RarelySimple.AvatarScriptLink.Objects;
namespace ScriptLinkHelloWorldDemo
{
public class ScriptLinkService : IScriptLinkService2015
{
public string GetVersion()
{
return "0.1.0";
}
public OptionObject2015 RunScript(OptionObject2015 optionObject, string parameter)
{
return optionObject.ToReturnOptionObject(ErrorCode.Alert, "Hello World!");
}
}
}
Update Program.cs
In this step, we are going to replace the default "Hello World!" API with our new ScriptLink-compatible API.
- Open the Program.cs file.
- Update the file to include the following:
using Microsoft.Extensions.DependencyInjection.Extensions;
using SoapCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSoapCore();
builder.Services.TryAddSingleton<IScriptLinkService2015, ScriptLinkService>();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
_ = endpoints.UseSoapEndpoint<IScriptLinkService>("/ScriptLinkService.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
// Optional Alternative
// _ = endpoints.UseSoapEndpoint<IScriptLinkService>("/ScriptLinkService.svc", new SoapEncoderOptions(), SoapSerializer.DataContractSerializer);
});
app.Run();
Update Launch Settings
This will help us launch the app and load the WSDL in the browser once running.
- Expand the Properties Folder and open the launchSettings.json file.
- In the https profile insert the following line before the applicationUrl:
"launchUrl": "ScriptLinkService.asmx",
.
{
// previous content
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "ScriptLinkService.asmx",
"applicationUrl": "https://localhost:7140;http://localhost:5106",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
// following content
}
Run the application
- Visual Studio
- Visual Studio Code
- Run/debug the application with the https profile.
dotnet run --launch-profile https
Congratulations! You should now see your WSDL showing in the browser and are ready to make the API calls.
If the WSDL was not automatically launched in your browser, you can get the URL from the launchSettings.json file. For example, https://localhost:7140/ScriptLinkService.asmx as seen in the same launchSettings file above.