Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

December 2011 - Posts

ASP.NET MVC: Helper method to display date ranges

I have events web site where I want to show events start and end time to visitors. I wrote simple extension method called DisplayTimeRange() to display event time range on user-friendly manner. My goal is to show times as 01.01.2012 10:00 – 14:00 and 01.01.2012 15:00 – 01.03.2012 18:00. It’s practically displaying time ranges is shortest possible way. In this posting I will show you how to do it using extension method.

For events that happen only on one date I want output like this:

DisplayDateRange(): Event covers only one date

For events that cover more than one date I want output like this:

DisplayDateRange(): Event covers more than one date

Here is my extension method for HtmlHelper. I called it DisplayDateRange and currently it generates output like shown above (styling depends on you).


public static MvcHtmlString DisplayDateRange(this HtmlHelper helper, DateTime from, DateTime to)

{

    var buffer = new StringBuilder(100);

 

    buffer.Append(@"<div class=""dateRange"">");

 

    buffer.Append(from.ToShortDateString());

    buffer.Append(" ");

    buffer.Append(from.ToShortTimeString());

    buffer.Append(" - ");

 

    if (from.Date == to.Date)

    {

        buffer.Append(to.ToShortTimeString());

    }

    else

    {

        buffer.Append(to.ToShortDateString());

        buffer.Append(" ");

        buffer.Append(to.ToShortTimeString());

    }

 

    buffer.Append("</div>");

 

    return new MvcHtmlString(buffer.ToString());

}


This extension method can be easily extended also to nullable DateTime types if you need it.

DateTime formatting may seem not so perfect but it was the only way how to avoid weird long date and time outputs with seconds (we really don’t need seconds here). If you have good idea about how to format dates shorter in code then feel free to drop me a comment here.

Posted: Dec 31 2011, 11:53 AM by DigiMortal | with 7 comment(s)
Filed under: ,
Windows Azure error: An unsupported response was received. The response header 'MSDeploy.Response' was '' but 'v1' was expected.

You may get the following error when deploying your web application from Visual Studio to Windows Azure using WebDeploy:

Remote agent could not be contacted. Make sure the remote agent service is installed and started on the target computer.

An unsupported response was received. The response header 'MSDeploy.Response' was '' but 'v1' was expected.
The request was aborted: The request was canceled.
COM object that has been separated from its underlying RCW cannot be used.

If you monitor your HTTPS traffic you can find out that response to WebDeploy request was error 401 (not authenticated). Although I don’t know what is the exact source of this error there is simple solution: close Visual Studio and then run it again. Now you should be able to deploy your application.

The selected database contains foreign keys that create a cycle

You may get the following error when creating data scripts with MSSQL 2008: The selected database contains foreign keys that create a cycle. Publishing data only is not supported for databases with cyclical foreign key relationships. This problem is caused by self-referencing tables and it is given even if you don’t export data from one of such tables. Here is simple and very dirty solution.

If some of tables in the list of exported tables has circular references or some of these tables refers to table with circular references then this error occurs. As a very dirty workaround you can temporarily remove circular foreign keys and create them later again. Not very nice solution but works for me.

Deploying independent web applications to Windows Azure using single web role

I found very good blog post by Andy Cross’ Blog where he describes how to deploy multiple web sites in a single Windows Azure web role. I like the idea but there are some things that should be understood and done differently if you plan to use same web role for totally different web applications that you want to run on your Windows Azure instance. In this posting I will show you how to deploy independent web applications to Windows Azure instance using single web role.

Problems with independent sites

If your sites are in same solution or they are related to each other then use the solution than Andy Cross provides. But if you want to run different sites on same Windows Azure instance you have some things to consider first:

  1. Adding different sites to same solution is not good idea because sites can belong to different projects maintained by different people.
  2. Usually code is kept in source code repository and you don’t want to have files of one project in folder of other.
  3. You don’t always want to build all sites together and then publish them to cloud again.

Having these considerations in mind we can go step further from Andy’s solution.

Using special deployment project for same server sites

Windows Azure: File structure for independent sites deploymentMy solution is simple. I create new Windows Azure project on Visual Studio 2010 and add one WebRole to this solution. I call it DummyRole because it does practically nothing. You can use this project to redirect browser from your <something>.cloudapp.net address to default site.

Image on right shows my folder structure:

  • AzurePublishing – this is solution folder,
  • DummyRole – this is dummy web role mentioned before (I was not able to modify cloud project so it doesn’t have reference to some project in solution),
  • MsdnAzureDeploy – this folder contains cloud deployment project,
  • packages – this is created by Visual Studio, I left it there,
  • Publish – root folder for web applications that are deployed with web role,
  • www.example.com – publishing folders for web sites you want to deploy to server.

Every web site has its own folder under Publish folder and you can use Visual Studio web application publishing to publish web site files to correct subfolders.

Windows Azure: My deployment projectNow it’s time to modify our deployment package contents. You can see here web role called Redirect (this is the dummy one) and service configuration files. There are no publishing folder visible because cloud projects doesn’t support custom folders.

Expecting that your cloud project is set up correctly and you can deploy dummy site with no problems, we can take the next step and modify our service definition.

We have to add our publishing folders to service definition manually. Each web application folder is separate site in our case.

Here is the example content for service definition file:


<?xml version="1.0" encoding="utf-8"?>

<ServiceDefinition name="MsdnAzureDeploy" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">

  <WebRole name="Redirect" vmsize="Small">

    <Sites>

      <Site name="Web">

        <Bindings>

          <Binding name="Endpoint1" endpointName="Endpoint1" />

        </Bindings>

      </Site>

      <Site name="Ex1" physicalDirectory="..\Publish\www.example1.com">

        <Bindings>

          <Binding name="Endpoint1" endpointName="Endpoint1"
    
         hostHeader="www.example1.com"
/>

        </Bindings>

      </Site>

      <Site name="Ex2" physicalDirectory="..\Publish\www.example2.com">

        <Bindings>

          <Binding name="Endpoint1" endpointName="Endpoint1"
     
        hostHeader="www.example2.com"
/>

        </Bindings>

      </Site>

    </Sites>

    <Endpoints>

      <InputEndpoint name="Endpoint1" protocol="http" port="80" />

    </Endpoints>

    <Imports>

      <Import moduleName="Diagnostics" />

      <Import moduleName="RemoteAccess" />

      <Import moduleName="RemoteForwarder" />

    </Imports>

  </WebRole>

</ServiceDefinition>


Some things to note:

  1. our end-point is port 80 (HTTP)
  2. dummy web app has no host header specified and it is accessible from <something>.cloudapp.net,
  3. our two example web applications have host headers specified and during deployment IIS will be automatically configured so addresses point to correct applications (you have to make CNAME records in your domain keeper site),
  4. publishing folders location are relative to deployment project folder.

Make sure you have all paths correctly specified and try to deploy your cloud solution to Windows Azure. If your deployment succeeds then try to access your sites by URL-s you gave in service definition file and make sure everything works normally.

Conclusion

Although multiple web roles on same server are not supported by Windows Azure we can gather our sites under same web role and publish them all together. The solution shown here is not perfect but it works. Be careful with deployments and make sure your deployment packages contain always the versions that are allowed to be published. Also make your deployments to test instance first, test everything and then put your test instance to production.

Posted: Dec 29 2011, 07:57 PM by DigiMortal | with 3 comment(s)
Filed under: ,
ASP.NET security update shipping tomorrow

Out-of-band security update will be published for ASP.NET tomorrow. Security update will solve publicly known DoS issue that exists in all versions of ASP.NET. Update will be delivered through Windows Update and Windows Server Update. More details:

From security advisory document: “Sites that disallow application/x-www-form-urlencoded or multipart/form-data HTTP content types are not vulnerable”. It seems like most of servers are affected.

Posted: Dec 29 2011, 06:23 AM by DigiMortal | with no comments
Filed under:
My next year TOP5 wishlist for Windows media, WP7 and XBox teams in Microsoft

After building up my home network where XBox streams media from Windows Home Server using Windows Media Center I was very happy with my solution. Over weeks and months of active consuming of my media I found a lot of things that need to be improved or fixed. I am still happy but sometimes I’m looking already for alternatives. Here is my TOP5 wishlist for Microsoft people who build consumer media products.

Overview of my home network

This is overview of my home network. Here are shown only those devices that are used to consume multimedia I have in my Windows Home Server.

My home network

To give you better idea about my home network, here is a short description about every component:

  1. Windows Home Server is connected to my wireless and 3G router.
  2. Windows Home Server runs Windows 7 Home with Windows Media Center on VMWare virtual machine.
  3. XBox 360 is using Windows Media Center through its connector.
  4. Sound and video is going to TV set.
  5. I use my personal laptop to listen to my music at home.
  6. I use my Windows Phone 7 to access content in WHS when I’m out somewhere.
  7. I use my laptop in office to listen to my music when I work.

I think you have now some idea what I built and how things are working.

Wish #1: Windows Home Server should run Windows Media Center

First problem I faced when building my home media center was that Windows Home Server doesn’t run Windows Media Center. I like Windows Media Center although it is a little bit slow. But it still works very well for me. As I had no idea that there is such a legacy problem up I bought very usual home server box. There were also available very expensive boxes that have hardware powerful enough for real-time streaming but I thought it is something for professionals.

I looked for solutions to get Windows Media Center work on WHS but no luck. I even found no good hacks for that. Solution – let’s run virtualized Windows 7 Home Edition on WHS. And problem again – there is no Hyper-V role available. I installed VMWare player and installed Windows 7 Home Edition on it. It is dumb solution but it still works and it is way easier to set up that messing with Hyper-V server. I am not administrator and it probably takes me more time.

New version of Windows Home Server should include Windows Media Center. In the era of laptops and mobile computers there is no reason to expect that homes have usual desktop computers.

Wish #2: Windows Media Center should support video playlist

The next big problem I faced was missing support for video playlists. I have a little bit more than 800 music videos from different bands and different genres. There is no need for video playlists if you have only some home videos. But 800 music videos … it changes the game totally.

I thought that it is possible to create playlists like playlists for parties, family events, late night reading etc. It can be done with audio files but not with video files. Okay, there is one extremely stupid way to do it – create different folders and add hard links to video files because Windows Media Center is able to play contents of video folders. But this is not solution. It should be something I can do using Windows Media Player.

In this point I organized my music videos by DVD-s from where I ripped them and that was okay in the beginning. It is not enough right now.

I hope to see support for video and mixed playlists in next version of Windows Media Player, Zune, Windows Media Center or whatever it is that replaces them tomorrow.

Wish #3: Windows Media Center extender for XBox should support subtitles or provide easy way how to show what is currently playing

Now suppose I select my video folder from XBox and start play on root folder – there are about 800 music videos to play and there are a lot of artists. I don’t want to filter videos anyhow and it is very okay for me if videos are played in random order (remember, there is no support for video playlists yet).

One question that raises now is: who is the artist and what is the song? I have found no easy way in Windows Media Center how to get this information to screen. My video files have meta information but it is not shown to me when video is playing.

What I can do easily? Generate subtitle files programmatically. Windows Media Center is able to show me subtitles from my hard disk. XBox extender on the other hand knows nothing about it.

As video files are also getting more and more popular then they should be also treated similarly to audio files. Currently we get enough information about audio file that is playing in XBox. For video files I hope there will be setting that makes XBox show video file title on screen for some seconds.

Wish #4: Windows Phone 7 should also provide same rich media capabilities as Windows Media Center does

Lately Microsoft released very good app for Windows Phone 7: My Home Server. This app is able to connect to Windows Home Server and also play media that is stored on it. It works nice if Windows Home Server is configured correctly and accessible over internet.

As it is the first version of this application it’s not problem for me that all features are not built to maximum. The most important thing is – this application should be built to powerful mobile interface for Windows Home Server. Or better yet – why not integrate Windows Home Server support to Windows Phone 7?

I hope that Windows Home Server media support in My Home Server is first step towards integrating Windows Home Server content to Windows Phone 7. If there is good reason not to do it then I really hope that My Home Server will offer exactly same features that Windows Phone 7 built-in media player offers.

Wish #5: Windows Media Player should be more stable on Windows Server 2008

I am not the only one who uses Windows Server 2008 as main operating system. I have powerful laptop that hosts some virtual machines with my development environments and I really need some powerful virtualization engine like Hyper-V. This far it has worked like charm for me and I don’t plan to switch to some other technology.

Hyper-V is the only advanced thing running on my host system. Other stuff is usual – MS Office, browsers, chat clients and other usual programs. I don’t want to install developer tools on my host system because I have clean virtual machines available if something happens. It is way easier to restore some crashed or dead virtual machine than installing everything to host system.

I hope that Microsoft will also focus more on stabilizing their products on other platforms besides usual desktop ones. I know that there is no point to make all desktop software work on server but I think that most elementary software should also work on server environments.

There are more wishes for sure as I go on and discover XBox, Windows Home Server and so on. But those five here are most burning ones.

Connecting Windows Home Server to web through two routers

I wanted to get my Windows Home Server configured correctly for web so I have valid certificate by Microsoft and I can use subdomain service they are offering. As my home network is not default that is expected by Windows Home Server (no direct access to router that is connected to web) I had to trick my WHS a little bit. In this posting I will show you how to get Windows Home Server work with Microsoft address (http://something.homeserver.com) if it is not connected directly to router that has access to web.

My home network

Here is overview of my home network. This is just overview and virtual machines running on other machines are not drawn out. Basically I have 3G modem that is providing web access to my network (it has static IP). Behind 3G modem there is wireless router. Home server and XBox are connected to router using cable and laptops access web over WiFi.

My home network

The not-so-default thing for WHS is fact that it is connected to wireless and not 3G mobile modem/router. I have to use 3G modem/router because using 3G USB modem on WHS makes UPnP service go mad and consume almost all CPU resource. This is known error but there is no fix yet.

Setting up router and subdomain

If you have similar network set up then you have to find some moments of time when people are not connected to your network or at least they are not using web to do something important.

  1. Disconnect internal router from WHS.
  2. Disconnect web router from internal router.
  3. Connect web router to WHS.
  4. Log in to WHS and run WHS dashboard.
  5. Open server settings and move to Remote Web Access tab.
  6. Click button Set Up after router and let it set up your router.
    (make sure your router supports UPnP – then everything goes automatically)
  7. Click button Set Up after domain name and go through steps to get your own homeserver.com address.
  8. Assuming that everything went well you can try to access your WHS from external network. You may also ask some friend to try if he or she can access your WHS from his or her computer.

Now it’s time to restore previous setup of network and configure it so WHS can still be accessed from web.

Configuring routers

Your first router must now let some traffic to internal one and internal one must redirect all this traffic to your WHS. There are many ways how to do it usually. This is just one and you can give your own suggestions in comments of this post.

  1. Disconnect web router from WHS.
  2. Connect web router to internal router.
  3. Connect WHS to internal router.
  4. Make sure WHS has static IP (let’s say it has 192.168.1.10).
  5. Make sure that your internal router has static IP for web connections and for internal network. If your web router uses 192.168.100.1 as IP address for network then make your internal router use 192.168.100.100 by example.
  6. Make sure your internal router has static IP set for internal network (let’s say it is 192.168.1.1).
  7. Enable DMZ in web router and assign external IP of internal router to DMZ IP. I want my internal router to handle security and routings.
  8. In internal router define port forwards to your WHS (192.168.1.10):
    • 80 – this is usual HTTP port and this is required for media streaming,
    • 443 – this is secure HTTP port and over this port you can see WHS web interface.
    • You can also enable forwards for other ports you need to access.
  9. Make sure that internal router is using full security if it is put to DMZ.
  10. Try to access your home server from external network.

Now everything should work normally and you should be able to access your WHS from other machines. If you check from WHS dashboard you should see picture like this (of course, with your own router name).

Windows Home Server: Router is configured correctly

Conclusion

It was not hard to get Windows Home Server configured for web when it is behind two routers. You first let WHS configure web router and subdomain. Then you just restore your network setup like it was before and add some routings so WHS can be accessed from web. If your setup is correct then WHS can see your internal router and WHS server settings dialog shows that everything is okay.

MSSQL & NHibernate – mapping week numbers to properties

In one of my applications I have to use week numbers as properties of business classes that are persisted to database using NHibernate. Week numbers are used in UI and users can filter and sort data by week numbers. In this posting I will show you how to make SQL Server deal with week numbers and how to use them in your domain model.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

More Posts