ASP.NET Web API with Linq2Couchbase dependancy injection - c#

I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?

Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.

Related

Error : HttpContext.Current is null. |PerWebRequestLifestyle | Castle Windsor | Threading

I have WCF service and inside this service using MEF i am calling runtime class library.
I have implemented DI(castle Windsor) in this class library and it work ok in all request but instantly some time it return error like below
"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net"
We have apply all internet solution like install Asp.net feature, change DI Lifecycle from LifestylePerWebRequest to scope , hibridgeLifeStyle etc
But not found proper result below is my code
Can any one plese help me solve this issue. As i am new in DI and don't this is Life cycle issue or MEF issue or threading issue because i am calling this class libreary in to thread
Thanks in advance
You need to run WCF in ASP.Net compat mode to get access to HttpContext. Not sure why you need it though because then you can only run the WCF services on IIS in asp.net compat mode and not within a different hosting environment like a windows service.
more information here: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet which also tells you about contexts you should be using instead of HttpContext

OrchardCore Localization in Asp.Net Core - AddViewLocalization error

I am trying to insert the localization using PO file. My project is on Asp.Net core and uses Orchard core.
I am following this guide - Configure Portable Object but I have a problem with the initial registration of the localization.
I should add the following code:
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
Technically, my project should have service.AddMvc() by default, but I am using services.AddOrchardCms() instead.
When I try to call the first code to register the localization:
...
services.AddOrchardCms();
services.AddMvc()..AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
...
I receive an error as the application "Can not find the index page" (It actually does not exist as I am using services.AddOrchardCms() and I think they have a conflict).
And, of course, if I don't insert the AddViewLocalization() the PO files don't work.
Does anyone know how can I solve this problem?
AddOrchardCms is internally calling AddMvc, by calling it again you are overwriting Orchard pipline with basic MVC pipeline. Try calling:
services
.AddOrchardCms()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
instead.

asp net core 1 RC2 AccountController injection

I created a asp.net core rc2 web application with user identity, however i'm confused how the account controller class is getting it's arguments, usermanager, signinmanager? Where are they being passed in from? I follow the call stack and I get external code, what external code is passing in these objects? Help me understand, how these 2 objects are being initialized.
In your Startup.cs you will see a call to this method
services.AddIdentity<ApplicationUser, IdentityRole>()
Afte reading the links on dependency injection suggested by #AndrésRobinet you can actually see where the services are being wired up.
This extension method lives in `IdentityServiceCollectionExtensions - You can then go and look at the source code for this method call (.NET core is on github):
line 67 of the AddIdentity method
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
what external code is passing in these objects?
Right-click on External code and click Show External Code - now you can get an idea of what is happening under the hood. the code down to and including the Kestrel webserver is also browsable/downloadable on github
image truncated

LINQPad over MVC4 WebApi

I have prepared some code that builds a collection. I would like to use this collection in LINQPad for various queries.
As LINQPad allows WCF OData I thought that MVC4 WebApi would be perfect for this.
Unfortunatelly I wasn't able to make this work even with the template WebApi project.
When I try to add service to LINQPad I get "Data at the root level is invalid. Line 1, position 1.
I think the problem is that LINQPad doesn't use content negotiation and it is expecting xml but WebApi returns json.
Unfortunatelly Fiddler wasn't able to monitor the requests.
Anyone know how to fix this?
LinqPad does not have the REST drivers by default to query a Web API service. See: http://forum.linqpad.net/discussion/199/linqpad-error-when-access-odata-using-webapi
However, the following blog post by Filip W. speaks to this issue and how you can create a custom ControllerResolver that overrides the default resolver and allows running Web API from LinqPad. Have a look:
Hosting ASP.NET Web API in LinqPad
This post follows the same question: Using WebAPI in LINQPad?
why don't you just use the HttpClient class? HttpClient

Restfull urls for ASP.NET page on IIS

Ok I had a huge Issue giving this a proper title, my excuses for that.
Anyways I have started slowly to look at Web and ASP.NET again, I am a C# developer but I have mostly worked with Windows applications the past 5 years or so, It is not that I haven't touched the web as such in that time, but this is as web services (Restfull as well as the ugly SOAP services) I have also worked with more "raw" web requests.
But I have not worked with IIS or ASP.NET in all that time.
What I would like to do is hos a web page that uses a URL style I could best describe with "like rest", hence the "Restfull urls" title. Because I think most people thinks of such URL's in terms of:
http://example.com/item/
http://example.com/item/23/
and so forth. Not that they have to look like that, however I would like to use such URL's instead of
http://example.com/item?id=23
I know subtext does this, but i have not had any luck finding it in their code base.
Now as far as I can tell I could just implement some IHttpHandler's, but at least for the examples I have seen of that, they write the page source back in code, and I still have master pages etc. I wish to use instead of taking over all that stuff my self, I really just kinda wants to route http://example.com/item/23/ to http://example.com/item and asking for the item with id 23...
I hope this makes sense at all >.<... And that someone has some better examples at hand that what I have been able to find.
You can achieve this using Routing here is a link to an MSDN blog, The .Net Endpoint - Using Routes to Compose WCF WebHttp Services that should get you started.
If you're looking at asp.net/IIS, another option to look at is ASP.Net MVC. It's pretty straight forward to create RESTful services.
Here's a tutorial:
http://www.codeproject.com/Articles/233572/Build-truly-RESTful-API-and-website-using-same-ASP
So here are your options-
For .net 3.5 sp1 framework with IIS7 you can use asp.net routing feature to have MVC style urls that you mentioned should create a custom route handler implementing IRouteHandler interface as explained here How to: Use Routing with Web Forms and register your route rules in Application_Start method in Global.asax. For your example you can register a route like this
routes.Add("ItemRoute", new Route
(
"item/{itemId}",
new CustomRouteHandler("~/item.aspx")
));
and then you can access itemId in your routed item.aspx page by checking request context item
requestContext.HttpContext.Items["itemId"]
For .net framework 4 MVC you dont have to create a custom handler, you can directly use
routes.MapPageRoute("ItemRoute", "item/{itemId}", "~/item.aspx");
in you global.asax Application_Start method.
This link explains more about the Routing
A way of achieve this is using URL rewriting.
If you're planning to host your Web application in Internet Information Services 7.x, you can take advantage of IIS URL Rewriting Module:
http://www.iis.net/download/urlrewrite
URL rewriting is just mapping a friendly URL to an unfriendly, common one, which is programming-friendly to inspect GET parameters.
For example:
http://yourdomain.com/item/48 => http://yourdomain.com/Items.aspx?Id=48

Categories

Resources