Friday, September 13, 2013

Create Wix installer for Windows Service

In this article I'm going to discuss create wix installer for windows service. in this scenario i considered about following situation.
  • Attach Exe with App.config
  • Attach external dll
  • Modify App.config values based on installed location.
for this you can use either visual studio editor or Wix Editor. 
 <?xml version="1.0" encoding="utf-8"?>  
 <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">  
   <Product Id="5AC767F2-4F21-45DF-9DAB-C013C62B2B67" Name="Installer" Language="1033" Version="1.0.0.0" Manufacturer="TRT " UpgradeCode="3fd83540-23f6-45f4-bb1c-e77c12725680">  
     <Package InstallerVersion="200" Compressed="yes" />  
     <Media Id="1" Cabinet="SampleApp.cab" EmbedCab="yes" />  
     <Directory Id="TARGETDIR" Name="SourceDir">  
       <Directory Id="ProgramFilesFolder">  
         <Directory Id="INSTALLFOLDER" Name="Installer">  
           <Directory Id="TestService" Name="TestService">  
             <Component Id="MainExecutable" Guid="CE30D5D8-3211-4890-A9DC-0C315C15B39E">  
               <File Id="testServiceexe" Name="TestInstallerProject.exe" DiskId="1" KeyPath="yes" Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe" />  
               <File Id="TestInstallerProject.exe.config" Name="TestInstallerProject.exe.config" Source="..\TestInstallerProject\App.config" Vital="yes" DiskId="1" />  
               <File Id="GOOGLE.GDATA.ANALYTICS.DLL" Name="Google.GData.Analytics.dll" Source="..\TestInstallerProject\bin\Debug\Google.GData.Analytics.dll" />  
               <File Id="GOOGLE.GDATA.CLIENT.DLL" Name="Google.GData.Client.dll" Source="..\TestInstallerProject\bin\Debug\Google.GData.Client.dll" />  
               <ServiceInstall Name="ScheduledService" Type="ownProcess" Start="auto" ErrorControl="ignore" Id="ServiceInstaller" DisplayName="ScheduledService" Account="[SERVICEACCOUNT]" Password="[SERVICEACCOUNTPASSWORD]">  
                 <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" ThirdFailureActionType="restart" ResetPeriodInDays="1" RestartServiceDelayInSeconds="1" />  
               </ServiceInstall>  
               <ServiceControl Id="StartService" Name="ScheduledService" Start="install" Stop="both" Remove="uninstall" Wait="no" />  
               <RemoveFolder Id="INSTALLDIR" On="uninstall" />  
               <util:XmlFile Id="ModifyServiceLocation" ElementPath="//configuration/appSettings/add[\[]@key='TextLocation'[\]]/@value" File="[TestService]\TestInstallerProject.exe.config" Action="setValue" Value="[TestService]ScheduledServiceNew.txt" PreserveModifiedDate="yes" SelectionLanguage="XPath" Sequence="1" />  
             </Component>  
           </Directory>  
         </Directory>  
       </Directory>  
     </Directory>  
     <Feature Id="Complete" Title="Installer" Level="1">  
       <ComponentRef Id="MainExecutable" />  
     </Feature>  
     <UI>  
     </UI>  
   </Product>  
 </Wix>  

when attache app.config, you have to consider about name and the id of the config. it should be the name of the exe.

 <File Id="TestInstallerProject.exe.config" Name="TestInstallerProject.exe.config" Source="..\TestInstallerProject\App.config" Vital="yes" DiskId="1" />  

for modify the App.config have to use util:XmlFile tag. in here my app.config file is something like this.

 <?xml version="1.0" encoding="utf-8" ?>  
 <configuration>  
  <appSettings>  
   <add key="TextLocation" value="d:\ScheduledServiceNew.txt"/>  
  </appSettings>  
 </configuration>  
I want to update it into something like this:

 <?xml version="1.0" encoding="utf-8"?>  
 <configuration>  
  <appSettings>  
   <add key="TextLocation" value="C:\Program Files (x86)\Installer\TestService\ScheduledServiceNew.txt"/>  
  </appSettings>  
 </configuration>  

for that i have to use  util:XmlFile tag as follows:
 <util:XmlFile Id="ModifyServiceLocation" ElementPath="//configuration/appSettings/add[\[]@key='TextLocation'[\]]/@value" File="[TestService]\TestInstallerProject.exe.config" Action="setValue" Value="[TestService]ScheduledServiceNew.txt" PreserveModifiedDate="yes" SelectionLanguage="XPath" Sequence="1" />  

once you compile the project you can create installer using wix for windows service

Sunday, April 28, 2013

Read And Write data in MVC using n tier architecture

In this scenario we discuss how we can do CRUD operations in MVC using n tier architecture. in this lesson i'm going to assume that reader must have basic knowledge of MVC framework and how it works.

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
    <script type="text/javascript" src="../../Scripts/jquery-1.5.1.min.js"></script>  
 </head>  
 <body>  
   <div>  
     <%: Html.ActionLink("Create full time student", "CreatefullStudent")%><br>  
     <%: Html.ActionLink("Create part time student", "CreatepartStudent")%>  
     <br />  
     <input type="button" value="Get student" id="btnstudent" />  
     <div id="studentlist"></div>  
   </div>  
 </body>  
 </html>  
 <script type="text/javascript">  
   $(document).ready(function () {  
     $("#btnstudent").click(function () { GetStudent(); });  
     GetStudent();  
   });  
   function GetStudent() {  
     $.ajax({  
       url: "Home/GetStudentList",  
       type: 'GET',  
       contentType: 'application/json; charset=utf-8',  
       dataType: 'json',  
       success: function (result) {  
         alert("test");  
         if (result.length == 0) {  
           $("#studentlist").html("No details available");  
         } else {  
           var str = "<table><thead><tr><td>Id</td><td>Name</td></tr></thead><tbody>"  
           for (var i = 0; i < result.length; i++) {  
             str += "<tr>";  
             str += "<td>" + result[i].Id + "</td>";  
             str += "<td>" + result[i].Name + "</td>";  
             str += "</tr>";  
           }  
           str += "</tbody></table>"  
           $("#studentlist").html(str);  
         }  
       }  
     });  
   }  
 </script>  



Sunday, February 17, 2013

Read data from Google data API

When you send data to Google analytics, you might need access those data. so then first you need to create Google API console project. follow the instructions in here-
https://developers.google.com/analytics/resources/articles/gdata-migration-guide
in API access tab you can get the API key which is needed for getting data from Analytics. In addition to that you need following dll.
  • Google.GData.Analytics.dll
  • Google.GData.Client.dll
you can download those dll from following link-
http://code.google.com/p/google-gdata/downloads/list 

once you install this you can get the dll's from 
C:\Users\xyz\Documents\Google Data API SDK\Sources\Library\analytics\bin\Debug folder

 private void RefreshFeed()   
   {   
    string userName = "abc@gmail.com";   
    string passWord = "pwdabc";   
    string gkey = "?key=AIzaSyACrV2kbnPl9ybZZh8-sla_sPnLIKYKbxA";   
    string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;   
    AnalyticsService service = new AnalyticsService("WebAppTest");   
    service.setUserCredentials(userName, passWord);   
    DataQuery query2 = new DataQuery(dataFeedUrl);   
    query2.Ids = "ga:12345678";   
    query2.Metrics = "ga:pageviews";   
    query2.Sort = "ga:pageviews";   
    query2.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");   
    query2.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");   
    query2.StartIndex = 1;   
    DataFeed data = service.Query(query2);   
    foreach (DataEntry entry in data.Entries)   
    {   
       string st = entry.Title.Text;   
       string ss = entry.Metrics[0].Value;   
    }   
  }   

You can get Id as shown in following photo