Hi hikalkan,
When i upgrade my project to abp 0.8.0.1, now i got the following exception:
{
"message": "An error has occurred.",
"exception_message": "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.",
"exception_type": "System.InvalidOperationException",
"stack_trace": " 在 System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()\r\n 在 System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)\r\n 在 System.Web.Http.HttpRouteCollection.GetRouteData(HttpRequestMessage request)\r\n 在 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 在 Abp.WebApi.Controllers.ResultWrapperHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 在 System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()"
}
I have see the issue[https://github.com/aspnetboilerplate/aspnetboilerplate/issues/850], it have fixed by this update, why do I have this error?
Hi,
Can you try to add this in your web module's postinitialize code:
Configuration.Modules.AbpWebApi().HttpConfiguration.EnsureInitialized();
Also, where is your route definition?
I had already added ensureinitialized method in module's postinitialize, but it didn't work.
Following is my code:
Startup.cs
[assembly: OwinStartup(typeof(Startup))]
namespace Sparxo.Platform.WebApi
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseAbp();
app.Use((context, next) =>
{
var lang = context.Request.Query.Get("lang");
var currentLang = "en";
if (!string.IsNullOrEmpty(lang))
{
currentLang = lang;
}
try
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLang);
}
catch (Exception) { }
return next.Invoke();
});
ConfigureAuth(app);
ConfigureWebApi(app);
}
}
}
Startup.WebApi.cs
public partial class Startup
{
public static HttpConfiguration HttpConfig = new HttpConfiguration();
public void ConfigureWebApi(IAppBuilder app)
{
//var config = new HttpConfiguration();
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
HttpConfig.SuppressDefaultHostAuthentication();
HttpConfig.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
//config.Filters.Add(new OAuthAttribute());
// Web API routes
HttpConfig.MapHttpAttributeRoutes();
HttpConfig.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var formatter = HttpConfig.Formatters.OfType<JsonMediaTypeFormatter>().First();
formatter.SerializerSettings.ContractResolver = new CustomPropertyNamesContractResolver();
app.UseWebApi(HttpConfig);
}
}
module
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
//var abpExceptionFilter = Startup.HttpConfig.Filters.FirstOrDefault(f => f.Instance.GetType() == typeof(AbpExceptionFilterAttribute)).Instance;
//Startup.HttpConfig.Filters.Remove(abpExceptionFilter);
//Startup.HttpConfig.Filters.Add(IocManager.Resolve<PlatformExceptionFilterAttribute>());
Startup.HttpConfig.Filters.Add(IocManager.Resolve<OAuthFliter>());
}
public override void PreInitialize()
{
Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
Configuration.Localization.Sources.Add(
new DictionaryBasedLocalizationSource(
"PlatformWebApi",
new XmlFileLocalizationDictionaryProvider(
HttpContext.Current.Server.MapPath("~/Localization/PlatformWebApi")
)
)
);
Configuration.Modules.AbpWebApi().HttpConfiguration = Startup.HttpConfig;
Configuration.Modules.AbpWebApi().HttpConfiguration.EnsureInitialized();
ConigPlatformRedis();
ConigPlatformCore();
}
I have tried to register route in global.asax
public class Global : AbpWebApplication
{
protected override void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(App_Start.WebApiConfig.Register);
IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config"));
base.Application_Start(sender, e);
}
}
then it can work.
@hikalkan
Do you know why can not register webapi's route by owin's startup file?
like this
public partial class Startup
{
public static HttpConfiguration HttpConfig = new HttpConfiguration();
public void ConfigureWebApi(IAppBuilder app)
{
//var config = new HttpConfiguration();
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
HttpConfig.SuppressDefaultHostAuthentication();
HttpConfig.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
//config.Filters.Add(new OAuthAttribute());
// Web API routes
HttpConfig.MapHttpAttributeRoutes();
HttpConfig.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var formatter = HttpConfig.Formatters.OfType<JsonMediaTypeFormatter>().First();
formatter.SerializerSettings.ContractResolver = new CustomPropertyNamesContractResolver();
app.UseWebApi(HttpConfig);
}
}
Hi,
Probably it will work if you move route configuration to Initialize event of your web module, before MVC route registration (like this in startup template: https://github.com/aspnetboilerplate/aspnetboilerplate-templates/blob/master/src/AbpCompanyName.AbpProjectName.WebSpaAngular/App_Start/AbpProjectNameWebModule.cs#L47). Can you try this?
Web API route should be defined before MVC route since MVC route is more generic and suppress the web api route. Since module initialization runs before owin startup, you can not put it into owin startup.
Mine is a Web API module not MVC module.
So i can not use owin startup to register Web API route?
Then you should be able to use it in owin startup. let me check it. for now, continue with the workaround.
thanks.
I'm interested in your investigation . I would like to create an web app with with just web API controllers with owin and without global.asax file.
Thank you
I discovered that if there is a problem in route configuration attributes, Web Api gives this error, it is very misleading. If you put config.EnsureInitialized(); end of your StartUp.Configuration() method, you can see the real underlying exception in a debug session.
Why don't you use GlobalConfiguration and creating new HttpConfiguration?
You can also get the HttpConfiguration object used by ABP. In the Startup class;
var httpConfiguration = Abp.Dependency.IocManager.Instance.Resolve<IAbpWebApiModuleConfiguration>().HttpConfiguration;
Do not create a new HttpConfiguration.
+1 @Serguzest
I found my problem was due to route attribute configuration. calling config.EnsureInitialized() in Startup causes the server to blow up before it runs. Thanks!
@hikalkan owin need new HttpConfiguration。
see here。
If you are self-hosting with OWIN, create a new HttpConfiguration instance. Perform any configuration on this instance, and then pass the instance to the Owin.UseWebApi extension method.
Then what about that:
Your module's PreInitialize method:
C#
Abp.Dependency.IocManager.Instance.Resolve<IAbpWebApiModuleConfiguration>().HttpConfiguration = new HttpConfiguration();
Startup.cs
````C#
var httpConfiguration = Abp.Dependency.IocManager.Instance.Resolve
...use httpConfiguration
appBuilder.UseWebApi(httpConfiguration);
````
then i get a error:
{"message":"An error has occurred.","exceptionMessage":"The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.","exceptionType":"System.InvalidOperationException","stackTrace":" \u5728 System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()\r\n \u5728 System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)\r\n \u5728 System.Web.Http.HttpRouteCollection.GetRouteData(HttpRequestMessage request)\r\n \u5728 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n \u5728 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n \u5728 Abp.WebApi.Controllers.ResultWrapperHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) \u4f4d\u7f6e D:\\Halil\\Github\\aspnetboilerplate\\src\\Abp.Web.Api\\WebApi\\Controllers\\ResultWrapperHandler.cs:\u884c\u53f7 0\r\n \u5728 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n \u5728 System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- \u5f15\u53d1\u5f02\u5e38\u7684\u4e0a\u4e00\u4f4d\u7f6e\u4e2d\u5806\u6808\u8ddf\u8e2a\u7684\u672b\u5c3e ---\r\n \u5728 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n \u5728 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n \u5728 System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()"}
config.EnsureInitialized();
whatever I put it in PreInitialize method or Startup, it does not work
Okay. After that line, add:
config.EnsureInitialized();
On Jan 18, 2017 19:37, "ColinZeb" notifications@github.com wrote:
then i get a error:
{"message":"An error has occurred.","exceptionMessage":"The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.","exceptionType":"System.InvalidOperationException","stackTrace":" u5728 System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()rn u5728 System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)rn u5728 System.Web.Http.HttpRouteCollection.GetRouteData(HttpRequestMessage request)rn u5728 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn u5728 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn u5728 Abp.WebApi.Controllers.ResultWrapperHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) u4f4du7f6e D:\Halil\Github\aspnetboilerplate\src\Abp.Web.Api\WebApi\Controllers\ResultWrapperHandler.cs:u884cu53f7 0rn u5728 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn u5728 System.Web.Http.Owin.PassiveAuthenticationMessageHandler.
d__0.MoveNext()rn--- u5f15u53d1u5f02u5e38u7684u4e0au4e00u4f4du7f6eu4e2du5806u6808u8ddfu8e2au7684u672bu5c3e ---rn u5728 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn u5728 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn u5728 System.Web.Http.HttpServer. d__0.MoveNext()"} —
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/872#issuecomment-273657113,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGpYcAaHeJ9wwxLiaGlEynXmFuIlU545ks5rTr5wgaJpZM4HbCfP
.
I have tried. it does't work
May be caused by this
config.MapHttpAttributeRoutes();
Did you call EnsureInitialized as last in Startup (after resolving from Abp.Dependency.IocManager.Instance)?
sure,but if comment config.MapHttpAttributeRoutes(),then it's ok. so why? why i can't use attribute route?
@hikalkan don't support attributeroutes?
I created a demo project to reproduce some of the problems,
review here
Thanks. We'll check it.
Who knows how this question is now?
We'll check this in this bugfix release.
Hi @ColinZeb,
Can you try to add below code in your web api module's preInitialize just before base.PreInitialize();
Configuration.Modules.AbpWebApi().HttpConfiguration.MapHttpAttributeRoutes();
@ismcagdas Well, that's ok.
But how do I determine PreInitialize or Startup.cs at the time of configuration?
Normally such configurations are done in Startup, but since ABP offers a modular architecture, it is better to configure each module in itself.
So, If ABP has a configuration for your need, it is better to do it in ABP module, otherwise in Startup.
Of course, you cannot know everything but you can search on our github repo or on our forum before proceed.
Please close the issue if it's resolved?
@ismcagdas understood,thanks a lot.
@hikalkan yes, it's resolved,but i'm not owner of this issue,please you close it.thanks
excuse me, here I have a suggestion, my issue caused by ABP, then I hope ABP can wrap or handle this exception information, rather than throw an exception with unknown meaning . Such as InvalidOperationException:valuefactory attempted to access the value property of this instance。
Such basic exceptions are difficult to analyse and solve.
Hi @ColinZeb,
The error message "InvalidOperationException:valuefactory attempted to access the value property of this instance" is related to OData I think. Since you didn't configure Odata with ABP, the exception is not thrown by ABP in this project https://github.com/ColinZeb/OwinTest/. By the way, thank you for that demonstration, it helped a lot :).
I have applied the stes
Can you try it ? If it does not work for you, please open another issue for that.
Thanks.
Most helpful comment
I discovered that if there is a problem in route configuration attributes, Web Api gives this error, it is very misleading. If you put config.EnsureInitialized(); end of your StartUp.Configuration() method, you can see the real underlying exception in a debug session.