1
加载我的ServiceStack Api项目时出现异常。下面是ServiceStack输出:ServiceStack - 两次添加CORS模块?
"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)));
}
所以,在某种程度上,我注册CORS插件不止一次。或者其他一些模块正在加载CORS插件。
应用程序运行得很好,这个例外。我在调试时无法捕获它,并找出它不止一次被设置的位置。
感谢任何见解。
谢谢@mythz。我认为在OPTIONS调用期间或者在未经认证的调用期间,使用Aurelia获取CORS错误时,我添加了那些b/c标题没有被设置。但是这工作。非常感激。 –