小编42v*_*ons的帖子

带伪造的单元测试中的Visual Studio 2015 InvalidProgramException

我正在使用Visual Studio 2015 Enterprise RTM为使用Unity Container的项目编写单元测试.

我发现为Unity添加伪造程序集的简单行为,甚至实际上不使用伪造,足以生成此异常:

System.InvalidProgramException:公共语言运行时检测到无效程序.



请考虑以下步骤来重现:

  • 使用Visual Studio 2015 Enterprise RTM创建面向.NET 4.6的单元测试项目

  • 添加NuGet包"Unity"版本3.5.1404.0

  • 添加NuGet包"CommonServiceLocator"1.2.0版

  • 像这样写一个单元测试:

[TestClass]
public class UnitTest1 : IDisposable
{
    [TestMethod]
    public void TestMethod1()
    {
        new ResolvedArrayParameter<IDisposable>(new IDisposable[] {this});
    }

    void IDisposable.Dispose()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 验证测试通过

  • 右键单击Microsoft.Practices.Unity参考并选择"Add Fakes Assembly"

  • 重新运行测试

  • 观察以下显着的测试失败:

测试名称:TestMethod1
测试FullName:UnitTestProject11.UnitTest1.TestMethod1
测试源:c:\ temp\UnitTestProject11\UnitTestProject11\UnitTest1.cs:第12行
测试结果:失败
测试持续时间:0:00:00.0572447

结果StackTrace:

在Microsoft.Practices.Unity.ResolvedArrayParameter..ctor(类型arrayParameterType,Type elementType,Object [] elementValues)
,
位于UnitTestProject11.UnitTest1的Microsoft.Practices.Unity.ResolvedArrayParameter`1..ctor(Object [] elementValues). C:\ temp\UnitTestProject11\UnitTestProject11\UnitTest1.cs中的TestMethod1():第13行
结果消息:
测试方法UnitTestProject11.UnitTest1.TestMethod1引发异常:
System.InvalidProgramException:公共语言运行时检测到无效程序.



这个问题最显着的特点是显然假货甚至不需要直接出现在代码中就无法显示.

大量的小提琴显示,将测试项目重新定位到.NET 4.5"修复"了这个问题,这对我来说是一个非首发因素,因为我在几周后发布了另一个问题.

几乎所有的假货设置(代码合同等)都更难以解决问题.

关于这个问题的任何建议都将非常感谢.

c# unit-testing microsoft-fakes visual-studio-2015 .net-4.6

27
推荐指数
1
解决办法
1417
查看次数

ASP.NET 5 OAuthBearerAuthentication:未接受以下身份验证方案:承载

更新:

Pinpoint帮助我从发射台上取下这个原型 - 我非常接近,除了:

  • 我需要按照这些说明升级到beta6 SDK .Global.json现在显示如下:
{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-beta6"
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 我更新了project.json中的引用:
{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Mvc": "6.0.0-beta6",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta6",
    "System.IdentityModel.Tokens": "5.0.0-beta6-207211625",
    "Serilog.Framework.Logging": "1.0.0-beta-43",
    "Microsoft.AspNet.Authentication.OAuthBearer": "1.0.0-beta6"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --config hosting.ini"
  },

  "frameworks": {
    "dnx451": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ]
}
Run Code Online (Sandbox Code Playgroud)
  • 启动的Configure方法中的中间件顺序很重要.UseOAuthBearerAuthentication需要在UseMvc之前.Startup.cs中的Configure方法现在显示如下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ …
Run Code Online (Sandbox Code Playgroud)

jwt asp.net-core-mvc

10
推荐指数
1
解决办法
2985
查看次数

在Visual Studio 2015中打破假货

我有Visual Studio 2015 Enterprise,发现我几乎无法编译任何现有的2013项目.进一步调查显示,Visual Studio 2015无法为Visual Studio 2013中的类型生成假货.

请考虑以下步骤来重现:

  • 创建面向.NET 4.5的单元测试项目
  • 右键单击引用选项卡中的"系统",然后选择"添加假装配"
  • 要消除此示例中的噪声,请编辑后续的"System.fakes"文件:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="System" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
  </ShimGeneration>  
</Fakes>
Run Code Online (Sandbox Code Playgroud)
  • 然后编辑"mscorlib.fakes"文件以填充ConcurrentDictionary <TKey,TValue>:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="mscorlib" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
    <Add FullName="ConcurrentDictionary"/>
  </ShimGeneration>
</Fakes>
Run Code Online (Sandbox Code Playgroud)
  • 编译项目.

  • 这会在mscorlib.4.0.0.0.Fakes.messages文件中生成以下错误消息(在FakesAssemblies文件夹中生成):

警告:编译失败,重试没有代码合同

并在输出窗口中显示以下错误消息:

c:\ temp\UnitTestProject1\UnitTestProject1\f.cs(955,13):错误CS0311:类型'System.Collections.Concurrent.ConcurrentDictionary'不能在泛型类型或方法'ShimRuntime中用作类型参数'TShimmed'. Bind(TShim,TBound)'.没有从'System.Collections.Concurrent.ConcurrentDictionary'到'System.Collections.Generic.IReadOnlyCollection>'的隐式引用转换.[C:\ TEMP\UnitTestProject1\UnitTestProject1\OBJ \调试\伪造品\米\ f.csproj]

c:\ temp\UnitTestProject1\UnitTestProject1\f.cs(979,13):错误CS0311:类型'System.Collections.Concurrent.ConcurrentDictionary'不能在泛型类型或方法'ShimRuntime中用作类型参数'TShimmed'. Bind(TShim,TBound)'.没有从'System.Collections.Concurrent.ConcurrentDictionary'到'System.Collections.Generic.IReadOnlyDictionary'的隐式引用转换.[C:\ TEMP\UnitTestProject1\UnitTestProject1\OBJ \调试\伪造品\米\ f.csproj]

GENERATEFAKES:错误:项目编译失败,退出代码为1

没有任何摆弄mscorlib.fakes设置似乎有帮助.例如,TypeName,FullName和Namespace属性的各种排列产生相同的"编译失败并退出代码1".

禁用代码合同只会打破更多:

警告元素"正版正货"命名空间" http://schemas.microsoft.com/fakes/2011/具有无效的子元素命名空间"StubGeneration" http://schemas.microsoft.com/fakes/2011/ ".UnitTestProject1 c:\ temp\UnitTestProject1\UnitTestProject1\Fakes\mscorlib.fakes 4

任何帮助理解如何解决/解决这个问题将非常感激.

.net-4.5 microsoft-fakes visual-studio-2015

6
推荐指数
1
解决办法
4241
查看次数

验证AspNet.Security.OpenIdConnect.Server发布的令牌(ASP.NET vNext)

我正在使用Visual Studio 2015 Enterprise和ASP.NET vNext Beta8构建一个既发布又使用JWT令牌的端点.我最初由生成令牌自己走近这个,描述在这里.后来@Pinpoint的一篇有用的文章显示,AspNet.Security.OpenIdConnect.Server(又名OIDC)可以配置为我发布和使用令牌.

所以我按照这些说明,站起来,通过从邮递员提交x-www-form-urlencoded帖子,我收到了一个合法的令牌:

{
  "token_type": "bearer",
  "access_token": "eyJ0eXAiO....",
  "expires_in": "3599"
}
Run Code Online (Sandbox Code Playgroud)

这很棒,但也是我卡住的地方.现在,如何注释控制器操作以便它需要此持有者令牌?

我认为我所要做的就是用[Authorize("Bearer")]装饰我的控制器方法,添加一个认证方案:

        services.AddAuthorization
        (
            options => 
            {
                options.AddPolicy
                (
                    JwtBearerDefaults.AuthenticationScheme, 
                    builder => 
                    {
                        builder.
                        AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme).
                        RequireAuthenticatedUser().
                        Build();
                    } 
                );
            }
        );
Run Code Online (Sandbox Code Playgroud)

然后像我在前面的例子中所做的那样用"授权持有者eyJ0eXAiO ...."标题调用我的控制器动作.可悲的是,所有这些方法似乎都会产生异常:

处理请求时发生未处理的异常.

SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:50000

WebException:无法连接到远程服务器

HttpRequestException:发送请求时发生错误.

IOException:IDX10804:无法从以下位置检索文档:' http:// localhost:50000/.well-known/openid-configuration '.Microsoft.IdentityModel.Logging.LogHelper.Throw(String message,Type exceptionType,EventLevel logLevel,Exception innerException)

InvalidOperationException:IDX10803:无法从以下位置获取配置:' http:// localhost:50000/.well-known/openid-configuration '.内部异常:'IDX10804:无法从以下位置检索文档:' http:// localhost:50000/.well-known/openid-configuration '.'.


请考虑以下步骤来重现(但请不要考虑这个生产有价值的代码):

  • 描述应用ASP.NET Beta8工装这里

  • 打开Visual Studio Enterprise 2015并创建新的Web API ASP.NET 5预览模板项目

  • 改变project.json

    {
    "根目录": "wwwroot的", …

asp.net jwt openid-connect aspnet-contrib

6
推荐指数
1
解决办法
4225
查看次数