本文介绍了无法配置"IApplicationBuilder UseOwin"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如官方文档中所述,我正在尝试在Startup.cs.中实现UseOwin,我试图在IApplicationBuilder (Microsoft.AspNetCore.Builder.IApplicationBuilder)中使用/端口IAppBuilder (Microsoft.Owin.Builder.AppBuilder).我有使用IAppBuilder编写的旧代码,在.Net Framework 4.5上运行良好.

As stated in official document, I am trying to implement UseOwin in the Startup.cs.I am trying to use/port IAppBuilder (Microsoft.Owin.Builder.AppBuilder) inside IApplicationBuilder (Microsoft.AspNetCore.Builder.IApplicationBuilder). I had legacy code written using IAppBuilder running fine on .Net Framework 4.5.

我已经看到了几个有关在IAplicationBuilder中使用IAppBuilder的示例,例如示例1 示例2 .这些尝试是关于.netcore 1.1,而不是.net core 2.0.可能这就是我无法移植的原因.

I have seen couple of examples about using IAppBuilder in IAplicationBuilder e.g. example 1 example 2. These attempts were about .netcore 1.1 and not .net core 2.0. May be this is the reason i am unable to port.

请分享您的想法,无论我是试图实现目前无法在.net core 2.0中实现的目标,还是我的代码中存在一些错误.

Please share your thoughts whether i am trying to achieve something not possible at the moment in .net core 2.0 or there is some error in my code.

注意:我正在将dotnetcore 2.0Visual Studio 2017

错误

我遇到以下错误.

我的尝试

    app.UseOwin(setup => setup(next =>
    {
        var owinAppBuilder = new AppBuilder();

        var aspNetCoreLifetime =
            (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));

        new AppProperties(owinAppBuilder.Properties)
        {
            OnAppDisposing = aspNetCoreLifetime?.ApplicationStopping ?? CancellationToken.None,
            DefaultApp = next,
            AppName = "test"
        };

        // Only required if CORS is used, configure it as you wish
        var corsPolicy = new System.Web.Cors.CorsPolicy
        {

            AllowAnyHeader = true,
            AllowAnyMethod = true,
            AllowAnyOrigin = true,
            SupportsCredentials = true
        };

        //corsPolicy.GetType()
        //      .GetProperty(nameof(corsPolicy.ExposedHeaders))
        //      .SetValue(corsPolicy, tusdotnet.Helpers.CorsHelper.GetExposedHeaders());

        owinAppBuilder.UseCors(new Microsoft.Owin.Cors.CorsOptions
        {
            PolicyProvider = new CorsPolicyProvider
            {
                PolicyResolver = context => Task.FromResult(corsPolicy)
            }
        });

        PublicClientId = "self";

        OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new Microsoft.Owin.PathString("/Login"),
            Provider = new MyServiceProvider(PublicClientId),

            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
            AllowInsecureHttp = true,
            RefreshTokenProvider = new MyRefreshTokenProvider(),
        };

        owinAppBuilder.UseOAuthBearerTokens(OAuthOptions);
        //owinAppBuilder.UseTus(context => new DefaultTusConfiguration
        //{
        //    // Excluded for brevity, use the same configuration as you would normally do
        //});


        return owinAppBuilder.Build<Func<IDictionary<string, object>, Task>>();
    }));

推荐答案

Microsoft.Owin和相关程序包没有.NET Core的目标,没有.NET Standard的目标.他们所拥有的只是针对完整.NET的dll.您可以从针对.NET Core的项目中引用此类库,但正如您所看到的那样,它们不能保证正常运行,因为完整.NET和.NET Core的API(类集,方法\签名)不同.这样做时,Visual Studio甚至会显示警告,例如:

Microsoft.Owin and related packages do not have targets for .NET Core, no for .NET Standard. All they have is dlls targeting full .NET. You can reference such libraries from your project targeting .NET Core, but they are not guaranteed to work, as you see yourself, because API (set of classes\methods\signatures) of full .NET and .NET Core are different. Visual Studio even will show a warning when you are doing that, for example:

Microsoft.AspNetCore.Owin软件包,您可以按第一个链接的描述在.NET Core应用程序中使用OWIN中间件,但几乎提供的只是UseOwin扩展方法.那里没有AppBuilder类型,依此类推,也没有Microsoft.AspNetCore.Owin.Cors软件包或类似软件包.因此,您必须自己实现所有功能(没有理由,因为您可以使用asp.net核心框架提供的相同功能),或者等待面向.NET Standard \ Core的OWIN程序包执行此操作(无法检查,也许它们甚至已经存在).

There is Microsoft.AspNetCore.Owin package and you can use OWIN middleware in .NET Core app as your first link describes, but almost all it provides is UseOwin extension method. There is no AppBuilder type there and so on, and there are no Microsoft.AspNetCore.Owin.Cors packages or similar. So you have to either implement all that yourself (no reason to, because you can use the same functionality provided by asp.net core framework) or wait for OWIN packages that target .NET Standard\Core and do that (didn't check, maybe they even exist already).

因此,您的代码使用的软件包确实与目标框架不兼容,这是运行时显示的异常.因此,另一个答案(出于某种原因被否决)在技术上是正确的.

So, your code uses packages which are indeed not compatible with your target framework, as exception you have at runtime shows. So another answer (for some reason downvoted) is technically correct.

如果您仍然想可靠地使用这些软件包-您需要定位完整的.NET Framework,而不是.NET Core.为此,请打开您的.csproj文件并进行更改

If you still want to use those packages reliably - you need to target full .NET Framework and not .NET Core. To do that, open your .csproj file and change

<TargetFramework>netcoreapp2.0</TargetFramework>

对于某些支持.NET Standard 2.0的.NET框架版本,例如:

To some .NET framework version that supports .NET Standard 2.0, for example:

<TargetFramework>net47</TargetFramework>

然后转到nuget软件包管理器,如果您有microsoft.aspnetcore.all软件包(或其他针对.NET Core的软件包)-请将其卸载,则无论如何都不需要它.然后安装Microsoft.AspNetCore软件包和所需的所有其他asp.net核心软件包(如果尚未安装).重建,运行,它将正常运行.

Then go to nuget package manager and, if you have microsoft.aspnetcore.all package (or other packages targeting .NET Core) - uninstall it, you don't need it anyway. Then install Microsoft.AspNetCore package and all other asp.net core packages you need (if not installed already). Rebuild, run and it will work just fine.

之所以可行,是因为所有(大多数?)AspNetCore软件包都针对.NET Standard,而不是.NET Core,并且您可以在针对完整.NET Framework的项目中使用它们.

That works because all (most?) AspNetCore packages target .NET Standard, not .NET Core, and you can use them in projects targeting full .NET Framework.

请注意,这样做您就拥有了asp.net Core项目,但没有在.NET Core上,因此产生了所有后果(不能与dotnet run运行,在Linux上需要与mono运行,依此类推)

Note that by doing that you have asp.net Core project, but not on .NET Core, with all consequences that come from that (cannot run with dotnet run, on linux need to run with mono, and so on).

这篇关于无法配置"IApplicationBuilder UseOwin"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:37