新增按条件选择使用哪种Web框架

This commit is contained in:
2025-09-28 15:01:44 +08:00
parent 13b838c7c7
commit d22b35ce66
8 changed files with 207 additions and 71 deletions

View File

@@ -1,4 +1,6 @@
using HandyControl.Collections; 
using HandyControl.Collections;
using System; using System;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;

View File

@@ -1,4 +1,4 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<PropertyChanged /> <PropertyChanged />
<Costura /> <Costura ExcludeAssemblies="Nancy"/>
</Weavers> </Weavers>

View File

@@ -1,4 +1,5 @@
using System; #if !Nancy
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -34,3 +35,4 @@ namespace 电子展板.Utility.Web
} }
} }
#endif

View File

@@ -1,4 +1,5 @@
using Nancy; #if Nancy
using Nancy;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Configuration; using Nancy.Configuration;
using Nancy.Conventions; using Nancy.Conventions;
@@ -51,5 +52,5 @@ namespace 电子展板.WebModule
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*")); pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*"));
} }
} }
} }
#endif

View File

@@ -1,4 +1,5 @@
using Nancy; #if Nancy
using Nancy;
using Nancy.ModelBinding; using Nancy.ModelBinding;
using Nancy.Swagger; using Nancy.Swagger;
using Nancy.Swagger.Annotations.Attributes; using Nancy.Swagger.Annotations.Attributes;
@@ -6,15 +7,11 @@ using Swagger.ObjectModel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Pipes;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using .Base; using .Base;
using .Models; using .Models;
using .Utility; using .Utility;
using .Utility.Core;
using .Utility.Extension;
using .Utility.Logs;
using .Utility.Other; using .Utility.Other;
namespace .WebModule namespace .WebModule
@@ -120,11 +117,11 @@ namespace 电子展板.WebModule
[Route(Tags = new[] { "上传图片" })] [Route(Tags = new[] { "上传图片" })]
private object UploadImage([RouteParam(ParamIn = ParameterIn.Form, Name = "file", Description = "图片文件", ParamType = typeof(SwaggerFile), Required = true)] HttpFile file) private object UploadImage([RouteParam(ParamIn = ParameterIn.Form, Name = "file", Description = "图片文件", ParamType = typeof(SwaggerFile), Required = true)] HttpFile file)
{ {
string uploadPath = MyEnvironment.Root("/Upload/"); //string uploadPath = MyEnvironment.Root("/Upload/");
if (!Directory.Exists(uploadPath)) //if (!Directory.Exists(uploadPath))
{ //{
Directory.CreateDirectory(uploadPath); // Directory.CreateDirectory(uploadPath);
} //}
string ext = Path.GetExtension(file.Name); string ext = Path.GetExtension(file.Name);
string fileName = UUID.StrSnowId + ext; string fileName = UUID.StrSnowId + ext;
@@ -178,3 +175,4 @@ namespace 电子展板.WebModule
} }
} }
} }
#endif

View File

@@ -1,4 +1,5 @@
using System; #if !Nancy
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Globalization; using System.Globalization;
@@ -180,3 +181,4 @@ namespace 电子展板.WebModule
} }
} }
} }
#endif

View File

@@ -1,56 +1,62 @@
using Microsoft.Owin; using System;
using Microsoft.Owin.Hosting;
using Nancy.Hosting.Self;
using Owin;
using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
#if Nancy
using Nancy.Hosting.Self;
#else
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
using System.Web.Http; using System.Web.Http;
using static System.Net.Mime.MediaTypeNames; using System.Net.Http.Formatting;
#endif
namespace namespace
{ {
public class WebServer public class WebServer
{ {
private static bool UseNancy = false;
#if Nancy
private static NancyHost _host; private static NancyHost _host;
#else
private static IDisposable host; private static IDisposable host;
#endif
public static void Start() public static void Start()
{ {
if (UseNancy) #if Nancy
{ _host = new NancyHost(new Uri("http://localhost:80"));
_host = new NancyHost(new Uri("http://localhost:80")); _host.Start();
_host.Start(); #else
} StartOptions startOptions = new StartOptions();
else startOptions.Urls.Add($"http://*:80/");
{ host = WebApp.Start<Startup>(startOptions);
StartOptions startOptions = new StartOptions(); #endif
startOptions.Urls.Add($"http://*:80/");
host = WebApp.Start<Startup>(startOptions);
}
} }
public static void Stop() public static void Stop()
{ {
#if Nancy
if (_host != null) if (_host != null)
{ {
_host.Stop(); _host.Stop();
_host.Dispose(); _host.Dispose();
_host = null; _host = null;
} }
if (host != null) #else
{ if (host != null)
host.Dispose(); {
host = null; host.Dispose();
} host = null;
}
#endif
} }
} }
#if !Nancy
/// <summary> /// <summary>
/// Web启动类 /// Web启动类
/// </summary> /// </summary>
@@ -117,4 +123,109 @@ namespace 电子展板
} }
} }
} }
#endif
} }
#if !Nancy
namespace Nancy.Swagger.Annotations.Attributes
{
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class ModelAttribute : Attribute
{
public ModelAttribute(string description)
{
Description = description;
}
public string Description { get; set; }
/// <summary>
/// By default, only read/write props are shown, this
/// prop allows read only props to be shown.
/// </summary>
public bool ShowReadOnlyProps { get; set; }
}
public abstract class SwaggerDataTypeAttribute : Attribute
{
private long? _maximum;
private long? _minium;
private bool? _required;
private bool? _uniqueItems;
protected SwaggerDataTypeAttribute(string name)
{
Name = name;
}
public string Description { get; set; }
public string DefaultValue { get; set; }
public string[] Enum { get; set; }
public long Maximum
{
get { return _maximum.GetValueOrDefault(); }
set { _maximum = value; }
}
public long Minimum
{
get { return _minium.GetValueOrDefault(); }
set { _minium = value; }
}
public string Name { get; set; }
public bool Required
{
get { return _required.GetValueOrDefault(); }
set { _required = value; }
}
public bool UniqueItems
{
get { return _uniqueItems.GetValueOrDefault(); }
set { _uniqueItems = value; }
}
internal long? GetNullableMaximum()
{
return _maximum;
}
internal long? GetNullableMinimum()
{
return _minium;
}
internal bool? GetNullableRequired()
{
return _required;
}
internal bool? GetNullableUniqueItems()
{
return _uniqueItems;
}
}
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public class ModelPropertyAttribute : SwaggerDataTypeAttribute
{
public ModelPropertyAttribute() : this(null)
{
}
public ModelPropertyAttribute(string name) : base(name)
{
}
/// <summary>
/// Ignore this property when generating swagger model.
/// </summary>
public bool Ignore { get; set; }
}
}
#endif

View File

@@ -8,6 +8,10 @@
<ApplicationIcon>favicon.ico</ApplicationIcon> <ApplicationIcon>favicon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);Nancy</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Include="favicon.ico" /> <Content Include="favicon.ico" />
<Resource Include="Assets\**" /> <Resource Include="Assets\**" />
@@ -17,37 +21,53 @@
<Content Include="..\Configs\Config.json" Link="%(Filename)%(Extension)"> <Content Include="..\Configs\Config.json" Link="%(Filename)%(Extension)">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<Choose>
<!-- 条件加载 如果宏定义了Nancy就使用Nancy的库-->
<When Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(DefineConstants),'^(.*;)*Nancy(;.*)*$'))">
<ItemGroup>
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
<PackageReference Include="ZJW.NancySwagger" Version="2.0.1" />
</ItemGroup>
</When>
<!-- 否则就使用Microsoft的Owin -->
<Otherwise>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.9" />
<PackageReference Include="Microsoft.AspNet.WebApi.Owin" version="5.3.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.3.0" />
<PackageReference Include="Microsoft.Owin" Version="4.2.3" />
<PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNet.Identity.Owin" Version="2.2.4" />
<PackageReference Include="Swashbuckle.Core" version="5.6.0" />
<PackageReference Include="Beginor.Owin.StaticFile" Version="0.3.1" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup> <ItemGroup>
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
</ItemGroup> <!-- JSON框架 -->
<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<!-- 界面UI-->
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
<!-- 生成单文件 -->
<PackageReference Include="Costura.Fody" Version="6.0.0"> <PackageReference Include="Costura.Fody" Version="6.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<!-- JSON框架 -->
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<!-- 日志框架 -->
<!--<PackageReference Include="NLog" Version="6.0.2" />-->
<!-- WEB 自宿主 -->
<PackageReference Include="ZJW.NancySwagger" Version="2.0.1" />
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
<!-- 界面UI-->
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
<!-- WEB 自宿主 -->
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.9" />
<PackageReference Include="Microsoft.AspNet.WebApi.Owin" version="5.3.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.3.0" />
<PackageReference Include="Microsoft.Owin" Version="4.2.3" />
<PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNet.Identity.Owin" Version="2.2.4" />
<PackageReference Include="Swashbuckle.Core" version="5.6.0" />
<PackageReference Include="Beginor.Owin.StaticFile" Version="0.3.1" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>