请随意为问题建议一个更好的标题。我想不出一个好名字来描述这个问题。
我需要在启动时通过依赖注入来访问一个类。该类不仅应该通过它的具体实现来使用,还应该通过它实现的接口来使用。我需要确保它是通过两次注入返回的同一对象实例。
导致我想到单例案例的现实世界场景是提供接口的抽象块(IStore),多个块保存具体实现(DBStore、RedisStore)。当我尝试对每个存储实现进行运行状况检查时,我可以注入 IStore,但不能注入具体实现。我想使用一些在具体实现中初始化和修改的变量(这就是为什么我两次注入都需要相同的实例)。由于商店(希望)被用作单身人士,所以它起作用了。我并不是说存在作用域和瞬态方式的现实场景。我只是好奇如果他们不是单身人士这是否可能。
以下代码描述了我如何设法使用单例来做到这一点。
让我找到单例解决方案的方式:
有这个界面:
public interface ITestInterface
{
string ReturnAString();
int ReturnAnInt();
}
Run Code Online (Sandbox Code Playgroud)
以及这个具体的实现
public class TestImplementation : ITestInterface
{
private int counter = 0;
public string ReturnAString() {return "a string"; }
public int ReturnAnInt() { return counter++; }
}
Run Code Online (Sandbox Code Playgroud)
它们用于两个(比方说)服务。一种需要在构造函数中注入接口,另一种需要具体的实现。
Startup.ConfigureServices 方法中用于在两种情况下注入相同实例的尝试和错误:
尝试1:
// only ITestInterface is injected but not TestImplemenation
services.AddSingleton<ITestInterface, TestImplementation>();
Run Code Online (Sandbox Code Playgroud)
尝试2:
//only TestImplementation is injected (DI does not recognize it implements the Interface)
services.AddSingleton<TestImplementation>();
Run Code Online (Sandbox Code Playgroud)
尝试3:
// both are injected but they are not …Run Code Online (Sandbox Code Playgroud) 我拥有的是一个 .NET Core 2.1 Web API 控制器(在下面的 TestController 中),它应该在接收 GET 请求时生成到其他 url 的重定向。
例子:
控制器被称为:http://localhost/api/v1/Test/somedir/somesubdir/filename.extension
它应该返回一个重定向到https://example-domain.com/somedir/somesubdir/filename.extension
我这个问题的示例控制器看起来像:
[Authorize]
[Route("api/v1/[controller]")]
public class TestController : ControllerBase
{
[HttpGet("{path}")]
public async Task<IActionResult> Get(string path)
{
//path e.g. is somedir/somesubdir/filename.extension
string prefix = "https://example-domain.com/api/v1/Other/";
//string path2 = HttpContext.Request.Path.Value.Replace("/api/v1/Test/", "/api/v1/Other/").Replace("%2F", "/");
return Redirect(prefix + path);
}
}
Run Code Online (Sandbox Code Playgroud)
我没有让路由工作。如果我使用 Swagger 调用该方法,它会被调用(斜线被 %2F 替换),但至少它会被调用。如果我通过邮递员 .NET Core 调用控制器,只会返回 404 Not Found。
我不一定需要 HttpGet("{path}")。我知道我可以像分配 path2 变量一样获取路径。
任何提示我如何才能获得正确的路由?
另一种可能的解决方案:
正如 John 和 Kirk Larkin 在评论中指出的那样,我正在寻找的是一个包罗万象的路由,用“somedir/somesubdir/filename.extension”填充路径参数,与之后的路径长度无关。
只需在变量名前加一个星号即可。
[HttpGet("{*path}")]
在我们的项目中,我们有一个数据库,我们在其中使用 DB First 方法。由于我们使用不会被搭建的功能,我有第二个 DBContext,它继承自生成的 DBContext。这使我可以避免每次发生数据库更改时手动操作生成的数据库上下文。
因此 StartUp 中的类定义和用法如下所示:
// the db context generated by scaffolding the database
public partial class ApplicationDatabaseContextGenerated : DbContext
{
public ApplicationDatabaseContextGenerated() {}
public ApplicationDatabaseContextGenerated(DbContextOptions<ApplicationDatabaseContextGenerated> options) : base(options) {}
// the db sets scaffolded
}
// the db context used by the app with the extended functionality
public class ApplicationDatabaseContext : ApplicationDatabaseContextGenerated
{
public ILogger<ApplicationDatabaseContext> Logger { get; protected set; }
public ApplicationDatabaseContext() : base() {}
public ApplicationDatabaseContext(DbContextOptions<ApplicationDatabaseContext> options, ILogger<ApplicationDatabaseContext> logger) : base(options)
{
Logger …Run Code Online (Sandbox Code Playgroud) entity-framework entity-framework-core .net-core asp.net-core-2.1 entity-framework-core-2.1
按照本教程,可以通过命令行使用 Entity Framework Core 构建数据库上下文。
脚手架有一些奇怪的要求
在我当前的项目中,我需要“共享项目”(而不是直接类库)中的模型类。共享项目没有入口点。
(我目前有很多脚手架,因为数据库工程师在数据库优先的方法中更新了很多数据库模型)。
为了创建一些自动化脚手架任务脚本,我计划自动化以下任务:
到目前为止,我设法自动化了第一点。
但我不知道如何将 ItemGroup DotNetCliToolReference 添加到 .csproj 文件的 xml 中。
是否有任何 dotnet cli 命令可以让我添加 DotNetCliToolReference 而不仅仅是包和项目到项目的引用?
请给我任何其他提示?
我知道,还有其他类似的问题,例如将数字转换为特定于文化的
但是我遇到了困难,即使答案对我也不起作用。仍然得到 System.FormatException: '输入字符串的格式不正确。'
我的当前/系统语言环境是 de,我正在解析一个 en int。到目前为止,无论我尝试过什么(包括之前问题的答案,都不起作用。
那么,我错过了什么?
string numberStr = "1,111"; // one thousand one hundred eleven
//int result = int.Parse(numberStr);
//int result = int.Parse(numberStr, CultureInfo.GetCultureInfo("en-US").NumberFormat);
//int result = int.Parse(numberStr, CultureInfo.InvariantCulture);
//int result = int.Parse(numberStr, CultureInfo.InvariantCulture.NumberFormat);
//int result = int.Parse(numberStr, CultureInfo.GetCultureInfo("en-UK").NumberFormat);
int result = int.Parse(numberStr, CultureInfo.GetCultureInfo("en"));
Console.WriteLine(result);
Run Code Online (Sandbox Code Playgroud) 我正在开发一组相当复杂的接口,它允许定义具有特定结构的对象。通过接口和泛型,它还允许我定义每个元素中可用的接口组合。该结构映射了我们背景中的某些内容,因此目标是在代码中重新创建结构,以便更好地理解代码实际操作的内容。
结果如下:
static void Main(string[] args)
{
IComplexDefinition1 CD1 = null;
CD1.Access.Level1.ElementB.SomeStructrueMethod();
CD1.Access.Level1.ElementC.ActorAMethod1();
CD1.Access.Level1.ElementC.ActorBMethod2();
CD1.Access.Level1.ElementC.ActorCMethod1();
CD1.Access.Level2.ElementA.SomeOperationPoolMethod();
CD1.Access.Level2.ElementC.ActorAMethod2();
CD1.Access.Level2.ElementC.ActorBMethod1();
CD1.Access.Level2.ElementC.ActorCMethod2();
}
Run Code Online (Sandbox Code Playgroud)
现在,在实现具体类时,我在 Visual Studio 中单击了“自动实现”,它出现了一行我到目前为止还没有看到的代码(并且没想到会发生这种情况)。因此,我正在寻找它的工作原理以及我可以在哪里阅读/研究更多相关信息的见解。
具体实现的相关部分是:
public class ComplexDefinition : IComplexDefinition1,....
{
protected IActorComposition _actors;
protected SomeStructure _structure;
protected SomeOperationPool _operationPool;
public IElementsBC<IActorCompositionLevel2, SomeStructure> Level1 => this;
public IElementsAC<IActorCompositionLevel1, SomeOperationPool> Level2 => this;
public SomeStructure ElementB => _structure;
public SomeOperationPool ElementA => _operationPool;
public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC => _actors;
}
Run Code Online (Sandbox Code Playgroud)
尤其是这两行让我感到困扰(尤其是最后一行):
public IActorCompositionLevel2 ElementC => _actors;
IActorCompositionLevel1 IElementC<IActorCompositionLevel1>.ElementC …Run Code Online (Sandbox Code Playgroud) 每次我用谷歌搜索或在这里搜索“如何发送电子邮件”时,我都会得到如下结果:使用 smtp 客户端,连接到 smtp 服务器,通过 smtp 服务器发送邮件。
我正在寻找服务器如何将电子邮件发送到其他服务器的代码(或想法)。所以上面链接中的方法对我来说不起作用。
有什么提示 smtp 服务器库会做什么吗?
我正在寻找一种将配置(选项)中的值注入到验证属性的参数中的方法。
让我印象深刻的场景是脚手架身份用户界面。
它提供了配置有关密码长度的选项的可能性。但生成的注册页面上不尊重更改。这是因为页面上验证属性的值是硬编码的。
有人知道这是否可能吗?
.net-core ×6
c# ×6
asp.net-core ×2
dotnet-cli ×1
email ×1
interface ×1
scaffold ×1
scaffolding ×1
smtp ×1