Automated Site Binding for Custom URLs

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
{
string eventsource;

public RegistrationEvent(string EventSourceName)
{
eventsource = EventSourceName;
if (!System.Diagnostics.EventLog.SourceExists(eventsource))
{
System.Diagnostics.EventLog.CreateEventSource(eventsource, “Application”);
}
}

public void Raise(string CustomerName)
{
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = eventsource;
log.WriteEntry(CustomerName, System.Diagnostics.EventLogEntryType.Information);
}
}

Create an instance of the class and call the raise event after your customer registration is complete.

RegistrationEvent regevent = new RegistrationEvent(“RobertCustomerRegistration”);
regevent.Raise(“whatsaco”);

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.

<ValueQueries>
<Value name=”eventData”>Event/EventData/Data</Value>
</ValueQueries>

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:

$(eventData)

Edit the task to run as the local server administrator.

Updating the bindings

Edit your batch file to contain the following:

set CustomerName=%1
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe RegisterCustomer.proj

Create an MSBuild project file of the same name that contains the following targets:

<Target Name=”AddBinding”>
<Exec Command=”appcmd set site /site.name:nicesites /+bindings.[protocol=’http’,bindingInformation=’$(IPAddress):80:$(DomainAddress)’]” WorkingDirectory=”$(InetSrvDir)”></Exec>
</Target>

<ItemGroup>
<HostsFile Include=”$(System32Directory)drivers\etc\hosts”/>
<HostEntry Include=”%0D%0A%09$(IPAddress) $(DomainAddress)”/>
</ItemGroup>
<Target Name=”UpdateHostsFile” DependsOnTargets=”AddBinding”>
<WriteLinesToFile File=”@(HostsFile)” Lines=”@(HostEntry)” Overwrite=”false”></WriteLinesToFile>
</Target>

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.

Advertisements
Tagged with:
Posted in Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: