Wouldn’t it be nice if your customers that you sell custom sites to could register themselves and get their own URL automatically, without any intervention on your part? I think that would be cool. Usually you would have to log on to your IIS server as an administrator and open up IIS Manager and add the site bindings there, then you would open up the Hosts file and add an entry for each customer. Well, fasten your seatbelts because here is how we are going to avoid all of this drudgery.
First of all, you’ll want to make a little registration page for your customers where they can enter in their information including the name that they want for the first part of the URL. For example if your customer is George, then he may want the URL to be george.nicesites.com. Ok, now what do you with that piece of information? You can’t just update the bindings, right from the page. You wouldn’t even have permissions to do that. Ok, so imagine a separate application running on the server that can run as administrator and set the bindings. You don’t want to have to log on to the server though, to run the application and leave it running all the time, so maybe a service that runs on the server sounds possible. The best solution is to have your application run only when a customer registers. It would be event driven, so to speak.
Picture this: your customer completes a registration, an event is raised, your application runs and and updates the site bindings. Your customer can immediately go and see his site. This is possible through the Windows event logging in conjunction with the Task Scheduler.
Raising the Event
Create this class to raise an event in your application code.
public class RegistrationEvent
public RegistrationEvent(string EventSourceName)
eventsource = EventSourceName;
public void Raise(string CustomerName)
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = eventsource;
Create an instance of the class and call the raise event after your customer registration is complete.
RegistrationEvent regevent = new RegistrationEvent(“RobertCustomerRegistration”);
Run your application at least once so that you can find the event in your event log and then create a task from it. I found an example of how to do this with pictures here: find-the-event-that-triggered-your-task
Creating an event driven task
Once you find the event in your event viewer, right-click on it and select “Attach A Task to This Event”. Edit the Trigger to begin “On an event”. Export the task to an XML file. Edit the XML file to add the following section after the Subscription element.
Import the task from the XML file with changes. Edit the action to Start a Program. For the Program choose a batch file that you’ll edit later. In the Add arguments field enter the following:
Edit the task to run as the local server administrator.
Updating the bindings
Edit your batch file to contain the following:
Create an MSBuild project file of the same name that contains the following targets:
<Exec Command=”appcmd set site /site.name:nicesites /+bindings.[protocol=’http’,bindingInformation=’$(IPAddress):80:$(DomainAddress)’]” WorkingDirectory=”$(InetSrvDir)”></Exec>
<HostEntry Include=”%0D%0A%09$(IPAddress) $(DomainAddress)”/>
<Target Name=”UpdateHostsFile” DependsOnTargets=”AddBinding”>
<WriteLinesToFile File=”@(HostsFile)” Lines=”@(HostEntry)” Overwrite=”false”></WriteLinesToFile>
Now the full system is in place. Your page writes an event log entry after the registration is complete and then a sceduled task is triggered that runs an MSBuild script that updates the bindings.