XIKEW.COM - 实用教程 - 基于LinkCoreX开发手册 - 实用教程,linkcorex, netcore, 框架, C# - LinkCoreX是以构件开发模式思想,代码低耦合开发高效率为目的的一个NETCORE开发框架。

基于LinkCoreX开发手册
LINKCORE NETCORE 框架 9/3/2024 5:36:56 PM 阅读:10

[[toc]]

LinkCoreX是以构件开发模式思想,代码低耦合开发高效率为目的的一个NETCORE开发框架。 关键字: linkcorex, netcore, 框架, C#

什么是LinkCoreX?

LinkCoreX :smile: 是一款用于开发RESTful接口跨平台的后台框架,只因希望能保护好自己的头发,于是创造了它!基于 NetCore 8.0 构建,相较官方 ASP.NET Core 的学习难度,LinkCoreX 主打一个轻松快乐,你将会有 PHP 脚本语言编码的错觉,无论在编写习惯还是编译测试的速度上。

支持 WebApi、WebSocket、WPF(Win桌面应用) 多种应用形态,让一套知识搞定更多应用场景。

而且它还自带了一些实用的功能封装,比如 ORMX (数据库操作)、JSONDocument(JSON操作) 、IHttp(非HttpClient封装)、 ITask(多线程)、OnTimer(计时器) 能大大增加 Plugin(插件) 对基础代码库的复用率。

值得一提的是对插件的 热编译、热加载 功能,让项目的迭代、维护变得更便捷和更安全。要得到这一切你不需要费劲的部署 K8S,也不需要停机重启让应用生效,只要你设计的足够合理,你可以让一年前开发的功能跟刚出炉的功能共存投产。哪怕无法避免的需要重启服务,你也无需担心,一切是那么高效方便。

关于稳定性

多年来写过一些(PHP、GoLang)框架,但 NetCore3.0 底层框架的一些问题让一个商业项目发生了一场事故,所以一度决定放弃这个框架。后来离开了那个公司,出于不甘心陆续的关注着NetCore的版本更新。直到 6.0 版本测试通过后,我又重拾了 LinkCoreX。从 2020年3.06.0 再到现在的 8.0LinkCoreX 已经在几个项目落地并验证了稳定性。

关于性能

并不是追求性能而去做的项目不过也简单测了一下,200并发 ORMX 插入数据用的模型是 User 耗时 4.4 秒 |数据|时间| |-|-| |第一|18:46:45.818149| |最后|18:46:50.192050|

测试并不科学,请不要较真!有空再说吧~

创建项目

环境介绍

根目录是开发环境,Release目录是发布版本

::: tip 注意事项 之后相关示例对应的目录如下,其中WebAppNet8为WebApp的目录,如有区别请自行调整 :::

Projects
|- Release
   |- WebAppNet8
      |- Plugin
         |- DemoPlugin(编译生成的dll文件)
      |- ...
|- DemoPlugin(插件项目目录)
   |- ...
  • 下载完成后启动 WebApp.exe ::: tip app.config.js 的配置说明请见 链接 :::

Nuget创建 (推荐)

安装 LinkCoreX.Templates

dotnet new install LinkCoreX.Templates::1.0.2

查看下帮助 dotnet new LinkCoreX -h

dotnet new LinkCoreX -h

LinkCoreX 基于构件开发模式,支持热加载
作者: 知时

用法:
  dotnet new LinkCoreX [options] [模板选项]

选项:
  -n, --name <name>      正在创建的输出名称。如未指定名称,则使用输出目录的名称。
  -o, --output <output>  要放置生成的输出的位置。
  --dry-run              如果运行给定命令行将导致模板创建,则显示将发生情况的摘要。
  --force                强制生成内容 (即使它会更改现有文件)。
  --no-update-check      在实例化模板时,禁用对模板包更新的检查。
  --project <project>    应用于上下文评估的项目。

模板选项:
  -na, --param:name <param:name>  类型: string
                                  默认: MyPlugin
  -m, --mode <webapi|websocket>   类型: choice
                                    webapi     创建一个WebApi项目
                                    websocket  创建一个WebSocket项目
                                  默认: webapi
  -ap, --appPath <appPath>        类型: string
                                  默认: ..\Release\WebAppNet8

::: tip -na 参数就是插件项目名称 -m 创建 webapiwebsocket -ap 主程序的目录 :::

尝试着来创建一个 DemoPlugin 项目

mkdir DemoPlugin
cd DemoPlugin
dotnet new LinkCoreX -na DemoPlugin

搞定!来愉快的玩耍吧~ :smiley:

手动创建 WebAPI

  1. 使用 Visual Studio 在 Projects 目录下创建一个跨平台的 NetCore 8.0 控制台应用
  2. 编辑项目 .csproj 文件
(((折叠==折叠)))<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
	<Reference Include="LinkCore.Interface">
		<HintPath>..\\\\Release\\\\WebAppNet8\\\\LinkCore.Interface.dll</HintPath>
	</Reference>
	<Reference Include="LinkCore.Interface.ORMX">
		<HintPath>..\\\\Release\\\\WebAppNet8\\\\LinkCore.Interface.ORMX.dll</HintPath>
	</Reference>
	<Reference Include="LinkCore.Interface.Router">
		<HintPath>..\\\\Release\\\\WebAppNet8\\\\LinkCore.Interface.Router.dll</HintPath>
	</Reference>
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
	<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
	<OutputPath>..\\\\Release\\\\WebAppNet8\\\\Plugins\\\\$(MSBuildProjectName)\\\\</OutputPath>
	<NoWarn>1701;1702;1591;0649</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
	<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
	<OutputPath>..\\\\Release\\\\WebAppNet8\\\\Plugins\\\\$(MSBuildProjectName)\\\\</OutputPath>
	<NoWarn>1701;1702;1591;0649</NoWarn>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
	<Exec Command="$(OutDir)..\\\\..\\\\DebugBuilder.exe $(MSBuildProjectName)" />
</Target>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
	<Exec Command="$(OutDir)..\\\\..\\\\DebugBuilder.exe $(MSBuildProjectName) STOP" />
</Target>
  1. 创建一个Runner.cs类
using LinkCore.Interface;
using LinkCore.Interface.Router;

namespace DemoPlugin
{
  internal class Runner : IPlugin.IRunner
  {
    public void Dispose()
    {
    }

    public void OnStart()
    {
        // 如果传'Hello'参数的前后加'/'符号会被自动过滤
        Builder.AddRouter('Hello', Hello);
    }

    // 也支持非静态方法
    static public IActionResult Hello(HttpListenerRequest request) 
    {
        return new StringActionResult() { Data = "Hello LinkCoreX!!"};
    }
  }
}

  1. Ctrl + B 快捷键编译,提示DemoPlugin插件启动成功
  2. 通过浏览器访问 http://localhost:8088/DemoPlugin/Hello 浏览器显示 Hello LinkCoreX!

::: tip 插件被加载后内部的一个生命周期

flowchart LR
    OnStart --> 接口处理 -- 插件卸载 --> Dispose

:::

创建 WebSocket

  1. 按照WebAPI的步骤进行到第3步
  2. 创建一个WSDemo.cs文件
using LinkCore.Interface.Router;
using System;

namespace DemoPlugin
{
    class WSDemo : WebSocketAbstractController
    {
        public override IWSActionResult OnClosed()
        {
            Console.WriteLine("WebSocket 已经断开");
            return null;
        }

        public override IWSActionResult OnConnect()
        {
            Console.WriteLine("发现用户接入");
            this.SendStringAsync("恭喜你接入成功!");
            return null;
        }

        public override IWSActionResult OnReceiveStringMessage(string message)
        {
            Console.WriteLine("收到用户发来的消息");
            Console.WriteLine(message);
            this.SendStringAsync("刚刚收到了您发来的消息:" + message);
            return null;
        }
    }
}
  1. Ctrl + B 快捷键编译,提示DemoPlugin插件启动成功
  2. 使用 WebSocket客户端 或 在线测试工具 地址 ws://localhost:8088/DemoPlugin/Index/

创建 WPFApp

::: tip 文档书写正在进行中... :::

补充

::: tip 接下来的教程中会出现普通和高阶两种定级内容,高阶内容可以选择性跳过 :::