網(wǎng)站首頁 編程語言 正文
話題
我們知道在.NET Framework中可以嵌入運(yùn)行Web APi,那么在.NET Core(.NET 6+稱之為.NET)中如何內(nèi)嵌運(yùn)行Web Api呢,在實(shí)際項(xiàng)目中這種場(chǎng)景非常常見,那么我們本節(jié)以.NET 6.0作為演示示例一起來瞅瞅
內(nèi)嵌運(yùn)行.NET Core Web APi
接下來我們通過控制臺(tái)作為主程序來啟動(dòng)Web APi,首先我們創(chuàng)建名為EmbedWebApi的控制臺(tái)程序,然后創(chuàng)建Embed.WebApi類庫運(yùn)行Web APi,我們?cè)诖薟eb APi中創(chuàng)建如下接口,并實(shí)現(xiàn)相關(guān)方法來運(yùn)行Web APi
public class InitTest : IInitTest
{
public void Init()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddControllers();
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
app.Run();
}
}
public interface IInitTest
{
void Init();
}
通過寫接口并在對(duì)應(yīng)方法中運(yùn)行Web APi主要是達(dá)到在控制中調(diào)用該接口進(jìn)行模擬實(shí)現(xiàn),這里需要注意一點(diǎn)的是,因?yàn)槲覀儎?chuàng)建的Web APi是類庫,要想使用Web里面的Api等等,直接在項(xiàng)目文件中添加如下一行以表明我們要引用框架,這樣一來框架里面所包含的APi等等版本都一致統(tǒng)一,而不是通過NuGet一一下載,這是錯(cuò)誤的做法
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
接下來我們?cè)谠擃悗熘邪凑找?guī)范創(chuàng)建Controllers文件夾,并創(chuàng)建測(cè)試控制器,如下
using Microsoft.AspNetCore.Mvc;
namespace Embed.WebApi.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class TestController : ControllerBase
{
[HttpGet]
public IActionResult Test()
{
return Ok("Hello World");
}
}
}
最后我們?cè)诳刂婆_(tái)程序中注冊(cè)上述接口并調(diào)用初始化方法,如下:
internal class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddTransient<IInitTest, InitTest>();
var serviceProvider = services.BuildServiceProvider();
var initTest = serviceProvider.GetRequiredService<IInitTest>();
initTest.Init();
Console.Read();
}
}
蕪湖,我們通過Postman模擬調(diào)用測(cè)試接口,結(jié)果驚呆了,404了~~~
當(dāng)我們將類庫中的控制器移動(dòng)到控制臺(tái)中,此時(shí)請(qǐng)求測(cè)試接口并成功返回對(duì)世界的問候,這是什么原因呢? 不難猜測(cè)可知,默認(rèn)WebAPi控制器的激活以作為入口的主程序集進(jìn)行查找激活。雖然這樣看似解決了問題,假設(shè)調(diào)用嵌入運(yùn)行的主程序是底層已經(jīng)封裝好的基礎(chǔ)設(shè)施,那么豈不是遭到了代碼入侵,所以我們就想在運(yùn)行的Web APi類庫里面去激活,此時(shí)我們想到將類庫作為Web APi應(yīng)用程序一部分應(yīng)用手動(dòng)加載并激活,在初始化方法里面修改為如下即可請(qǐng)求測(cè)試接口成功
public class InitTest : IInitTest
{
private static readonly string AssemblyName = typeof(InitTest).Assembly.GetName().Name;
public void Init()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddControllers()
.AddApplicationPart(Assembly.Load(new AssemblyName(AssemblyName)));
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
app.Run();
}
}
上述直接在運(yùn)行Web APi類庫中添加控制器激活,這種場(chǎng)景完全限定于底層主入口已封裝好,所以只能采用這種方式,若是主入口我們自己可控制,當(dāng)然還有另外一種方式,來,我們瞧瞧截取的關(guān)鍵性源碼
/// <summary>
/// Populates the given <paramref name="feature"/> using the list of
/// <see cref="IApplicationFeatureProvider{TFeature}"/>s configured on the
/// <see cref="ApplicationPartManager"/>.
/// </summary>
/// <typeparam name="TFeature">The type of the feature.</typeparam>
/// <param name="feature">The feature instance to populate.</param>
public void PopulateFeature<TFeature>(TFeature feature)
{
if (feature == null)
{
throw new ArgumentNullException(nameof(feature));
}
foreach (var provider in FeatureProviders.OfType<IApplicationFeatureProvider<TFeature>>())
{
provider.PopulateFeature(ApplicationParts, feature);
}
}
internal void PopulateDefaultParts(string entryAssemblyName)
{
var assemblies = GetApplicationPartAssemblies(entryAssemblyName);
var seenAssemblies = new HashSet<Assembly>();
foreach (var assembly in assemblies)
{
if (!seenAssemblies.Add(assembly))
{
// "assemblies" may contain duplicate values, but we want unique ApplicationPart instances.
// Note that we prefer using a HashSet over Distinct since the latter isn't
// guaranteed to preserve the original ordering.
continue;
}
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
foreach (var applicationPart in partFactory.GetApplicationParts(assembly))
{
ApplicationParts.Add(applicationPart);
}
}
}
private static IEnumerable<Assembly> GetApplicationPartAssemblies(string entryAssemblyName)
{
var entryAssembly = Assembly.Load(new AssemblyName(entryAssemblyName));
// Use ApplicationPartAttribute to get the closure of direct or transitive dependencies
// that reference MVC.
var assembliesFromAttributes = entryAssembly.GetCustomAttributes<ApplicationPartAttribute>()
.Select(name => Assembly.Load(name.AssemblyName))
.OrderBy(assembly => assembly.FullName, StringComparer.Ordinal)
.SelectMany(GetAssemblyClosure);
// The SDK will not include the entry assembly as an application part. We'll explicitly list it
// and have it appear before all other assemblies \ ApplicationParts.
return GetAssemblyClosure(entryAssembly)
.Concat(assembliesFromAttributes);
}
private static IEnumerable<Assembly> GetAssemblyClosure(Assembly assembly)
{
yield return assembly;
var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: false)
.OrderBy(assembly => assembly.FullName, StringComparer.Ordinal);
foreach (var relatedAssembly in relatedAssemblies)
{
yield return relatedAssembly;
}
}
從上述源碼可知,通過主入口程序集還會(huì)加載引用的程序集去查找并激活相關(guān)特性(比如控制器),當(dāng)然前提是實(shí)現(xiàn)ApplicationPartAttribute特性,此特性必須在主入口程序集里定義,定義在程序集上,所以我們只需一行代碼即可搞定,我們?cè)诳刂婆_(tái)主入口命名空間頂部添加特性,引入Web APi類庫程序集作為應(yīng)用程序的一部分,如下:
[assembly: ApplicationPart("Embed.WebApi")]
那么接下來問題又來了,要是需要運(yùn)行多個(gè)Web APi我們又當(dāng)如何呢?按照上述方式一一添加未嘗不可,我們也可以通過MSBuild任務(wù)來進(jìn)行構(gòu)建將相關(guān)特性自動(dòng)添加到主入口程序集描述信息里面去,例如:
<ItemGroup>
<AssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartAttribute">
<_Parameter1>Embed.WebApi</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
有的童鞋就問了,這不寫死了么,那還不如通過添加特性的方式去處理,請(qǐng)注意這里只是使用示例,實(shí)際情況下,我們可將多個(gè)Web APi放在同一解決方案下,然后在此解決方案下創(chuàng)建可構(gòu)建任務(wù)的.targets文件,并在主項(xiàng)目文件里引入,將程序集名稱作為變量引入,剩下事情自行統(tǒng)一處理,若不清楚怎么搞,就在代碼中使用特性方式也未嘗不可,例如如下:
<ItemGroup>
<AssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartAttribute">
<_Parameter1>$(AssemblyName)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
總結(jié)
本節(jié)我們重點(diǎn)討論如何內(nèi)嵌運(yùn)行.NET Core Web APi類庫,同時(shí)介紹了兩種激活比如控制器特性方案, 希望對(duì)您有所幫助,謝謝,我們下節(jié)再會(huì)
原文鏈接:https://www.cnblogs.com/CreateMyself/p/16721269.html
相關(guān)推薦
- 2022-11-26 React?數(shù)據(jù)獲取與性能優(yōu)化詳解_React
- 2022-06-16 golang?beego框架路由ORM增刪改查完整案例_Golang
- 2022-05-15 Qt windows打開資源管理器并高亮文件
- 2022-04-25 淺談Golang?Slice切片如何擴(kuò)容的實(shí)現(xiàn)_Golang
- 2022-07-13 數(shù)據(jù)結(jié)構(gòu)之冒泡排序
- 2023-12-09 springframework.jdbc.BadSqlGrammarException:
- 2022-02-28 獲取元素的寬度,高度
- 2022-07-23 詳解Nginx的超時(shí)keeplive_timeout配置步驟_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支