65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Thriftly.IO 将遗留 C# 代码迁移到云端

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2018年11月14日

CPOL

6分钟阅读

viewsIcon

11887

Thriftly.IO 允许组织利用现有的代码和基础设施资产来定义新的云原生 API。

将遗留应用程序现代化并转向面向服务的架构是一个漫长的过程。对许多人来说,新式现代系统所需的逻辑已经以代码的形式存在,这些代码已经被编写、完全测试并已经以厚客户端 Windows 应用程序的形式使用了多年。从头开始,定义云 API 并重新实现逻辑和数据层可能会耗时且容易出错。

Thriftly.IO 提供了一条更安全的现代化现有业务逻辑的替代路线,它提供了一项服务,可以原样利用您现有的业务逻辑代码和数据层,并将其公开为可重用的云原生 Web 服务。这使得公司能够利用他们已拥有的技术资产,同时仍然对其应用程序进行现代化。这种重用避免了在现代化过程中给您的组织增加技术债务。

让我们以遗留 C# 代码为例,看看这一切是如何工作的。

情况:Factorant 的困境

Factorant 是一家虚构的工厂设备制造商,已经营了 40 多年。在此期间,公司经历了多次技术演变。他们目前正经历又一次技术颠覆——适应和改变与将其设备连接到物联网 (IoT) 相关的工作。

Factorant 有许多遗留系统,包括一个帮助现场技术人员根据书面设备日志中的数据估算设备总体效率的工具。最初,该工具被打包成一个 C#/WPF 的加固笔记本电脑应用程序,公司现在正开始将其员工转移到运行在智能手机上的移动应用程序。新的云后端系统还需要计算设备总体效率的计算,以便根据其新连接的启用 IoT 的设备收集的 IoT 遥测数据计算实际值。由于这些计算需要多个客户端(即移动应用程序和后端云系统)的访问,因此利用 Windows WPF 应用程序中已创建的代码并将其集中化,将其公开为可重用的 Web 服务是有意义的。

在本文中,我们将重点关注如何从 WPF 应用程序中提取现有逻辑,将其公开为 RESTful Web 服务,供云后端系统和新的移动应用程序使用。

入门步骤 - 设置您的帐户、软件和许可证

要利用 Thriftly.IO 服务,您必须首先注册一个帐户。注册过程非常简单,只需要您的电子邮件地址。您将收到一封电子邮件,要求您通过选择密码并填写简短的基本个人资料表来完成注册过程。

现在您的帐户已创建,您可以登录系统并查看您的仪表板。

单击仪表板主页上的“构建您的应用程序”图标,然后单击中间的下载图标以下载并安装最新版本的 Thriftly Developer for Windows。如果尚未安装,此安装还将要求安装 SQL Server Express 2014 LocalDB。

成功安装软件后,就可以获取开发者许可证了——试用许可证对我们来说就足够了。单击最右边的“获取试用许可证”图标即可获取试用许可证,系统将为您生成一个。

从仪表板菜单中,选择“开发许可证”——现在您将看到分配给您的注册代码。单击注册代码,磁贴将翻转,然后允许您将代码复制到剪贴板。

将代码复制到剪贴板后,从 Windows 开始菜单中打开 Thriftly Developer Assistant 应用程序。单击“输入注册码”按钮,然后粘贴剪贴板中的内容。您的试用许可证现在将激活,您可以关闭 Thriftly Developer Assistant 应用程序。

业务逻辑代码概述

OEE 计算器应用程序有许多与 UI 表示和用户交互相关的代码。在为 Thriftly 服务提取业务逻辑时,我们只对这部分代码感兴趣。让我们看一下此特定应用程序中嵌入的业务逻辑代码。

业务逻辑类

using Factorant.OEE.WPF.Models;

namespace Factorant.OEE.WPF.BL
{
    public class OeeCalculator
    {

        public OeeCalcModelOut CalculateOee(OeeCalcModelIn inbound)
        {
            OeeCalcModelOut returnValue = new OeeCalcModelOut();
            returnValue.PlannedProductionTime = _calculatePlannedProductionTime(inbound.ShiftLength, inbound.Breaks);
            returnValue.RunTime = _calculateRunTime(returnValue.PlannedProductionTime, inbound.StopTime);
            returnValue.GoodCount = _calculateGoodCount(inbound.RejectCount, inbound.TotalCount);
            returnValue.Availability = _calculateAvailability(returnValue.RunTime, returnValue.PlannedProductionTime);
            returnValue.Performance = _calculatePerformance(inbound.IdealCycleTime, inbound.TotalCount, returnValue.RunTime);
            returnValue.Quality = _calculateQuality(returnValue.GoodCount, inbound.TotalCount);
            returnValue.Oee = _calculateOee(returnValue.Availability, returnValue.Performance, returnValue.Quality);
            return returnValue;
        }

        private decimal _calculatePlannedProductionTime(decimal shiftLength, decimal breaks)
        {
            return shiftLength - breaks;
        }

        private decimal _calculateRunTime(decimal plannedProductionTime, decimal stopTime)
        {
            return plannedProductionTime - stopTime;
        }

        private decimal _calculateGoodCount(decimal rejectCount, decimal totalCount)
        {
            return totalCount - rejectCount;
        }

        private decimal _calculateAvailability(decimal runTime, decimal plannedProductionTime)
        {
            if (plannedProductionTime == 0)
                return 0;
            return runTime / plannedProductionTime;
        }

        private decimal _calculatePerformance(decimal idealCycleTime, decimal totalCount, decimal runTime)
        {
            if (runTime == 0)
                return 0;
            return (idealCycleTime * totalCount) / (runTime * 60);
        }

        private decimal _calculateQuality(decimal goodCount, decimal totalCount)
        {
            if (totalCount == 0)
                return 0;
            return goodCount / totalCount;
        }

        private decimal _calculateOee(decimal availability, decimal performance, decimal quality)
        {
            return availability * performance * quality;
        }


    }
}

入站模型

namespace Factorant.OEE.WPF.Models
{
    public struct OeeCalcModelIn
    {
        public decimal ShiftLength;
        public decimal Breaks;
        public decimal StopTime;
        public decimal IdealCycleTime;
        public decimal TotalCount;
        public decimal RejectCount;
    }
}

出站模型

namespace Factorant.OEE.WPF.Models
{
    public struct OeeCalcModelOut
    {
        public decimal PlannedProductionTime;
        public decimal RunTime;
        public decimal GoodCount;
        public decimal Availability;
        public decimal Performance;
        public decimal Quality;
        public decimal Oee;
    }
}

将 Thriftly 引入 WPF 应用程序

提取服务逻辑的第一步是将 Thriftly 服务器引入源代码。打开 *App.xaml.cs* 并通过重写应用程序的 OnStartup 方法来引入 Thriftly 服务器,如下所示:

using System.Windows;
using Thriftly.Server;

namespace Factorant.OEE.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private ThriftlyServer _server;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            _server = new ThriftlyServer();
	//instantiate the business logic class that contains the method, expose it as OeeCalculatorService
            _server.AddService(new BL.OeeCalculator(), "OeeCalculatorService");
            _server.StartServer();
        }
    }
}

接下来,我们将打开包含业务逻辑的类——在此示例中是 *BL\OeeCalculator.cs*,并添加 using 语句 ‘Thriftly.Server’,以及在您希望通过服务公开的方法上添加 ‘PublishedAttribute’ 属性。仅此而已,我们无需进行其他修改!

在本地测试 Thriftly 服务

启动应用程序,等待片刻让 Thriftly 服务器启动并显示类似以下的窗口:

在此窗口中,您可以设置要公开服务的传输方式、协议、编码和端口。目前,让我们仅将协议设置为 JSON-REST,然后单击“启动 Thriftly”按钮。浏览器将打开,允许您向 JSON 请求添加值以发送到服务。确保将服务名称附加到 URL,然后按“发送”以启动对服务的请求并查看返回的数据。现在您可以关闭此浏览器窗口并停止 Thriftly 服务器(但请保持应用程序运行,以进行下一步操作!)。

将 Thriftly 服务迁移到云端

将您的服务公开到云端也很简单。只需返回 Thriftly Developer 窗口,然后勾选“启用网关”复选框。然后选择离您地理位置最近的网关区域。最后,再次单击“启动 Thriftly”按钮,即可通过原生的云服务公开您的本地逻辑!

将显示一个浏览器窗口,其中显示了您可以与之交互的端点和服务。

将 OeeCalculatorService 的位置 URL 复制到剪贴板。然后,您可以使用 Postman 等工具来创建对该服务的请求。或者,您也可以单击与该服务关联的“测试 API”链接,并在浏览器内进行测试。

结论

Thriftly.IO 允许组织利用现有的代码和基础设施资产来定义新的云原生 API。这些 API 可通过多种传输选项和协议提供,您可以从中选择。Thriftly.IO 还实现了 JWT 安全(如果需要),从而为在云中公开业务逻辑提供了全面的解决方案。

© . All rights reserved.