Для лучшего тестового задания с Microsoft.Owin.Testing.TestServer
я обнаружил, что Global.asax не загружен Owin TestServer.
Итак, я пытаюсь переместить мои конфигурации Global.asax в Startup.cs, как показано ниже,
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
// pasted Global.asax things start.
GlobalConfiguration.Configuration.Formatters.Clear();
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// pasted Global.asax things end.
ConfigureAuth(app);
}
}
Но TestServer
не удалось инициализировать в каждой точке конфигурации, такой как AreaRegistration.RegisterAllAreas
, FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
и так далее...
Минимальная жизнеспособная миграция (успешный тест с TestServer) для меня таков, как показано ниже.
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.Formatters.Clear();
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
config.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
config.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());
WebApiConfig.Register(config); // moved from GlobalConfiguration.Configure(WebApiConfig.Register)
app.UseWebApi(config);
ConfigureAuth(app);
}
}
В любом случае, чтобы переместить все конфигурации в Startup.cs?