我正在尝试编写自己的授权属性,在那里我使用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) 我在尝试将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) 我有一个包含两个dotnet核心2.1项目(c#)的解决方案.
第一个是控制台应用程序
秒是一个带有单元测试的测试项目
我使用此命令在项目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) 我正在使用WPF .net 4.5(c#),我想创建一个包含一系列用户控件的ListBox.(如果列表框是错误的控件类型,请告诉我).
我希望我的列表框将用户控件的副本作为列表项,每个列表项中包含不同的内容.
如何将用户控件添加到列表框?
感谢您的帮助!
我正在使用point3D和vector3D类,我需要一些帮助调整给定距离的点.

码:
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) 希望这是一个很好而简单的答案.
我在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中完成?
提前感谢任何提示!
我有一个现有的基于云的解决方案,它带有一个 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
我在我的角度项目中运行“ 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) 我对测量执行特定方法所需的时间很感兴趣。
我认为使用自定义属性而不是乱丢垃圾方法来启动/停止秒表并发送到记录器会非常方便。如果我可以使用属性来装饰有问题的方法,那将非常方便!
我能够按照本文创建自定义属性: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
我对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线程.
提前致谢!
c# ×6
asp.net ×2
asp.net-core ×2
wpf ×2
.net ×1
.net-core ×1
angular ×1
angular6 ×1
animation ×1
asp.net-mvc ×1
attributes ×1
cobertura ×1
image ×1
keyframe ×1
listbox ×1
listboxitem ×1
logging ×1
math ×1
point ×1
selenium ×1
stopwatch ×1
testing ×1
unit-testing ×1
vector ×1
xamarin ×1