Tag: .net core

如何在类库中添加配置文件,后跟.NET Core 1.1的连接字符串

我正在开发n层应用程序,我有数据访问层。 这与任何其他应用程序无关。 我已经为.NET Core 1.1创建了类库,我可以看到依赖项文件夹但不能查看任何config / JSON文件。 我想知道,我可以在类库项目中添加AppSetting.JSON文件吗? 接下来我该如何添加连接字符串。 我知道在DbContext类 – > SQLServerConnection中覆盖ConfigureBuilder但我想保留在单独的配置文件中并引用这些连接字符串。 另请注意,此类库不会直接链接到ASP.NET应用程序 在谷歌搜索时,我发现以下链接https://www.stevejgordon.co.uk/project-json-replaced-by-csproj但我的问题仍然存在,我在哪里以及如何为类库项目添加连接字符串?

如何通过分配新的Collection来更新Many-Many中的Collection?

在entity framework核心2.0中, Post和Category之间有很多关系(绑定类是PostCategory )。 当用户更新Post ,整个Post对象(及其PostCategory集合)将被发送到服务器,在这里我想重新分配新收到的Collection PostCategory (用户可以通过添加新类别和删除来显着更改此Collection一些类别)。 我用来更新该集合的简化代码(我只是分配了全新的集合): var post = await dbContext.Posts .Include(p => p.PostCategories) .ThenInclude(pc => pc.Category) .SingleOrDefaultAsync(someId); post.PostCategories = … Some new collection…; // <<< dbContext.Posts.Update(post); await dbContext.SaveChangesAsync(); 此新集合具有在先前集合中具有相同Id对象的对象(例如,用户删除了一些(但不是全部)类别) 。 因为,我得到一个例外: System.InvalidOperationException:无法跟踪实体类型“PostCategory”的实例,因为已经跟踪了另一个具有{‘CategoryId’,’PostId’}键值的实例。 如何在不获得此exception的情况下有效地重建新集合(或简单地分配新集合)? UPDATE 这个链接中的答案似乎与我想要的有关,但它是一种好的有效方法吗? 有没有更好的方法? 更新2 我得到我的post(编辑覆盖其值),如下所示: public async Task GetPostAsync(Guid postId) { return await dbContext.Posts .Include(p => p.Writer) .ThenInclude(u […]

如何在ASP.NET Core中启用跟踪日志记录?

我无法在我的应用程序中获得basice LogTrace(…)输出。 这是一个复制品: 使用Visual Studio 2017创建新的ASP.NET Core应用程序。 (可选)注释掉.UseApplicationInsights()以使.UseApplicationInsights()更清晰 用这个替换ValuesController.cs的代码: using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace WebApplication1.Controllers { [Route(“api/[controller]”)] public class ValuesController : Controller { private readonly ILogger logger; public ValuesController(ILogger logger) { this.logger = logger; } [HttpGet] public IEnumerable Get() { logger.LogError(“ERROR!”); logger.LogWarning(“WARN!”); logger.LogInformation(“INFO!”); logger.LogTrace(“TRACE!”); return new string[] { “value1”, “value2” }; } } […]

在dotnet核心中读取json文件时出错“已达到inotify实例数量的已配置用户限制(128)”

我有一个控制台应用程序(在网络核心1.1中),它每隔1分钟在cron调度程序中安排一次。 在应用程序内部有调用配置文件。 我附上下面的代码。 public static T GetAppConfig(string key, T defaultValue = default(T)) { T value = default(T); logger.Debug($”Reading App Config {key}”); try { var environmentName = Environment.GetEnvironmentVariable(“ASPNETCORE_ENVIRONMENT”); var builder = new ConfigurationBuilder() .AddJsonFile($”appsettings.json”, true, true) .AddJsonFile($”appsettings.{environmentName}.json”, true, true) .AddEnvironmentVariables(); var configuration = builder.Build(); T setting = (T)Convert.ChangeType(configuration[key], typeof(T)); value = setting; if (setting == null) […]

Microsoft.DotNet.Common.Targets(262,5):错误:对象引用未设置为对象的实例

我正在将库从.Net 4.5迁移到.net core 。 我已经安装了.xproj ,但是当我运行dotnet build出现错误: Microsoft.DotNet.Common.Targets(262,5):错误:Obje ct引用未设置为对象的实例。 这是我的project.json文件: { “version”: “1.0.0”, “frameworks”: { “net45”: { “frameworkAssemblies”: { “System.Collections.Concurrent”: { “type”: “build” } } }, “netstandard1.6”: { “imports”: “dnxcore50”, “dependencies”: { “NETStandard.Library”: “1.6.0” } } } } 有什么我想念的吗?

调用AddUserAsync .NET Core 2.0时出现PlatformNotSupportedexception

我正在尝试编写一些使用Graph API在Azure AD中创建用户的代码。 我开始用网上的一个例子,但现在它在添加用户时就失败了 await adClient.Users.AddUserAsync(userGraphObj); 在下面的CreateUser()方法中。 我得到的错误是 我正在使用.NET Core 2.0,在Windows 7上进行调试。谷歌搜索并发现它们为2.0提供了序列化, 但仅针对特定类型 。 我真的不在乎。 如何在代码中将用户添加到Azure AD? const String appClientID = “2be733f1-88c3-6482-8e2a-5e9631fc3a32”; const String tenant = “brazzers.onmicrosoft.com”; const String authString = “https://login.microsoftonline.com/” + tenant; const String authClientSecret = “dDdaVGee315s65ewDSWEwfdw7wq5efDNO5C3cvN4RA”; const String resAzureGraphAPI = “https://graph.windows.net”; const String serviceRootURL = resAzureGraphAPI + appClientID; private ActiveDirectoryClient GetAADClient() { […]

在Linux机器上运行ac#app时,SOAP身份validation失败

我正在尝试通过ac#app连接到第三方SOAP服务。 在Windows计算机上运行应用程序时,以下代码有效: var ws = new MyWebServiceClient(); ws.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(“myusername”, “mypassword”, “mydomain”); var response = ws.SomeEndpoint(); Element xmlResult = response.Result.SomeEndpoint; … 但是,如果我从Linux或Mac OS运行相同的代码 ,它将失败: The HTTP request is unauthorized with client authentication scheme ‘Negotiate’. The authentication header received from the server was ‘Negotiate, NTLM’. 我有一个python应用程序,可以在任何操作系统上运行时使用相同的SOAP服务而不会遇到问题,所以问题不在我的Linux发行版/设置中。 有没有人看到.NET核心的类似问题或找到了解决方法? 我发现这个问题报告表明早期版本的.NET核心存在限制/错误,可能导致类似于我所看到的行为,但它声称这些问题在RC2之后得到了解决。 假设问题报告是错误的并且问题仍然存在于.NET核心的Linux / Mac发行版中,是否有人知道我如何获得CredentialCache解决方法,在该文章中建议使用SOAP客户端? 我是.NET的新手,也是.NET soap客户的新手,所以如果这是一个天真的问题我会道歉。 看来,对于非Windows,.NET核心在协商失败后无法尝试NTLM。 我知道,从python应用程序,NTLM使用这个特定的SOAP服务。 […]

如何在.NET Core中使用HttpClientHandler和HttpClientFactory

我想使用.NET Core 2.1中提供的HttpClientFactory ,但我还想在创建HttpClients时使用HttpClientHandler来利用AutomaticDecompression属性。 我很挣扎,因为.AddHttpMessageHandler接受DelegatingHandler而不是HttpClientHandler 。 有谁知道如何使这个工作? 谢谢,吉姆

.NET Core IServiceScopeFactory.CreateScope()vs IServiceProvider.CreateScope()扩展

我的理解是,当使用内置dependency injection时,.NET Core控制台应用程序将要求您自己创建和管理所有范围,而ASP.NET Core应用程序将默认通过定义的中间件创建和管理HttpRequest范围(s )。 使用ASP.NET Core,您可以选择创建和管理自己的范围,通过在需要HttpRequest之外的服务时调用CreateScope() 。 很明显,每次调用IServiceScopeFactory.CreateScope()都会创建一个新的IServiceScope ; 但是,调用IServiceProvider.CreateScope()扩展方法是否每次都会创建一个新的IServiceScope ? 基本上,在ASP.NET Core和.NET Core控制台应用程序中创建范围的以下方法之间是否存在有意义的差异: public class Foo() { public Foo(IServiceProvider serviceProvider) { using(var scope = serviceProvider.CreateScope()) { scope.ServiceProvider.GetServices(); } } } 和 public class Bar() { public Bar(IServiceScopeFactory scopeFactory) { using(var scope = scopeFactory.CreateScope()) { scope.ServiceProvider.GetServices(); } } }

C#.NET CORE如何获取自定义属性的值?

我有一个自定义属性类定义如下。 [AttributeUsage(AttributeTargets.Property, Inherited = false)] internal class EncryptedAttribute : System.Attribute { private bool _encrypted; public EncryptedAttribute(bool encrypted) { _encrypted = encrypted; } public virtual bool Encrypted { get { return _encrypted; } } } 我将以上属性应用于另一个类,如下所示。 public class KeyVaultConfiguration { [Encrypted(true)] public string AuthClientId { get; set; } = “”; public string AuthClientCertThumbprint { get; set; […]