Tag: asp.net core

在Asp.net Core中设置CultureInfo有一个。 作为CurrencyDecimalSeparator而不是,

我疯了。 我只想将整个Asp.net核心应用程序中使用的文化设置为“en-US”。 但似乎没有任何效果。 我在哪里设置整个应用程序的文化? 我对客户端浏览器文化并不感兴趣。 唯一似乎改变它的是改变Windows的语言设置。 我只是希望从应用程序本身而不是客户端确定文化。 到目前为止我尝试了什么: 在web.config中设置 设置System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo; Startup.Configure中的CurrentUICulture和甚至控制器中的CurrentUICulture 。 使用app.UseRequestLocalization(..如下所示 var enUsCulture = new CultureInfo(“en-US”); var localizationOptions = new RequestLocalizationOptions() { SupportedCultures = new List() { enUsCulture }, SupportedUICultures = new List() { enUsCulture }, DefaultRequestCulture = new RequestCulture(enUsCulture), FallBackToParentCultures = false, FallBackToParentUICultures = false, RequestCultureProviders = null }; app.UseRequestLocalization(localizationOptions); […]

没有数据库提供程序配置EF7

使用Entity Framework 7和MVC6时,我似乎收到此错误消息 System.InvalidOperationException未配置任何数据库提供程序。 在设置服务时,通过在DbContext类或AddDbContext方法中覆盖OnConfiguring来配置数据库提供程序。 我相信我已经做了我应该做的一切,所以也许它是一个错误。 我使用的是Entity Framework的7.0.0-beta7版本。 我已经设置了我的DbContext,一个接口,所以我可以模拟DbContext(在EntityFramework 6中需要进行unit testing)。 我的服务将接口作为构造函数,我在MVC 6中设置了DI。 在我的Startup.cs文件中,我有以下内容 public void ConfigureServices(IServiceCollection services) { // entity framework services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration[“Data:MyConnection:ConnectionString”]) ); // Add MVC services to the services container. services.AddMvc(); // new context on each request services.AddScoped(); } 我已经检查了我的connectionString,并且返回了一个有效的连接。 我还检查了我的服务,正在注入该对象,并且它不是null,因此应该全部工作。 我的config.json文件看起来像这样 { “Data”: { “MyConnection”: { “ConnectionString”: “Server=(local);Database=XXXX;Trusted_Connection=True;” […]

如何在ASP.NET Core中validationDI容器?

在我的Startup类中,我使用ConfigureServices(IServiceCollection services)方法来设置我的服务容器,使用来自Microsoft.Extensions.DependencyInjection的内置DI容器。 我想在unit testing中validation依赖图,以检查是否可以构造所有服务,这样我就可以修复在unit testing期间丢失的任何服务,而不是让应用程序在运行时崩溃。 在以前的项目中,我使用了Simple Injector,它对容器有一个.Verify()方法。 但是我无法找到类似ASP.NET Core的东西。 是否有任何内置(或至少推荐)的方法来validation是否可以构建整个依赖图? (我能想到的最愚蠢的方式就是这样,但由于框架本身注入的开放式generics,它仍然会失败): startup.ConfigureServices(serviceCollection); var provider = serviceCollection.BuildServiceProvider(); foreach (var serviceDescriptor in serviceCollection) { provider.GetService(serviceDescriptor.ServiceType); }

如何使用DI在Class Constructor中获取Microsoft.AspNet.Http.HttpContext实例

我正在MVC 6中构建一个一次性应用程序,并尝试使用不同的依赖架构。 我面临的问题是如何创建特定于应用程序的自定义“ MyAppContext ”对象。 这将需要来自HttpContext一些信息和来自数据库的一些信息,并且将是针对应用程序特定属性的请求范围的存储库。 我想将HttpContext的实例传递给’ MyAppContext ‘的构造函数。 我已经使用DI成功创建了一个带有IDataService接口的’ DataService ‘对象,这IDataService用。 与’MyAppContext’类的不同之处在于它在构造函数中有两个参数–‘ DataService ‘和Microsoft.AspNet.Http.HttpContext 。 这是MyAppContext类: public class MyAppContext : IMyAppContext { public MyAppContext(IDataService dataService, HttpContext httpContext) { //do stuff here with the httpContext } } 在启动代码中,我注册了DataService实例和MyAppContext实例: public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //adds a singleton instance of the DataService using DI services.AddSingleton(); services.AddScoped(); […]

用于JWT承载认证的.NET Core IssuerSigningKey文件

我正在努力实现(或理解)JWT承载令牌认证的签名密钥。 我希望有人可以帮助我或解释我的错误。 在过去的几周里,我抓了大量的教程,设法让一个自定义的Auth-Controller运行,它发出我的令牌并设法建立JWT承载认证,以validation标题中的令牌。 有用。 我的问题是所有示例和教程要么生成随机或内存(发行者)签名密钥,要么使用硬编码的“密码”字符串或从某些配置文件中获取它们(在代码示例中查找“密码”)。 我的意思是validation设置(在StartUp.cs中): //using hardcoded “password” SecurityKey key = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(“password”)); app.UseJwtBearerAuthentication(new JwtBearerOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = “MyIssuer”, ValidateAudience = true, ValidAudience = “MyAudience”, ValidateLifetime = true, IssuerSigningKey = key } }); 在创建令牌的AuthController中: //using hardcoded password var signingKey = […]

如何在dotnet核心中动态加载程序集

我正在构建一个Web应用程序,我想要单独关注,即在不同的项目中进行抽象和实现。 为了实现这一点,我尝试实现一个组合根概念,其中所有实现必须有一个ICompositionRootComposer实例来注册服务,类型等。 public interface ICompositionRootComposer { void Compose(ICompositionConfigurator configurator); } 在构建层次结构中直接引用的项目中,将调用ICompositionRootComposer实现,并在基础IoC容器中正确注册服务。 当我尝试在项目中注册服务时出现问题,我在其中设置了一个后期构建任务,将构建的dll复制到Web项目的调试文件夹: cp -R $(TargetDir)”assembly and symbol name”* $(SolutionDir)src/”webproject path”/bin/Debug/netcoreapp1.1 我正在加载程序集:(灵感: 如何在.net核心控制台应用程序中加载位于文件夹中的程序集 ) internal class AssemblyLoader : AssemblyLoadContext { private string folderPath; internal AssemblyLoader(string folderPath) { this.folderPath = Path.GetDirectoryName(folderPath); } internal Assembly Load(string filePath) { FileInfo fileInfo = new FileInfo(filePath); AssemblyName assemblyName = new AssemblyName(fileInfo.Name.Replace(fileInfo.Extension, […]

将图像添加到ASP.net核心

我想添加一个图像作为模型类的一部分,并将其显示在索引视图上。 我有问题要么将图像分类为byte []或iFormFile。 这就是我想要实现的目标 创建一个页面以插入员工列表 在索引页面中,能够列出员工并查看他们的图像。 这是模型。 Employee.cs using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; namespace MvcMovie.Models { public class Employee { public int ID { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string Nationality { […]

如何使用EF Core 2.1.0和代理进行延迟加载

我有以下型号: public class Session { public int SessionID { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } public class User { public int UserID { get; set; } public int OrganizationID { get; set; } public virtual ICollection Sessions { get; set; } public […]

Vnext Argument 1:无法从’string’转换为’System.IO.Stream’

我正在尝试使用Vnext项目创建一个通用的序列化程序 ,当我调用StreamWriter的构造函数时,它会抛出此编译器错误 错误CS1503参数1:无法从’string’转换为’System.IO.Stream’Test.ASP.NET Core 5.0 Helper.cs 14 即使有一个构造函数允许指定文件的路径作为参数。 这是我的class级文件 using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; using System.IO; namespace Test { public static class Helper { public static void SerializeToXml(string path, T value) { var serializer = new XmlSerializer(typeof(T)); using (var stream = new StreamWriter(path)) // ERROR OCCURS HERE { using (var writer = […]

模型类(实体)中的dependency injection

我正在使用Entity Framework Code-First构建ASP.NET Core MVC应用程序。 我选择实现一个简单的存储库模式,为我创建的所有模型类提供基本的CRUD操作。 我选择遵循http://docs.asp.net中提供的所有建议,DI就是其中之一。 在.NET 5中,dependency injection非常适用于我们不直接实例化的任何类(例如:控制器,数据存储库,……)。 我们只需通过构造函数注入它们,并在应用程序的Startup类中注册映射: // Some repository class public class MyRepository : IMyRepository { private readonly IMyDependency _myDependency; public MyRepository(IMyDependency myDependency) { _myDependency = myDependency; } } // In startup.cs : services.AddScoped(); services.AddScoped(); 我遇到的问题是,在我的一些模型类中,我想注入一些我声明的依赖项。 但我认为我不能使用构造函数注入模式,因为模型类通常是明确地实现的,因此,我需要为自己提供依赖项,我不能。 所以我的问题是:是否有另一种方式比构造函数注入注入依赖项,以及如何? 我是在考虑属性模式或类似的东西。