Wednesday, December 12, 2012

Sitecore Advanced System Reporter (ASR) export error

ASR for Sitecore can be a powerful module to increase the reporting capability users have in Sitecore. I recently found an issue with the export functionality of the module though. You can run and report and export it to csv or excel which is  a great feature, however, there is a requirement that is never called out anywhere. When I first tested this I was logged in as Administrator. The report ran great and I was able to export and download the report in excel format. However, when you login as a normal user (one that is not admin) you get a little different behavior. When you click export you are prompted for your email address. Wait, I was just able to download the file as an admin why does it need my email address now? Well it turns out that a non-admin user cannot just download the report, they have to email the downloaded report. For this reason  you are prompted for an email address. If you provide one and SMTP is not configured you just get an error. If SMTP is configured  you get the downloaded report sent via an email.

Moral of the story? Well, first try not to run very much as an admin, and second get SMTP configured for Sitecore if you want ASR export command to be of any use to anyone but the admin.

Saturday, November 17, 2012

Enabling Sitecore DMS

I have been working more with Sitecore as of late and one of the things I wanted to do was work with DMS. Every time I went into a DMS functional area though all I could get was "Analytics is disabled" message. So it was time to start plugging away to figure out why. The install steps are not clear, or at least they were not to me. Here are the steps I took to getting DMS enabled:

1) First check out John West’s blog for some things to check

If you are missing the Sitecore.Analytics.config file you probably missed the database install step of getting DMS up and running. You need to make sure you download and unzip the database zip file that holds the config files and database files for the Analytics database, for your version. Once you have that download here are a few steps you may have missed.

2) Move the config files to <sitecore site>\Website\App_Config\Include

3) Add a connection string settings to the ConnectionString.config file (<sitecore site>\Website\App_Config) with the name “analytics.”

At this point try and go into engagement analytics. The disabled message should have gone away. It may even work for you at this point. For me I still had database errors about invalid objects. This was an easy one though. When I copied the database connection string I forgot to update the database name to the analytics database.​

Saturday, November 10, 2012

Two weeks with the Microsoft Surface

The Microsoft Surface has been out for about two weeks now and I have been working with my for the same timeframe. I wanted to write up my experience with it to date. I have been trying to put it through its paces as much as I could. I will try and cover all the things I have done with it so you get a feel for where my opinion is coming from. For background I have only owned one other tablet in my life and it was the first version of the iPad. After a month or so of the iPad I stopped using it. Maybe that was because I shared it with my kids and wife. I think it was a combination of that and just not enjoying it as a primary device and apple kind of bugs me.

When I went to get the surface I was not sure if I would get it. When I finally got my hands on the hardware I was really impressed and it made it hard to say no. I got the 32GB devices with the type keyboard (writing this review on it now in fact). I have used it in a number of different scenarios the past two weeks and I will try and quickly summarize them each here.

Apps
I was not sure what to expect on this front and while the selection is still pretty slim it is actually better then I was expecting. I have found a few quality apps like google reader app, kindle, flight tracker, mindmapper, onenote and some good games. The selection needs to grow quickly though as your options are limited.

Touch Interface
I have really loved the touch interface. There have been a few times when the touch does not seems to register but it is infrequent enough it is not a big deal. It only took me a couple days to get use to the gestures and the ability to dock two apps and work with them as the same time has been great.

Keyboard
I have used the onscreen keyboard and the type keyboard a fair amount. Both have been a pleasant surprise. My typing speed, responsiveness and accuracy of the onscreen keyboard as been really good. While I don't think you want to type a lot with the on screen keyboard, the smaller stuff is pretty easy. With the screen being wider than other tablets typing while holding can't really be done without going one handed. You can switch the keyboard to thumbs mode (not sure if that is what it is really called) and that works but again typing with any speed at all that way would take some practice.

The type keyboard has been great. I have used it for all day meetings to type notes, write emails and word documents. The design is great as is the feel. I do have a few issues with it though and one is not its fault it is just the nature of the beast. The keyboard cover is smartly designed so when you flip it to the back it becomes unresponsive so you don't have to worry about key presses when you flip it to the back. This feature does not always work 100% though. Sometimes I will flip it back and the surface will turn on just in time to get a few key presses registered before it turns off. At that point it is hard to get the onscreen keyboard to come up because it thinks the typepad is active. Also when you flip it to the back and hold the device you can feel the keys being pressed. While it normally works get and the keyboard is shut off while flipped back it just feels odd to me to feel the keys back there. While it has held up great it just feels weird. I know it is the nature of the best so not Microsoft's fault. I have used the touch keyboard a little and it feels better when flipped back and it just feel like a cover. I have not used the touch enough to speak to the typing experience.

Airplane
I have used it on a couple business trips on the plane, and I have to say I love it. I read a review where he was complaining it did not fit very well. I have to say I did not find that to be the case at all. When I got on the plane I put it in the seatback pocket in front of me and it fit fine. It also fit well on the tray. Granted with the keyboard and kickstand there was not much extra room but it still worked great.

Battery
In order to really test this out I made a point to not plug it in. I spend a day (9-5:30pm) in a meeting taking notes, email, doing some internet searching and reading documents, and at the end of the day I still had 50% battery left. I then took the device to dinner with my boss where we spent another hour and a half doing some work (internet search and documents). When I got back to the hotel I still had a good quarter of the battery left. This was way better then I expected.

Reading
I have spent time reading my kindle books on the device, PDF files, websites and word documents. While you can read the device while holding it, it is heavy enough you can't do it for very long. Overall it has worked rather well. When you shift the device to portrait instead of landscape it provides an interesting experience. Since the device is a 16:9 widescreen device you get a more longer (think legal paper) reading experience. To tell  you the truth I am still not sure if this is a good thing or a bad thing. At times I really like it and at others I don't. Most of the time I end up reading in widescreen mode.

I have also spent time watching some movies on it Netflix is great and with an HDMI connector plugged into a TV it is even better. Things like Xbox smartglass is a cool feature, but to be honest I am not sure how much I will ever really use it.

Overall, I am very happy I bought the device. After two weeks I have a hard time being without it.

Wednesday, October 3, 2012

Trouble Activating Windows 8

In case you are having trouble activating your windows 8 install here is the trick for you.

Open a command prompt as admin and run the following:

slmgr.vbs -ipk "your-key-here"

Thursday, September 27, 2012

Multi-Threading with ThreadPool and a Thread Manager

The past few weeks have been spent trying to make some tasks in a winforms app multi-threaded. Threaded applications can get tricky really fast. I needed to create a thread manager that helped me queue and create new threads via the ThreadPool. Here is the solution I came up with. I by no means think is the best or only solution. It in fact has one constraint with it I will call out at the end. It is an approach to doing threading in .Net applications though.

I needed a class that would do the following:

- Calculate how many threads to spin up

- Calculate how much work to give to each thread

- Queue new threads with the ThreadPool

- Determine if WaitHandles needed to be created for the new threads

- Pass the WaitHandles to the new threads so they can signal when complete

- Return a collection of created WaitHandles back so future process could wait on all threads if needed

First I created a ThreadState object to hold the information I needed to pass to each thread.

   1:  public class ThreadState
   2:  {
   3:       public int UnitId { get; set; }
   4:       public int PageSize { get; set; }
   5:       public int PageNumber { get; set; }
   6:       public WaitHandle WaitHandle { get; set; }
   7:  }

Now the ThreadManager class:

   1:  public WaitHandle[] QueueThreads(double totalCount, ThreadTypes threadType, int UnitID, WaitHandle[] threadsToWaitfor)
   2:          {
   3:              int MaxThreadCount = int.Parse(System.Configuration.ConfigurationManager.AppSettings["MAX_THREADS"]);
   4:              int pageSize = (int)Math.Round(totalCount / MaxThreadCount, 0);
   5:              List<WaitHandle> createdHandles = new List<WaitHandle>();
   6:   
   7:              //Set the max threads the computer should prep form. This means all additional requests after this number
   8:              // will be queued and wait for previous threads to finish. 
   9:              ThreadPool.SetMaxThreads(MaxThreadCount, MaxThreadCount);
  10:   
  11:              // This sets the number of idles threads the process should keep ready for additional requests. 
  12:              ThreadPool.SetMinThreads(5,5);
  13:   
  14:              if (threadsToWaitfor != null && threadsToWaitfor.Count() > 0)
  15:                  WaitHandle.WaitAll(threadsToWaitfor);
  16:   
  17:   
  18:              for (int i = 1; i <= MaxThreadCount; i++)
  19:              {
  20:                  // add additional buffer records for final page
  21:                  if (i == MaxThreadCount)
  22:                      pageSize += 10;
  23:   
  24:                  ThreadState state = new ThreadState()
  25:                  {
  26:                      OrgUnitId = orgUnitID,
  27:                      PageNumber = i,
  28:                      PageSize = pageSize
  29:                  };
  30:   
  31:                  if (createWaitHandles)
  32:                  {
  33:                      // Each thread should only have 1 handle linked to it that it is responsible for calling single on when it is done.
  34:                      var threadHandle = new AutoResetEvent(false);
  35:                      state.WaitHandle = threadHandle;
  36:                      createdHandles.Add(threadHandle);
  37:                  }
  38:   
  39:                  switch (threadType)
  40:                  {
  41:                      case ThreadTypes:
  42:                          ThreadPool.QueueUserWorkItem(new ApplicationConverter().ConvertApplications, state);
  43:                          break;
  44:                     
  45:                      default:
  46:                          break;
  47:                  }
  48:   
  49:   
  50:              }
  51:   
  52:              // return an array list for all handles created. This allows other threads to be linked as dependant on these. 
  53:              return createdHandles.ToArray();
  54:          }

Here is the ConvertApplications method:

   1:  public void ConvertApplications(Object state)
   2:  {
   3:     ThreadState tState = (ThreadState)state;
   4:     _pageSize = tState.PageSize;
   5:     _pageNumber = tState.PageNumber;
   6:     _orgUnitId = tState.OrgUnitId;
   7:     DoTheWork();
   8:   
   9:    // If we have a waithandle it means we need to single when this process has completed
  10:    if (tState.WaitHandle != null)
  11:    {
  12:        ((System.Threading.AutoResetEvent)tState.WaitHandle).Set();
  13:    }
  14:  }

The idea with the ThreadManager class is you can pass in some basic information about the work you want to do and it will create the threads it needs. My work was pulling data from a database with an ORM tool that could handle paging. So the ThreadManager looks at the total count of records to process passed in against the max thread count the app is configured for and calculates how much work each thread needs to do. At line 14 it can stop and wait for other outstanding threads to signal they are done before creating and queuing additional threads.

Since creating AutoResetEvents is heavy on the system there is a flag you can pass in saying rather or not you need threads to single when they are done. This collection of AutoResetEvents is then stored in a collection and returned. This in turn allows your main thread to wait if needed.

This approach works really well, but there is one flaw in it. If you don’t need to spin up and keep track of more then 64 threads this will work fine. If you make the call “WaitHandle.WaitAll()” and pass in an area of WaitHandles that is greater than 64 you will get an error. 64 is the upper cap that WaitHandle.WaitAll can monitor. The reason for this is to try and limit the overhead created for all these WaitHandles. For some situations this may be just the manager you need (or at least a good start on it). For other situation not so much.

Tuesday, September 11, 2012

Windows 8 UI Freezing

Having issues with your new Windows 8 install freezing on you? Here is a possible fix.

http://www.withinwindows.com/2012/06/28/workaround-for-windows-8-freezing-issues/

Sunday, August 5, 2012

Free eBooks

Microsoft has been kind enough to release a number of free eBooks around their technology, and finally a number of them in a format besides PDF. If you want to read up one things like Azure, Windows Phone, SharePoint and Office here are some good starting points.

Azure, SharePoint, WP, Windows 8, Office – set 1

Azure, SharePoint, WP, Windows, Office – set 2

Friday, August 3, 2012

Running WIF Relying parties in Windows Azure

I am coping this blog from another blog here. Copying it here just to make sure I can find it in the future. My Azure app had this issue and this fixed the problem. 

When running in a multi server environment like windows azure it is required to make sure the cookies generated by WIF are encrypted with the same pair of keys so all servers can open them.

Encrypt cookies using RSA

In Windows Azure, the default cookie encryption mechanism (which uses DPAPI) is not appropriate because each instance has a different key. This would mean that a cookie created by one web role instance would not be readable by another web role instance. This could lead to service failures effectively causing denial of the service. To solve this problem you should use a cookie encryption mechanism that uses a key shared by all the web role instances. The following code written to global.asax shows how to replace the default SessionSecurityHandler object and configure it to use the RsaEncryptionCookieTransform class:

next upload the certificate to the hosted service and declare it in the LocalMachine certificate store of the running role.

void Application_Start(object sender, EventArgs e)
{
    FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
}
 
private void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
{
   List<CookieTransform> sessionTransforms =
       new List<CookieTransform>(new CookieTransform[] 
       {
          new DeflateCookieTransform(), 
          new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate),
          new RsaSignatureCookieTransform(e.ServiceConfiguration.ServiceCertificate)
       });
 
       SessionSecurityTokenHandler sessionHandler =
         new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
            e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
}

 

image

image

Failing to do the above will generate the following exception when running a relying party in azure: "InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API". It means that decryption with DPAPI failed. It makes sense because DPAPI key is coupled with the physical machine it is running on.

After changing the encryption policy (like so) make sure to delete all existing cookies other wise you will get the following exception: CryptographicException: ID1014: The signature is not valid. The data may have been tampered with. (It means that an old DPAPI cookie is being processed by the new RSA policy and that will obviously will fail.

Monday, June 25, 2012

Create a Cert for Azure with MakeCert.exe

I have been working with Azure a lot as of late. One of the tasks I have had to do a few times is create a cert I can upload and that my Azure web role can use. I have gotten tired of always trying to remember the right command line to do this. By default MakeCert creates a 1024 bit cert which is no longer a valid bit size for IIS. If you don’t change this default and try and upload and use that cert your web role will continue to crash on you and never fully initialize (A fact I found out painfully. FYI, if you have this issue I not only need to create a cert with 2048 bit size but I need to change the cert name in my web role config.). So, if you have an Azure web role that keeps crashing on you and it is not for the standard reason of missing assembly that was not set to copy local, check your certs.

Now on to the good stuff. Here is the command line I am using.

makecert –r –pe –a sha1 –n “CN=<your name here>” –ss My –len 2048 –sp “Microsoft Enhanced RSA and AES Cryptographic Provider” –sy 24 <cert name>.cer

This command also, as part of the creation, puts the cert in your local cert store. You can then export the private key (pfx file) via the management console (mmc). I tried to use this cert for my ACS relying party to encrypt the token but it is not a valid cert for key exchange.

Monday, April 23, 2012

DevExpress Complexity (Maintenance & Cyclomatic

I have been using DecExpress’s CodeRush tools a lot recently. One of the many great features COdeRush gives you is inline Cyclomatic and Maintenance complexity scores of your classes and methods.

image

The challenge is to remember how those scores rank so you know when you should start refactoring your methods. I found a great post here that I wanted to move over to my own blog just to make sure I could find it again and have it handy. There is also a PDF reference to all this info.

Here is my summary of the key things (in case the other post goes away):

Cyclomatic Complexity

Probably the simplest measure of complexity. CC is the number of decision points for a method plus one. It also happens to represent the minimum number of test cases needed to travel through all branches of a method.

image

Maintenance Complexity

The purpose of Maintenance Complexity is to give you a picture of how much code you have in a given member.

image