- Custom tasks with default contextType via tasks.vs.json are broken in Open Folder.
- No snapshot created for C++ native code in Memory Usage tool in the Diagnostic Tools window while debugging.
- Crash in VS 16.1.0 when pressing the link "search online".
- Fixed the issue where sometimes certain features (i.e. Find All References) in LiveShare guest session do not work.
- Fixed an issue where the devenv.exe process could hang around after shutdown of Visual Studio for up to 30 seconds.
سری ساخت افزونههای Visual Studio
services.AddOptions<BearerTokensOptions>() .Bind(configuration.GetSection("BearerTokens")) .Validate(bearerTokens => { return bearerTokens.AccessTokenExpirationMinutes < bearerTokens.RefreshTokenExpirationMinutes; }, "RefreshTokenExpirationMinutes is less than AccessTokenExpirationMinutes. Obtaining new tokens using the refresh token should happen only if the access token has expired.");
معرفی تنظیمات برنامه
فرض کنید فایل appsettings.json برنامه یک چنین محتوایی را دارد:
{ "ApiSettings": { "AllowedEndpoints": [ { "Name": "Service 1", "Timeout": 30, "Url": "http://service1.site.com" }, { "Name": "Service 2", "Timeout": 10, "Url": "https://service2.site.com" } ] } }
ایجاد مدلهای معادل تنظیمات JSON برنامه
بر اساس تعاریف JSON فوق، میتوان به مدلهای زیر رسید:
using System; using System.Collections.Generic; namespace FluentValidationSample.Models { public class AllowedEndpoint { public string Name { get; set; } public int Timeout { get; set; } public Uri Url { get; set; } } public class ApiSettings { public IEnumerable<AllowedEndpoint> AllowedEndpoints { get; set; } } }
namespace FluentValidationSample.Web { public class Startup { public void ConfigureServices(IServiceCollection services) { services.Configure<ApiSettings>(Configuration.GetSection(nameof(ApiSettings)));
تعریف شرطهای اعتبارسنجی مدلهای تنظیمات برنامه
پس از مدلسازی تنظیمات برنامه و همچنین اتصال آن به <IOptions<ApiSettings، اکنون میخواهیم این مدلها، شرایط زیر را برآورده کنند:
- باید مدخل ApiSettings در فایل تنظیمات برنامه وجود خارجی داشته باشد.
- میخواهیم AllowedEndpointها نامدار بوده و هر نام نیز منحصربفرد باشد.
- مقادیر timeoutها باید بین 1 و 90 تعریف شده باشند.
- تمام URLها باید منحصربفرد باشند.
- تمام URLها باید HTTPS باشند.
برای این منظور میتوان تنظیمات زیر را توسط Fluent Validation تعریف کرد:
using System; using System.Linq; using FluentValidation; using FluentValidationSample.Models; namespace FluentValidationSample.ModelsValidations { public class ApiSettingsValidator : AbstractValidator<ApiSettings> { public ApiSettingsValidator() { RuleFor(apiSetting => apiSetting).NotNull() .WithMessage("مدخل ApiSettings تعریف نشدهاست."); RuleFor(apiSetting => apiSetting.AllowedEndpoints).NotNull().NotEmpty() .WithMessage("مدخل AllowedEndpoints تعریف نشدهاست."); When(apiSetting => apiSetting.AllowedEndpoints != null, () => { RuleFor(apiSetting => apiSetting.AllowedEndpoints) .Must(endpoints => endpoints.GroupBy(endpoint => endpoint.Name).Count() == endpoints.Count()) .WithMessage("نامهای سرویسها باید منحصربفرد باشند."); RuleFor(apiSetting => apiSetting.AllowedEndpoints) .Must(endpoints => !endpoints.Any(endpoint => endpoint.Timeout > 90 || endpoint.Timeout < 1)) .WithMessage("مقدار timeout باید بین 1 و 90 باشد"); RuleFor(apiSetting => apiSetting.AllowedEndpoints) .Must(endpoints => endpoints.GroupBy(endpoint => endpoint.Url.ToString().ToLower()).Count() == endpoints.Count()) .WithMessage("آدرسهای سرویسها باید منحصربفرد باشند."); RuleFor(apiSetting => apiSetting.AllowedEndpoints) .Must(endpoints => endpoints.All(endpoint => endpoint.Url.Scheme.Equals("https", StringComparison.CurrentCultureIgnoreCase))) .WithMessage("تمام آدرسها باید HTTPS باشند."); }); } } }
- چگونه میتوان از تعریف و وجود یک مدخل فایل JSON، اطمینان حاصل کرد (اعمال RuleFor به کل مدل).
- چگونه میتوان اگر مدخلی تعریف شده بود، آنگاه برای آن اعتبارسنجی خاصی را تعریف کرد (متد When).
- چگونه میتوان شرایط سفارشی خاصی را مانند بررسی منحصربفرد بودنها، بررسی کرد (متد Must).
یکپارچه کردن اعتبارسنجی کتابخانهی FluentValidation با اعتبارسنجی توکار مدلهای تنظیمات برنامه توسط ASP.NET Core
در ابتدای بحث، امکان تعریف متد Validate را که از نگارش ASP.NET Core 2.2 اضافه شدهاست، مشاهده کردید:
services.AddOptions<BearerTokensOptions>() .Bind(configuration.GetSection("BearerTokens")) .Validate(bearerTokens => { return bearerTokens.AccessTokenExpirationMinutes < bearerTokens.RefreshTokenExpirationMinutes; }, "RefreshTokenExpirationMinutes is less than AccessTokenExpirationMinutes. Obtaining new tokens using the refresh token should happen only if the access token has expired.");
namespace Microsoft.Extensions.Options { public interface IValidateOptions<TOptions> where TOptions : class { ValidateOptionsResult Validate(string name, TOptions options); } }
در ادامه یک نمونه پیاده سازی جنریک IValidateOptions استاندارد ASP.NET Core را مشاهده میکنید:
using System.Linq; using FluentValidation; using Microsoft.Extensions.Options; namespace FluentValidationSample.ModelsValidations { public class AppConfigValidator<TOptions> : IValidateOptions<TOptions> where TOptions : class { private readonly IValidator<TOptions> _validator; public AppConfigValidator(IValidator<TOptions> validator) { _validator = validator; } public ValidateOptionsResult Validate(string name, TOptions options) { if (options is null) { return ValidateOptionsResult.Fail("Configuration object is null."); } var validationResult = _validator.Validate(options); return validationResult.IsValid ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(validationResult.Errors.Select(error => error.ToString())); } } }
در آخر روش معرفی آن به سیستم به صورت زیر است:
namespace FluentValidationSample.Web { public class Startup { public void ConfigureServices(IServiceCollection services) { services.Configure<ApiSettings>(Configuration.GetSection(nameof(ApiSettings))); services.AddTransient<IValidateOptions<ApiSettings>, AppConfigValidator<ApiSettings>>();
namespace FluentValidationSample.Web.Controllers { public class HomeController : Controller { private readonly IUsersService _usersService; private readonly ApiSettings _apiSettings; public HomeController(IUsersService usersService, IOptions<ApiSettings> apiSettings) { _usersService = usersService; _apiSettings = apiSettings.Value; }
An unhandled exception occurred while processing the request. OptionsValidationException: تمام آدرسها باید HTTPS باشند.
کدهای کامل این سری را تا این قسمت از اینجا میتوانید دریافت کنید: FluentValidationSample-part05.zip
This article dives into the mock questions I would ask, along with responses that are my personal best guess to the answers. Could my answers not reflect actual opinions shared by the team at Microsoft? Sure, but I'm hoping folks from the .NET team can jump in to correct me if I am way off base.
This is a rather interesting time for .NET – what's being done shapes the future of .NET for the next decade. Let's ask the honest questions and hopefully all of us will understand the new .NET ecosystem a little better.
نگاهی به C# 8.0
NET 7 Preview 5. منتشر شد
Today we released .NET 7 Preview 5. This preview of .NET 7 includes improvements to Generic Math which make the lives of API authors easier, a new Text Classification API for ML.NET that adds state-of-the-art deep learning techniques for natural language processing, various improvements to source code generators and a new Roslyn analyzer and fixer for RegexGenerator and multiple performance improvements in the areas of CodeGen, Observability, JSON serialization / deserialization and working with streams.
1.Visual Studio 2017 15.9 منتشر شد
Issues Fixed in 15.9.1
These are the issues addressed in 15.9.1:
- Fixed a bug where Visual Studio would fail to build projects using the Microsoft Xbox One XDK.
Details of What's New in 15.9.1
Universal Windows Platform Development SDK
The Windows 10 October 2018 Update SDK (build 17763) is now the default selected SDK for the Universal Windows Platform development workload.
Professional REST API design with ASP.NET Core and WebAPI
This project is an example of lightweight and extensible infrastructure for building RESTful Web API with ASP.NET Core.
This example contains a number of tricks and techniques which I've learned while building APIs in ASP.NET Core.
Techniques and Features
- JWT Authentication
- Secure JWT using Encryption (JWE)
- Logging to File, Console and Database using Elmah & NLog
- Logging to sentry.io (Log Management System)
- Exception Handling using Custom Middleware
- Automatic Validation
- Standard API Resulting
- Dependency Injection using Autofac
- Map resources using AutoMapper
- Async/Await Best Practices
- Versioning Management
- Using Swagger (Swashbuckle)
- Auto Document Generator for Swagger
- Integrate Swagger and Versioning
- Integrate Swagger and JWT/OAuth Authentication
- Best Practices for Performance and Security