ServiceStack-两次添加CORS模块?
加载ServiceStack Api项目时出现异常。以下是ServiceStack输出:ServiceStack-两次添加CORS模块?,cors,servicestack,Cors,servicestack,加载ServiceStack Api项目时出现异常。以下是ServiceStack输出: "startUpErrors": [{ "errorCode": "ArgumentException", "message": "An item with the same key has already been added.", "stackTrace": "[Object: 8/8/2017 6:47:38 PM]:\n[REQUEST: ]\n System.Arg
"startUpErrors": [{
"errorCode": "ArgumentException",
"message": "An item with the same key has already been added.",
"stackTrace": "[Object: 8/8/2017 6:47:38 PM]:\n[REQUEST: ]\n
System.ArgumentException: An item with the same key has already been added.\r\n
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\r\n
at ServiceStack.CorsFeature.Register(IAppHost appHost)\r\n
at ServiceStack.ServiceStackHost.LoadPluginsInternal(IPlugin[] plugins)",
"errors": []
}],
以下是我在AppHost.cs文件中的配置方法:
public override void Configure(Container container)
{
var allowedOrigin = CloudConfigurationManager.GetSetting("CORS.AllowedOrigin");
Plugins.Add(new CorsFeature(allowedOrigins: allowedOrigin, allowedHeaders: "Content-Type,Authorization"));
Plugins.Add(new ValidationFeature());
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
SetConfig(new HostConfig
{
DefaultContentType = MimeTypes.Json,
EnableFeatures = Feature.All.Remove(Feature.Html),
GlobalResponseHeaders =
{
{ "Access-Control-Allow-Origin", allowedOrigin },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH" },
{ "Access-Control-Allow-Headers", "Content-Type,Authorization" },
}
});
var connectionString = ConfigurationManager.ConnectionStrings["BareCove.ConnectionString"].ConnectionString;
var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
container.Register<IDbConnectionFactory>(c => new MultiTenantDbFactory(dbFactory));
PreRequestFilters.Add((req, res) => {
// Handles Request and closes Response after emitting global HTTP Headers
if (req.Verb == "OPTIONS")
{
res.StatusCode = 200;
res.EndRequest();
}
});
GlobalRequestFilters.Add((req, res, dto) =>
{
});
ServiceExceptionHandlers.Add((req, request, exception) =>
{
// Log
//
_exceptionLogger?.Invoke($"Service exception caught in AppHost.cs.", new[] { exception });
// Continue with default error handling
//
return null;
// Or return your own custom response
// return DtoUtils.CreateErrorResponse(request, exception);
});
// Handle unhandled exceptions outside of services
//
UncaughtExceptionHandlers.Add((req, res, operationName, exception) =>
{
// Log. TODO: incorporation operationName into message
//
_exceptionLogger?.Invoke($"Unhandled exception caught in AppHost.cs. Operation: {operationName}.", new[] { exception });
res.Write("Error: {0}: {1}".Fmt(exception.GetType().Name, exception.Message));
res.EndRequest(skipHeaders: true);
});
ConfigureAuth(new Lazy<IDbConnectionFactory>(() => new MultiTenantDbFactory(dbFactory)));
}
public override void Configure(容器)
{
var allowedOrigin=CloudConfigurationManager.GetSetting(“CORS.allowedOrigin”);
添加(新的公司功能(allowedOrigins:allowedOrigin,AllowedHeader:“内容类型,授权”);
Add(新的ValidationFeature());
ServiceStack.Text.JsConfig.casenames=true;
SetConfig(新主机配置)
{
DefaultContentType=MimeTypes.Json,
enableffeatures=Feature.All.Remove(Feature.Html),
全球负责人=
{
{“访问控制允许来源”,allowedOrigin},
{“访问控制允许方法”、“获取、发布、放置、删除、选项、修补程序”},
{“访问控制允许标头”、“内容类型、授权”},
}
});
var connectionString=ConfigurationManager.connectionString[“BareCove.connectionString”].connectionString;
var dbFactory=new或mliteconnectionfactory(connectionString,sqlserverdialent.Provider);
Register(c=>newmulti-tenantdbfactory(dbFactory));
PreRequestFilters.Add((请求,请求)=>{
//处理请求并在发出全局HTTP标头后关闭响应
如果(请求动词==“选项”)
{
res.StatusCode=200;
res.EndRequest();
}
});
GlobalRequestFilters.Add((请求、res、dto)=>
{
});
ServiceExceptionHandlers.Add((请求、请求、异常)=>
{
//日志
//
_exceptionLogger?.Invoke($“AppHost.cs中捕获的服务异常”,新[]{exception});
//继续默认错误处理
//
返回null;
//或者返回您自己的自定义响应
//返回DtoUtils.CreateErrorResponse(请求、异常);
});
//处理服务外部未处理的异常
//
UncaughtExceptionHandlers.Add((请求、恢复、操作名称、异常)=>
{
//Log.TODO:将operationName合并到消息中
//
_exceptionLogger?.Invoke($“AppHost.cs.Operation:{operationName}中捕获的未处理异常”,新[]{exception});
res.Write(“错误:{0}:{1}”.Fmt(exception.GetType().Name,exception.Message));
res.EndRequest(skipHeaders:true);
});
ConfigureAuth(newlazy(()=>newmulti-tenantdbfactory(dbFactory));
}
所以我不止一次注册了CORS插件。或者其他模块正在加载CORS插件
应用程序运行正常,但出现此异常。我只是无法在调试时捕获它,并不止一次地找出它的设置位置
感谢您的帮助。删除
GlobalResponseHeaders
和PreRequestFilters
,它们已经由CorsFeature
插件添加。删除GlobalResponseHeaders
和PreRequestFilters
,它们已经由CorsFeature
插件添加。谢谢@mythz。我想在某一点上,我添加了那些b/c,我在期权调用期间或在未经验证的调用期间使用Aurelia获得CORS错误,标题没有设置。但这起作用了。非常感谢。谢谢@mythz。我想在某一点上,我添加了那些b/c,我在期权调用期间或在未经验证的调用期间使用Aurelia获得CORS错误,标题没有设置。但这起作用了。非常感谢。