Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在.NET C#WebAPI程序上取消初始化库_C#_.net_Asp.net Web Api - Fatal编程技术网

如何在.NET C#WebAPI程序上取消初始化库

如何在.NET C#WebAPI程序上取消初始化库,c#,.net,asp.net-web-api,C#,.net,Asp.net Web Api,我有一个.NETC#WebAPI程序,我正在使用一个外部库,我需要在主应用程序退出之前处理它 这是我的启动功能: public class Startup { private static void WebApiSetup(IAppBuilder app) { using (var configuration = new HttpConfiguration()) { app.UseWe

我有一个.NETC#WebAPI程序,我正在使用一个外部库,我需要在主应用程序退出之前处理它

这是我的
启动
功能:

public class Startup
{
        private static void WebApiSetup(IAppBuilder app)
        {
            using (var configuration = new HttpConfiguration())
            {
                app.UseWebApi(configuration);
            }
        }
}

外部库是一个静态类,因此我只需调用
MyLib.DeInit()。不幸的是,我没有看到任何合适的地方来调用该函数,没有“我们退出”函数可自定义。有人说要注册一个“取消令牌”的回调,但我不知道怎么做

你可以像这样在“关机”上运行代码:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var context = new OwinContext(app.Properties);
        var token = context.Get<CancellationToken>("host.OnAppDisposing");
        if (token != CancellationToken.None)
        {
            token.Register(() =>
            {
                // code to run at shutdown
            });
        }
    }
}
公共类启动
{
公共无效配置(IAppBuilder应用程序)
{
var context=新文本(app.Properties);
var token=context.Get(“host.OnAppDisposing”);
if(令牌!=CancellationToken.None)
{
令牌.寄存器(()=>
{
//关闭时运行的代码
});
}
}
}
属性是IAppBuilder的基本Owin接口的一部分:

// Decompiled with JetBrains decompiler
// Type: Owin.IAppBuilder
// Assembly: Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5
// MVID: C152461C-65C1-4F51-912C-2454A21D9BAD
// Assembly location: E:\Felinesoft\BACP\DEV\WEB\BACP.Web-AzureSearch\BACP.Web.Presentation\Bin\Owin.dll

using System;
using System.Collections.Generic;

namespace Owin
{
  public interface IAppBuilder
  {
    IDictionary<string, object> Properties { get; }

    IAppBuilder Use(object middleware, params object[] args);

    object Build(Type returnType);

    IAppBuilder New();
  }
}
//使用JetBrains反编译器反编译
//类型:Owin.IAppBuilder
//程序集:Owin,版本=1.0.0.0,区域性=中性,PublicKeyToken=f0ebd12fd5e55cc5
//MVID:C152461C-65C1-4F51-912C-2454A21D9BAD
//程序集位置:E:\Felinesoft\BACP\DEV\WEB\BACP.WEB AzureSearch\BACP.WEB.Presentation\Bin\Owin.dll
使用制度;
使用System.Collections.Generic;
名称空间Owin
{
公共接口IAppBuilder
{
IDictionary属性{get;}
IAppBuilder使用(对象中间件,参数对象[]参数);
对象构建(返回类型);
IAppBuilder New();
}
}

我的iApp Buildery上没有可用的app.Properties是的,为什么没有.Properties字段?好问题,Owin的哪个版本?你有所有的Microsoft.Owin库吗?我刚刚检查了你的编辑,可能是我的intellisense出错了。。。我忽略了“intellisense错误”,它编译得很好,所以我认为我们刚刚解决了这个问题。。thanks@spender我的IAppBuilder上没有可用的app.Properties