小编Rob*_*abe的帖子

当我试图强制401时,自定义授权过滤器总是返回404

我正在尝试编写自己的授权属性,在那里我使用CustomAuthorization属性对任何web api方法执行一些自定义检查.

我的代码如下:

   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class CustomAuthorization : AuthorizationFilterAttribute
    {
        public override void OnAuthorization(AuthorizationContext context)
        {
            //// Attempt 1 - 404 error.  
            //// Doesnt block method with this attribute from executing (not desired behaviour).

            //context.HttpContext.Response.StatusCode = 401;
            //return; 

            //// Attempt 2 - 404 result. 
            //// Code with attribute doesnt execute (desired).
            //// Error thrown says:  An exception of type 'System.Web.Http.HttpResponseException' occurred in <namespace> but was not handled in user code
            //// Additional …
Run Code Online (Sandbox Code Playgroud)

asp.net attributes asp.net-web-api2

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

EntityFramework Core 2.0 - 添加迁移错误"未安装EntityFramework包"

我在尝试将Entity Framework Core的迁移添加到Code First项目时遇到错误,这里有详细信息......

我创建了一个新的ASP.Net核心Web项目(VS 2017中的Core 2.0).它使用Microsoft.AspNetCore.All依赖项,如下所示:

在此输入图像描述

我希望利用实体框架核心(我的理解是,所有元数据都已包含EF Core依赖项,如下所示,看起来是正确的):

在此输入图像描述

我已经设置了我的实体和上下文,并且我确保使用以下代码设置数据库.

示例模型

public class City
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MaxLength(50)]
    public string Name { get; set; }

    [MaxLength(200)]
    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

示例上下文

public class CityInfoContext : DbContext
{
    public DbSet<City> Cities { get; set; }
    public DbSet<PointOfInterest> PointsOfInterest { get; set; }

    public CityInfoContext(DbContextOptions options) : base(options)
    {
        Database.EnsureCreated();
    }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs配置

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
    .AddMvcOptions(options …
Run Code Online (Sandbox Code Playgroud)

c# ef-migrations entity-framework-core asp.net-core

6
推荐指数
2
解决办法
6602
查看次数

dotnet cli - 指定的.runsettings文件未在代码覆盖率运行中使用

我有一个包含两个dotnet核心2.1项目(c#)的解决方案.

  1. 第一个是控制台应用程序

  2. 秒是一个带有单元测试的测试项目

我使用此命令在项目2中执行测试时生成有关项目1的代码覆盖率统计信息:

dotnet test C:\tempDir\SampleApp\Tests\SampleApp.Tests.csproj 
/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura 
/p:CoverletOutput=C:\tempDir\Coverage\coverage 
/p:settings=CodeCoverage.runsettings --filter Category=Unit --logger trx 
--results-directory C:\tempDir\output
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到我指定CodeCoverage.runsettings作为设置参数 - /p:settings=CodeCoverage.runsettings.在我的运行设置文件中,我已经询问Program.cs并将Startup.cs其从coverage中排除,但它们仍包含在输出coverage.cobertura.xml文件中.

从以下输出报告中提取:

<classes>
    <class name="SampleApp.Startup" filename="SampleApp\Startup.cs" line-rate="1" branch-rate="0" complexity="2">
      <methods>
        <method name="ConfigureAppConfiguration" signature="(Microsoft.Extensions.Configuration.IConfigurationBuilder)" line-rate="1" branch-rate="0">
          <lines>
            <line number="18" hits="1" branch="False" />
            <line number="19" hits="1" branch="False" />
            <line number="20" hits="1" branch="False" />
          </lines>
        </method>
        <method name="ConfigureLogging" signature="(Microsoft.Extensions.Configuration.IConfiguration,Microsoft.Extensions.Logging.ILoggingBuilder)" line-rate="1" branch-rate="0">
          <lines>
            <line number="23" hits="1" branch="False" />
            <line number="24" hits="1" branch="False" />
            <line …
Run Code Online (Sandbox Code Playgroud)

.net c# testing code-coverage cobertura

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

如何将用户控件添加为列表框项

我正在使用WPF .net 4.5(c#),我想创建一个包含一系列用户控件的ListBox.(如果列表框是错误的控件类型,请告诉我).

我希望我的列表框将用户控件的副本作为列表项,每个列表项中包含不同的内容.

如何将用户控件添加到列表框?

感谢您的帮助!

c# wpf user-controls listbox listboxitem

5
推荐指数
1
解决办法
2741
查看次数

如何调整一个3D点距另一个3D点的距离给定距离

我正在使用point3D和vector3D类,我需要一些帮助调整给定距离的点.

  • A点 - 位于坐标0,0,0处的点.
  • B点 - 位于坐标1,1,1处的点.
  • 向量AB - 向量AB告诉我两点A和B之间的长度是距离= 1.73205078.

在此输入图像描述

码:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 };
Point3D B = new Point3D { X = 1, Y = 1, Z = 1 };
Vector3D AtoB = A - B;
Double distanceBetweenAandB = AtoB.Length; // the distance will be 1.73205078 here.
Run Code Online (Sandbox Code Playgroud)

我想调整点B.我想将点A和点B之间的距离减小到0.5而不是1(调整到位置C,如图所示).我想弄清楚如何做到这一点.

点A(0,0,0)是已知的,点B(1,1,1)是已知的并且调整的距离是已知的(0.5).我该如何计算?

伪代码:

Point3D A = new Point3D { X = 0, Y = 0, Z = 0 }; …
Run Code Online (Sandbox Code Playgroud)

c# math point vector

5
推荐指数
2
解决办法
1944
查看次数

返回任务<bool>并返回bool的区别是什么

希望这是一个很好而简单的答案.

我在ASP.NET Core 1中使用C#MVC Web API.我正在创建Web方法,并且我已经看到了返回数据的示例.

例1:

public async Task<IActionResult> ExampleCall([FromBody]ExampleObj model)
{
     // Call some lower level function...
     var result = LowerLevelCall(model);
     return result;
}
Run Code Online (Sandbox Code Playgroud)

并看到这样的其他例子.

例2:

public async Task<IActionResult> ExampleCall([FromBody]ExampleObj model)
{
     // Call some lower level function...
     var result = await LowerLevelCall(model);
     return result;
}
Run Code Online (Sandbox Code Playgroud)

主要区别在于使用await运算符的示例2 .实施await运营商的意义是什么?这个免费资源会不会等待代码在示例1中完成?

提前感谢任何提示!

c# asp.net asp.net-mvc task-parallel-library

5
推荐指数
1
解决办法
3031
查看次数

.net 中的数据同步设计

我有一个现有的基于云的解决方案,它带有一个 web api,可以将数据从后端 SQL 数据库传输到客户端应用程序。都非常标准。我的 web api 是使用 .NET Core 构建的。这运行良好,我将现有的 web api 与已构建的各种 web 客户端一起使用。架构如下所示:

当前解决方案 当前的解决方案需要扩展以支持使用 web api 的本地移动客户端应用程序(这里没有什么特别之处 - 通常他们会像任何其他客户端一样调用 web api)但是我必须满足这些新的要求客户端应用程序可用于离线场景。这意味着我不能期望设备上存在数据连接以便每次需要时调用我的 web api。我需要查看同步数据,以便它可以离线并在需要时发送回服务器。

想一想,数据将通过以下两种方式之一进行同步:

  • 单向同步 - 从服务器到客户端的数据,但不会对此数据进行更改,例如系统查找表。
  • 双向同步 - 现有数据将同步到客户端,修改并发送回服务器或在客户端上创建新数据并向上发送,例如新订单。

新架构如下:

在此处输入图片说明

所以 - 开始我的问题 - 有没有人知道在同步数据(单向和双向)或可能有内置同步代码的 NuGet 包方面遵循的良好设计模式?如果可能的话,我试图避免在同步方面重新发明轮子。

注意:仅供参考,本机移动应用程序将在 Visual Studio 2015 中使用 Xamarin 构建。

c# synchronisation data-synchronization xamarin asp.net-core

5
推荐指数
1
解决办法
1468
查看次数

Angular6-无法在Selenium-webdriver \ lib中解析'fs'

我在我的角度项目中运行“ ng test ”时遇到问题。运行时,我得到以下输出:

找不到./node_modules/selenium-webdriver/lib/http.js模块中的错误:错误:无法解析'C:\ Users \ test \ Desktop \ ui-portal \ node_modules \ selenium-webdriver \ lib中的'fs' '@ ./node_modules/selenium-webdriver/lib/http.js 28:11-24 @ ./node_modules/selenium-webdriver/http/index.js @ ./src/app/container/container.component.spec.ts @ ./src .spec.ts $ @ ./src/test.ts

--

找不到./node_modules/selenium-webdriver/lib/devmode.js模块中的错误:错误:无法解析C:\ Users \ test \ Desktop \ ui-portal \ node_modules \ selenium-webdriver \ lib中的'fs' '@ ./node_modules/selenium-webdriver/lib/devmode.js 25:11-24 @ ./node_modules/selenium-webdriver/lib/http.js @ ./node_modules/selenium-webdriver/http/index.js @。 /src/app/container/container.component.spec.ts @ ./src .spec.ts $ @ ./src/test.ts

我一直在寻找答案,但找不到任何有帮助的地方-有人以前见过这个特定问题吗?我的package.json文件如下:

{
  "name": "ui-portal",
  "version": "0.0.1",
  "license": "MIT",
  "scripts": {
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0", …
Run Code Online (Sandbox Code Playgroud)

selenium unit-testing selenium-webdriver angular angular6

5
推荐指数
3
解决办法
5069
查看次数

使用属性(.net core)测量方法执行时间

我对测量执行特定方法所需的时间很感兴趣。

我认为使用自定义属性而不是乱丢垃圾方法来启动/停止秒表并发送到记录器会非常方便。如果我可以使用属性来装饰有问题的方法,那将非常方便!

我能够按照本文创建自定义属性:https : //docs.microsoft.com/en-us/dotnet/standard/attributes/writing-custom-attributes

像这样:

public class MonitorExecutionTime : Attribute
{
    private Stopwatch measureExecution;

    // Start measuring on instantiation
    public MonitorExecutionTime()
    {
        measureExecution = new Stopwatch();
        measureExecution.Start();
    }

    // how do I hook into end invoke?
    public MethodHasEnded()
    {

        measureExecution.Stop();
        TimeSpan timeSpan = measureExecution.Elapsed;

        Console.WriteLine("Time: {0}h {1}m {2}s {3}ms", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不确定如何“捕获”正在调用和结束执行的调用点,以便启动秒表和停止秒表(测量时间并记录它)。

有没有人在 .net 核心应用程序中采用这种方法?在此先感谢您的指点!

logging stopwatch custom-attributes azure-application-insights .net-core

5
推荐指数
2
解决办法
5483
查看次数

WPF - 动画图像源更改

我对WPF很新,但我认为我需要做的事情相对简单.我需要创建一个图像"动画",我每隔0.25秒更换一个图像源.

我有一个名为"animation"的文件夹,图像为1到25 png live(名为1.png,2.png ... 25.png).每个图像都与我动画的不同帧相关联.

我想写xaml来将图像从1改为2,从2改为3,每隔0.25秒改变3到4等,直到它到达第25个图像,然后它应该循环回到开始.

我很可能需要写一些c#才能做到这一点.我希望它在可以与UI交互的线程上运行(如更新图像)但不阻止UI线程.

提前致谢!

wpf animation image keyframe

4
推荐指数
1
解决办法
9488
查看次数