TownSuite / TownSuite.Web.SSV3Adapter

Basic service stack v3 facade.
Other
1 stars 2 forks source link

asp.net core WebAPI support #4

Open majorsilence opened 2 years ago

majorsilence commented 2 years ago

Is your feature request related to a problem? Please describe. Currently, software that is using service stack version 3 style services work. This style of program is rare in the larger programming community and requires hacks and ongoing training of new developers. Provide a method of easily porting this code directly to asp.net webapi.

Describe the solution you'd like Any of the following are potential solutions

Justinreuben commented 2 years ago

It sounds like the application in question has an API but the data has been formatted in a custom way which makes it hard to read?

I would create an application that connects to the application in question's API, and can parse the custom data format and then convert it to a more readable format like XML or JSON. The application would also have its own API which would receive query requests and return the newly formatted data in, for example, JSON format.

Let me know if this is not what the issue is, I'm not sure what you mean by porting the code to a webapi?

Thanks, Justin

majorsilence commented 2 years ago

asp.net core has a project type known as webapi. It is used to create REST endpoints for data exchange between programs.

https://dotnet.microsoft.com/en-us/apps/aspnet/apis

Service stack is an alternative. There is an older version of service stack called version 3 (confined to .net framework). This project implements a small compat route extension to let basic service stack version 3 services but in net6.0. The service stack v3 compat layer produces and accepts json output and input.

Using this is a pain point.

It would be better to directly use modern asp.net core frameworks. This particular issue is to figure out if there is an easy path forward to turn a service stack v3 service class into an asp.net core restful webapi controller.

https://docs.microsoft.com/en-ca/aspnet/core/web-api/?WT.mc_id=dotnet-35129-website&view=aspnetcore-6.0

Justinreuben commented 2 years ago

Thanks Peter, I’ll have a look at those links.

Justin

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: Peter @.> Sent: May 19, 2022 12:37 PM To: @.> Cc: @.>; @.> Subject: Re: [TownSuite/TownSuite.Web.SSV3Facade] asp.net core WebAPI support (Issue #4)

asp.net core has a project type known as webapi. It is used to create REST endpoints for data exchange between programs.

https://dotnet.microsoft.com/en-us/apps/aspnet/apis

Service stack is an alternative. There is an older version of service stack called version 3 (confined to .net framework). This project implements a small compat route extension to let basic service stack version 3 services but in net6.0. The service stack v3 compat layer produces and accepts json output and input.

Using this is a pain point.

It would be better to directly use modern asp.net core frameworks. This particular issue is to figure out if there is an easy path forward to turn a service stack v3 service class into an asp.net core restful webapi controller.

https://docs.microsoft.com/en-ca/aspnet/core/web-api/?WT.mc_id=dotnet-35129-website&view=aspnetcore-6.0

— Reply to this email directly, view it on GitHubhttps://github.com/TownSuite/TownSuite.Web.SSV3Facade/issues/4#issuecomment-1131847354, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZEB632PTBGMJVOUM3XIZIDVKZKMLANCNFSM5VOGGJ4Q. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Justinreuben commented 2 years ago

Hello Peter,

I started a tutorial at Tutorial: Create a web API with ASP.NET Core | Microsoft Docshttps://docs.microsoft.com/en-ca/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio It’s very similar to what is in the townSuite app so I’m going to go through this and hopefully I’ll have a better understanding of what’s going on in that code.

Thanks, Justin

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: Peter @.> Sent: May 19, 2022 12:37 PM To: @.> Cc: @.>; @.> Subject: Re: [TownSuite/TownSuite.Web.SSV3Facade] asp.net core WebAPI support (Issue #4)

asp.net core has a project type known as webapi. It is used to create REST endpoints for data exchange between programs.

https://dotnet.microsoft.com/en-us/apps/aspnet/apis

Service stack is an alternative. There is an older version of service stack called version 3 (confined to .net framework). This project implements a small compat route extension to let basic service stack version 3 services but in net6.0. The service stack v3 compat layer produces and accepts json output and input.

Using this is a pain point.

It would be better to directly use modern asp.net core frameworks. This particular issue is to figure out if there is an easy path forward to turn a service stack v3 service class into an asp.net core restful webapi controller.

https://docs.microsoft.com/en-ca/aspnet/core/web-api/?WT.mc_id=dotnet-35129-website&view=aspnetcore-6.0

— Reply to this email directly, view it on GitHubhttps://github.com/TownSuite/TownSuite.Web.SSV3Facade/issues/4#issuecomment-1131847354, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZEB632PTBGMJVOUM3XIZIDVKZKMLANCNFSM5VOGGJ4Q. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Justinreuben commented 2 years ago

I have a solution to asp.net core WebAPI support #4 I couldn’t push the changes I made to the TownSuite/TownSuite.Web.SSV3Facade code on GitHub so I attached a word document on how to do it. I also put an application on GitHub called “connect” that tests the new controller.

You just need to add the attached .cs file to the controllers folder in the TownSuite/TownSuite.Web.SSV3Facade Example web API.

Hopefully you won’t have any issues getting this to work.

Thanks, Justin

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: Peter @.> Sent: May 19, 2022 12:37 PM To: @.> Cc: @.>; @.> Subject: Re: [TownSuite/TownSuite.Web.SSV3Facade] asp.net core WebAPI support (Issue #4)

asp.net core has a project type known as webapi. It is used to create REST endpoints for data exchange between programs.

https://dotnet.microsoft.com/en-us/apps/aspnet/apis

Service stack is an alternative. There is an older version of service stack called version 3 (confined to .net framework). This project implements a small compat route extension to let basic service stack version 3 services but in net6.0. The service stack v3 compat layer produces and accepts json output and input.

Using this is a pain point.

It would be better to directly use modern asp.net core frameworks. This particular issue is to figure out if there is an easy path forward to turn a service stack v3 service class into an asp.net core restful webapi controller.

https://docs.microsoft.com/en-ca/aspnet/core/web-api/?WT.mc_id=dotnet-35129-website&view=aspnetcore-6.0

— Reply to this email directly, view it on GitHubhttps://github.com/TownSuite/TownSuite.Web.SSV3Facade/issues/4#issuecomment-1131847354, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZEB632PTBGMJVOUM3XIZIDVKZKMLANCNFSM5VOGGJ4Q. You are receiving this because you are subscribed to this thread.Message ID: @.***>

using Microsoft.AspNetCore.Mvc; using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization.Json; using System.Net.Http; using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq;

// this controller was added (connect to TownSuite Web API Controller) // to redirect requests to https://localhost:7133/example/service/json/syncreply/Example2 // the url is ROOT + api/MiddleWare/Examlpe2 (https://localhost:7133/api/MiddleWare/Examlpe2) // this controller connects to another API instead of directly to a database.

namespace TownSuite.Web.Example.Controllers { [ApiController] [Route("api/MiddleWare")]

public class connectToTSwebAPIController : ControllerBase
{
    string URL = "";
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    StreamReader reader = null;
    string json = "";

    private readonly ILogger<connectToTSwebAPIController> _logger;

    public connectToTSwebAPIController(ILogger<connectToTSwebAPIController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public string GetOnPageLoad()
    {
        URL = @"https://localhost:7133/example/service/json/syncreply/Example2";
        request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";
        string jsonResponse = "";
        Newtonsoft.Json.Linq.JObject jobj = new Newtonsoft.Json.Linq.JObject();

        try
        {
            // inject json into the requset
            // the below attributes are assigned/used in Example2Service.cs TownSuite Web API
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                json = "{\"Number1\": 6, \"Number2\": 8,\"Model\": {\"Message\": \"test\"}}";
                //Console.WriteLine("Code3() injects the below data into the request");
                //Console.WriteLine(json);
                //Console.WriteLine("the result is read in json format");
                streamWriter.Write(json);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream responseStream = response.GetResponseStream())
            {
                reader = new StreamReader(responseStream, Encoding.UTF8);
                jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(reader.ReadToEnd());
                jsonResponse = JsonConvert.DeserializeObject<String>(jsonResponse);
                //jobj = JObject.Parse(jsonResponse);

                System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(jsonResponse, "Root");

                //Console.WriteLine(jsonResponse);
                //Console.WriteLine("");
                //Console.WriteLine("Convert to xml");
                //Console.WriteLine("");
                //Console.WriteLine(node);

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return jsonResponse.ToString();
    }

    [HttpPost("Examlpe2")]
    public string PostTodoItem(ServiceStackExample.Example2 postInfo)
    {
        URL = @"https://localhost:7133/example/service/json/syncreply/Example2";
        request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";
        string jsonResponse = "";
        Newtonsoft.Json.Linq.JObject jobj = new Newtonsoft.Json.Linq.JObject();

        try
        {
            // inject json into the requset
            // the below attributes are assigned/used in Example2Service.cs TownSuite Web API
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                json = "{\"Number1\":" + postInfo.Number1 + ", \"Number2\":" + postInfo.Number2 + ",\"Model\": {\"Message\": \"" + postInfo.Model.Message + "\"}}";
                //Console.WriteLine("Code3() injects the below data into the request");
                //Console.WriteLine(json);
                //Console.WriteLine("the result is read in json format");
                streamWriter.Write(json);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream responseStream = response.GetResponseStream())
            {
                reader = new StreamReader(responseStream, Encoding.UTF8);
                jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(reader.ReadToEnd());
                jsonResponse = JsonConvert.DeserializeObject<String>(jsonResponse);
                //jobj = JObject.Parse(jsonResponse);

                System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(jsonResponse, "Root");

                //Console.WriteLine(jsonResponse);
                //Console.WriteLine("");
                //Console.WriteLine("Convert to xml");
                //Console.WriteLine("");
                //Console.WriteLine(node);

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return jsonResponse.ToString();
    }
}

}