我有来自相同接口的服务
public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }
public class ServiceC : IService { }
Run Code Online (Sandbox Code Playgroud)
通常,其他IOC容器Unity允许您通过某些Key区分它们来注册具体实现.
在Asp.Net Core中,我如何注册这些服务并在运行时根据某些键解决它?
我没有看到任何通常用于区分具体实现的AddService方法key或name参数.
public void ConfigureServices(IServiceCollection services)
{
// How do I register services here of the same interface
}
public MyController:Controller
{
public void DoSomeThing(string key)
{
// How do get service based on key
}
}
Run Code Online (Sandbox Code Playgroud)
工厂模式是唯一的选择吗?
Update1
我已经阅读了这里 …
我已经完成了ASP.NET核心的配置文档.文档说您可以从应用程序的任何位置访问配置.
下面是模板创建的Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection …Run Code Online (Sandbox Code Playgroud) appsettings我的.Net核心应用程序中有3个特定于环境的文件
在project.json我这样设置publishOptions.(根据此处的建议)
"publishOptions": {
"include": [
"wwwroot",
"appsettings.development.json",
"appsettings.staging.json",
"appsettings.production.json",
"web.config"
]
},
Run Code Online (Sandbox Code Playgroud)
我有3个相应的启动类,它们使用适当appsettings的环境
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
Run Code Online (Sandbox Code Playgroud)
但是,当我发布应用程序时,所有3个appsettings文件都会在所有环境中结束.如何发布特定于环境的appsetting文件?
我有ASP.NET核心应用程序(Web Api).该文档解释了如何使用多个环境,但是它无法解释aspnetcore_environment在发布网站时如何设置.
因此,可以说,如果我有3个环境Development,Staging和Production
在经典的ASP.NET Web应用程序中,我用来创建3个构建配置.Development,Staging和Production(如下图所示).然后是3个.pubxml文件,每个配置一个.我是否也需要使用相同的方法进行ASP.NET Core应用?
如何设置aspnetcore_environment的.pubxml文件?
如果问题1中指定的方法已经过时,那么替代方法是什么?(我使用Jenkins进行CI)
更新1
我知道我必须设置ASPNETCORE_ENVIRONMENT但是我无法理解我们在哪里设置它?在开发过程中,我可以在配置文件中设置它launchSettings.json,但问题是我们如何在发布到分段或生产时设置它?我们在目标服务器上设置环境变量吗?
更新2
我在这里找到了解释设置环境变量的不同方法的文章.这部分回答了我的问题.但是,当我发布应用程序时,发布过程不会在发布时遵循环境变量appsettings.{env.EnvironmentName}.json
我已为该问题创建了单独的帖子
configuration .net-core coreclr asp.net-core asp.net-core-1.0
我有asp.net核心应用程序.我想使用IOptions模式从appsettings.json注入值.所以我有一个类SecurityHeaderOptions,并且还有目标类,SecurityHeadersBuilder其构造函数IOptions<SecurityHeaderOptions>作为参数.
我知道.net核心可以在用容器注册后SecurityHeadersBuilder通过注入隐式创建实例IOptions<SecurityHeaderOptions>.
但是,我想显式创建实例SecurityHeadersBuilder,调用其方法之一,然后使用容器注册实例.
public sealed class SecurityHeaderOptions
{
public string FrameOption { get; set; }
public string XssProtection { get; set; }
}
public class SecurityHeadersBuilder
{
private readonly SecurityHeaderOptions _options = null;
public SecurityHeadersBuilder(IOptions<SecurityHeaderOptions> options)
{
_options = options.Value;
}
public SecurityHeadersBuilder AddDefaultPolicy()
{
AddFrameOptions();
AddConetntSecurityPolicy();
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
ConfigureServices方法
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SecurityHeaderOptions>(Configuration.GetSection("SecurityHeaderOptions"));
services.AddScoped<SecurityHeadersBuilder>(provider =>
new SecurityHeadersBuilder(?????).AddDefaultPolicy())
}
Run Code Online (Sandbox Code Playgroud)
问题
1>如果我明确地将选项传递给构造函数,我是否需要SecurityHeaderOptions …
我在.Net 4.6.2中有这个代码,现在尝试转换为.Net核心但是我收到了错误
错误CS1061'Type'不包含'IsGenericType'的定义,也没有扩展方法'IsGenericType'接受类型'Type'的第一个参数(你是否缺少using指令或汇编引用?)
public static class StringExtensions
{
public static TDest ConvertStringTo<TDest>(this string src)
{
if (src == null)
{
return default(TDest);
}
return ChangeType<TDest>(src);
}
private static T ChangeType<T>(string value)
{
var t = typeof(T);
// getting error here at t.IsGenericType
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
}
Run Code Online (Sandbox Code Playgroud)
什么相当于.Net Core?
UPDATE1
令人惊讶的是,当我调试代码时,我看到变量t有IsGenericType 属性,但我不能IsGenericType在代码中使用.不确定我需要添加的原因或名称空间.我已经加入using System和using System.Runtime两个命名空间
我有 AWS step machine,其中一个 step 用于使用 SNS 服务通知失败。我想从inputjson 中选择一些元数据到传出消息中。所以我试图将常量字符串与 jsonpath 连接起来,如下所示
"Notify Failure": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"Message.$": "A job submitted through Step Functions failed for document id $.document_id",
"Subject":"Job failed",
"TopicArn": "arn:aws:sns:us-west-2:xxxxxxx:xxxxxxxx"
},
"End": true
}
Run Code Online (Sandbox Code Playgroud)
document_id输入 json 中的属性之一在哪里
但是,当我尝试保存状态机定义时,出现错误
您的 ASL 定义有问题,请检查并重试 'Message.$' 字段的值必须是有效的 JSONPath
如何在Windows 10机器上使用IIS8注册.Net 4.5.1?它曾经像运行命令"aspnet_regiis -i"一样简单......但不再是了.如果尝试运行此命令,我会收到错误
此版本的操作系统不支持此选项.管理员应该使用"打开/关闭Windows功能"对话框,服务器管理器管理工具或dism.exe命令行工具,使用IIS8安装/卸载ASP.NET 4.5.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=216771.完成安装ASP.NET(4.0.30319.0).
另外在windows 10中的"打开和关闭窗口功能"窗口下,为什么我看不到.net 4.5或4.5.1?我确实看到了4.6.和4.5.1一样吗?
(注意我确定我的机器上安装了4.5.1因为visual studio允许我在Web应用程序上设置目标框架4.5.1并且我有几个应用程序在4.5.1上运行而没有任何问题)
在ASP.NET Core应用程序中,我有一个返回一些数据的action方法.我想在客户端缓存这些数据.所以根据这里的文档,我可以使用ResponseCacheaction方法的属性.此属性Cache-Control在响应中添加标头
响应缓存是指在ASP.NET Core MVC操作所做的HTTP响应上指定与缓存相关的标头.这些标头指定了您希望客户端和中间(代理)计算机如何缓存对某些请求的响应(如果有的话).这可以减少客户端或代理对Web服务器的请求数,因为可以从客户端或代理的缓存提供对相同操作的未来请求.
也
响应缓存不会缓存Web服务器上的响应.它与输出缓存不同,后者会在早期版本的ASP.NET和ASP.NET MVC中缓存服务器内存中的响应.
所以这就是我的动作方法的样子
public class LookupController : Controller
{
[HttpGet]
[ResponseCache(Duration = 120)]
public IEnumerable<StateProvinceLookupModel> GetStateProvinces()
{
return _domain.GetStateProvinces();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用浏览器调用方法http:// localhost:40004/lookup/getstateprovinces 这是请求和响应头
请注意,响应标头Cache-Control: public,max-age-120符合预期.但是,如果使用F5刷新页面(120秒之前),则GetStateProvince操作方法中的调试器断点总是会命中.这意味着它不会破坏客户端的数据.
我还需要做些什么来启用客户端缓存吗?
更新 我尝试过使用IE,Chrome和POSTMAN但没有运气.每次我在地址栏中输入网址或点击刷新时,客户端(即浏览器或邮递员)都会调用操作方法.