【发布时间】:2020-01-25 00:18:55
【问题描述】:
我正在尝试调查迁移到 dot net core 的合理性,现在 3.0 已经发布。我们的关键组件之一允许我们的(私有)nugets 创建自己的 WebAPI,为消费者提供事件和方法。支持远程服务控制、远程服务配置等功能,允许api提供远程配置设置/检索等功能。
此功能是我们微服务架构当前工作方式的关键。
我正在尝试使用 dotnet core 复制它,但是,我正在努力寻找直接等效的教程/场景。我们基本上遵循了这里详述的过程:
但是,在检查了 nuget 包的兼容性(一切看起来都还不错..)之后,我现在在调用 WebApp.Start<Startup>(baseaddress); 时得到空引用异常
null 引用异常显然是由 nuget 包与 .net 核心的不兼容调用的,请参见此处:
NullReferenceException experienced with Owin on Startup .Net Core 2.0 - Settings?
链接中提供的解决方案是一种方式,但它使用了第三方应用程序 - NancyFx。有没有办法以当前形式实现与 dotnet core 相同的功能?之前有大量关于自托管的文档,但不幸的是,由于 aspnet 核心在自己的进程中运行,因此很难找到解决方案!
任何人都可以在这里指出正确的方向吗?
代码如下所示
//the external library would contain all this code. I.e. this could present the configuration endpoints as mentioned above.
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}
public class WebAPI:IDisposable
{
private IDisposable _webApp;
public WebAPI()
{
string baseAddress = "http://localhost:8800/";
_webApp = WebApp.Start<Startup>(baseAddress); // << This line throws null reference exception
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_webApp.Dispose();
_webApp = null;
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
public class ValuesController:ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
主应用,上面库的宿主/消费者。
class Program
{
static void Main()
{
var webapi = new WebApiTest.WebAPI();
Console.WriteLine("Running...");
Console.ReadLine();
webapi.Dispose();
}
}
【问题讨论】:
-
你看过
dotnet new webapi提供的模板了吗?这默认情况下自托管,但也可以部署到 IIS。 -
谢谢一个好主意,我会看看他们是怎么做的!谢谢..
-
@ChrisWatts 我知道这是一个老问题 - 但你有没有想过这个问题?我们正在将 .NET Framework 项目移植到 .NET 5,我也遇到了这个空引用问题。 NancyFX 项目已停止,如果可以的话,我宁愿避免它。
-
@tmwoods,见下文.. 应该是您需要的唯一一点.. 它可以帮助您入门,也可以解决您的问题..
标签: c# .net-core owin self-hosting