ASP.NET/ServiceStack Root URL at startup - asp.net

I'm trying to setup a ServiceStack template loosely based on the existing ASP.NET with razor template. The services to be created using this will be hosted in a variety of locations. What I would like is for them to be able to register themselves with a central server.
What I was hoping to do was to add some code to Application_start (or apphost) which would perform the registration however I can't find any way of getting the root url of the application. The normal method of using the request object doesn't work as there isn't a request object at that point.
If I can't get this from asp.net I'm wondering if there is a servicestack call I can make which can give me what I need

The URL for where an ASP.NET Web Application is hosted at is only available at runtime, inferred from the incoming are Request URL, so you won't be able retrieve it at Startup.

Related

How to Create a URL to an mvc Action in a Different WebForm Project in the Same Solution?

I have two projects one asp.net mvc and one webform in the same solution. You would know if you can use both the web projects. Specifically, it would be able to use some of the action mvc project in the webform project.
Routing is project-specific. Regardless of whether your projects share the same solution, there's no easy way to generate a URL from one project in another. This is mostly due to the fact that other factors play into what URLs are generated by project other than just the routes it defines, such as virtual directories, domains, etc. These things are only known by the specific project while it is running.
As a result, the only way to truly get the URL for a route from a project is to get it from that project, while it is running. That means, you need to set up some sort of endpoint that you can send an HTTP request to, which would return an appropriate URL. Then, in your other project, you would have to use something like HttpClient to issue a request to that endpoint with whatever information it would need to generate the URL.

Accessing Sitecore API from a Web service

I am wondering how to get to the Sitecore content through the Sitecore API in a separate webservice, I tried SiteContextSwitcher but I get a "Cannot load provider" Exception.
Should I somehow register this webservice in sitecore to use it ?
EDIT:
I would like to expose over WebService functions to publish specific items (by GUID or path)
You can use Sitecore Web APi module to get Sitecore Content .
Here you can find it.
I think you don't need your own WebService, with this module you can get Sitecore content out of the box. It's really easy to use it.
You can add web service to your website where you will have access to the Sitecore API and expose functions you need eg. publishing a page
You will need an ASMX service which you can call by URL to the file ie. http://server.com/Some/Directory/Service.asmx/YourMethodName
Since it's called in the context of the Sitecore application you will have access to the API in the service. You can pass the path to the item or the id using parameters.

Hiding http file extension without using MVC controller

I am using basic asp.net web application project template, because I want to move away from MVC into SPA.
Most of my pages will just be basic html files that will interact with the server through ajax calls. That said I want to hide the .html extension, but I don't want to create controllers just to hide this, which is too much of an overkill.
Of another note, I am using Azure as well, so setting this up on IIS directly is not going to work, as I would not be able to scale the administration nicely.
So how can I hide the html extensions without such a heavy layer as an MVC controller?
This sounds like a job for Url Routing
Url Routing allows you to intercept a request and then determine how to service it. It is how MVC does it and has many other useful benefits. If the router isn't able to service it, it falls back to the default ASP.NET pipeline processing, and then IIS.

Separate httpmodule(s) in web.config for a WCF web service within an ASP.NET website

I've got a WCF (RESTful) web service within an ASP.NET website project. The service resides at /WebServices/Registration.svc. I'm trying to create a custom BasicAuthenticationModule httpModule to handle Basic Auth as explained at https://stackoverflow.com/a/4729555/291323, but the module is being used for my entire website, which I do not want.
I tried using the tag to define the module for ONLY the service, but couldn't get it to work. I tried setting the path to both "WebServices/Registration.svc" as well as just "WebServices".
Is there a better way to register an httpModule to affect ONLY a WCF webservice within a website project?
TIA for any assistance!

nhibernate session created on every web request in asp.net MVC2

I have an ASP.net MVC2 application that is using NHibernate for data access. On every request, even static file requests (images, javascript), a new session is getting created. So for a single view where I'm returning a list, I'm creating around 15 session that don't load anything.
is there way to only create sessions when they are required?
I'm currently using Castle.Windsor to inject the session into my Controllers.
Is there a way to filter out static file requests?
It sounds like you need to exclude those paths in your routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// add these lines
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("{resource}.js");
}
Although if you ask me, a request for a static file shouldn't be instantiating a controller. You might want to take a look at your code and figure out why it's doing that.
Also I figured it out that this isn't a configuration with NHibernate or MVC. It is that ASP.net development server services every request through ASP.net. IIS will not send static file requests through ASP.net unless configured.
From Here (http://www.asp.net/hosting/tutorials/core-differences-between-iis-and-the-asp-net-development-server-cs)
Another core difference between IIS
and the ASP.NET Development Server is
how they handle requests for static
content. Every request that comes into
the ASP.NET Development Server,
whether for an ASP.NET page, an image,
or a JavaScript file, is processed by
the ASP.NET runtime. By default, IIS
only invokes the ASP.NET runtime when
a request comes in for an ASP.NET
resource, such as an ASP.NET web page,
a Web Service, and so forth. Requests
for static content - images, CSS
files, JavaScript files, PDF files,
ZIP files, and the like - are
retrieved by IIS without the
involvement of the ASP.NET runtime.
how are you creating the session, as the .jpg should not ask for an instance of ISession, therefor castle will not create one (from my understanding), could you post the castle setup, and how have you implemented it as a factory?
things to look out for
Make sure your session factory is a singleton
Create a session as PerWebRequest, using the OpenSession from the SessionFactory
//Setup the Hibernate dependencies
container.AddFacility<FactorySupportFacility>().Register(
Component.For<ISessionFactory>().LifeStyle.Singleton
.Instance(NHibernateHelper.GetSessionFactory()),
Component.For<ISession>().LifeStyle.PerWebRequest
.UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession())
);
once you have registerd castle with MVC as a controller factory, it should only create a single session PerWebRequest and only if the controller, or its dependants are dependent on it
I have a sample app if it helps
I use Spring.NET, not Castle Windsor, but I assume the concepts are the same. The scope of your ISession should be defined as per request, and shouldn't actually be created until asked for, as dbones says. While MVC handles every request, asking for a .jpg shouldn't hit a controller which depends on ISession.
Is Castle Windsor eagerly creating per request objects instead of on demand? That may be a configuration issue. Or do you have a custom module, handler or a Global.asax application method that is asking Windsor for an ISession?

Resources