Feb 16, 2011

Site Web Analytics not updating Sharepoint 2010

If you facing the issue that the web Analytics Reports in Sharepoint 2010 Central Administration is not updating data.

When you go to your site > site settings > Site Web Analytics reports or Site Collection Analytics reports 
You get old data as in the ribbon displayed "Data Last Updated: 12/13/2010 2:00:20 AM"

Please insure that the following things are covered:
Insure that Usage and Data Health Data Collection service is configured correctly.
Log Collection Schedule is configured correctly
Microsoft Sharepoint Foundation Usage Data Import and Microsoft SharePoint Foundation Usage Data Processing Timer jobs are configured to run at regular intervals
One last important Timer job is the Web Analytics Trigger Workflows Timer Job insure that this timer job is enabled and scheduled to run at regular intervals (for each site that you need analytics for).
After you have insured that the web analytics service configuration is working fine and the Usage Data Import job is importing the *.usage files from the ULS LOGS folder into the WSS_Logging database, and that all the required timer jobs are running as expected… wait for a day for the report to get updated… the report gets updated automatically at 2:00 am in the morning… and i could not find a way to control the schedule for this report update job.

So be sure to wait for a day before giving up :)

kick it on DotNetKicks.com

Feb 4, 2011

Change AccountName/LoginName for a SharePoint User (SPUser)

Consider the following:
We have an account named MYDOMAIN\eholz. This accounts Active Directory Login Name changes to MYDOMAIN\eburrell
Now this user was a active user in a Sharepoint 2010 team Site, and had a userProfile using the Account name MYDOMAIN\eholz.
Since the AD LoginName changed to eburrell hence we need to update the Sharepoint User (SPUser object) as well update the userprofile to reflect the new account name.
To update the Sharepoint User LoginName we can run the following stsadm command on the Server:

STSADM –o migrateuser –oldlogin MYDOMAIN\eholz –newlogin MYDOMAIN\eburrell –ignoresidhistory

However to update the Sharepoint 2010 UserProfile, i first tried running a Incremental/Full Synchronization using the User Profile Synchronization service… this did not work. To enable me to update the AccountName field (which is a read only field) of the UserProfile, I had to first delete the User Profile for MYDOMAIN\eholz and then run a FULL Synchronization using the User Profile Synchronization service which synchronizes the Sharepoint User Profiles with the AD profiles.

Update: if you just run the STSADM –o migrateuser command… the profile also gets updated automatically. so all you need is to run the stsadm –o migrate user command and you dont need to delete and recreate the User Profile

kick it on DotNetKicks.com

Jun 18, 2010

Search All Columns in All Tables using TSQL

DECLARE @SearchStr nvarchar(100)

SET @SearchStr = 'SEARCH_KEYWORD'

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END

SELECT ColumnName, ColumnValue FROM #Results

kick it on DotNetKicks.com

Jun 7, 2010

Unit testing internal methods in a strongly named assembly/project

If you need create Unit tests for internal methods within a assembly in Visual Studio 2005 or greater, then we need to add an entry in the AssemblyInfo.cs file of the assembly for which you are creating the units tests for. For e.g. if you need to create tests for a assembly named FincadFunctions.dll & this assembly contains internal/friend methods within which need to write unit tests for then we add a entry in the FincadFunctions.dll’s AssemblyInfo.cs file like so :

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("FincadFunctionsTests")]

where FincadFunctionsTests is the name of the Unit Test project which contains the Unit Tests. However if the fincadFunctions.dll is a strongly named assembly then you will the following error when compiling the FincadFunctions.dll assembly :


Friend assembly reference “FincadFunctionsTests” is invalid. Strong-name assemblies must specify a public key in their InternalsVisibleTo declarations.

Thus to add a public key token to InternalsVisibleTo Declarations do the following: You need the .snk file that was used to strong-name the FincadFunctions.dll assembly. You can extract the public key from this .snk with the sn.exe tool from the .NET SDK. First we extract just the public key from the key pair (.snk) file into another .snk file. sn -p test.snk test.pub Then we ask for the value of that public key (note we need the long hex key not the short public key token): sn -tp test.pub We end up getting a super LONG string of hex, but that's just what we want, the public key value of this key pair. We add it to the strongly named project "FincadFunctions.dll" that we want to expose our internals from. Before what looked like:

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("FincadFunctionsTests")]

Now looks like.

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("FincadFunctionsTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010011fdf2e48bb")]

And we're done. hope this helps

kick it on DotNetKicks.com

May 21, 2010

LinqToXML removing empty xmlns attributes

Suppose you need to generate the following XML:
<GenevaLoader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.advent.com/SchemaRevLevel401/Geneva masterschema.xsd" xmlns="http://www.advent.com/SchemaRevLevel401/Geneva">
<PriceRecords>
<PriceRecord>
</PriceRecord>
</PriceRecords>
</GenevaLoader>

Normally you would write the following C# code to accomplish this:

const string ns = "http://www.advent.com/SchemaRevLevel401/Geneva";
XNamespace xnsp = ns;
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");

XElement root = new XElement( xnsp + "GenevaLoader",
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute( xsi + "schemaLocation", "http://www.advent.com/SchemaRevLevel401/Geneva masterschema.xsd"));

XElement priceRecords = new XElement("PriceRecords");
root.Add(priceRecords);

for(int i = 0; i < 3; i++)
{
XElement price = new XElement("PriceRecord");
priceRecords.Add(price);
}

doc.Save("geneva.xml");

The problem with this approach is that it adds a additional empty xmlns arrtribute on the “PriceRecords” element, like so :

<GenevaLoader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.advent.com/SchemaRevLevel401/Geneva masterschema.xsd" xmlns="http://www.advent.com/SchemaRevLevel401/Geneva">
<PriceRecords xmlns="">
<PriceRecord>
</PriceRecord>
</PriceRecords>
</GenevaLoader>

The solution is to add the xmlns NameSpace in code to each child and grandchild elements of the root element like so :

XElement priceRecords = new XElement( xnsp + "PriceRecords");
root.Add(priceRecords);

for(int i = 0; i < 3; i++)
{
XElement price = new XElement(xnsp + "PriceRecord");
priceRecords.Add(price);
}

kick it on DotNetKicks.com

Copying files from GAC using xcopy or Windows Explorer

use this command for copying files using a wildcard from the GAC to a local folder.
xcopy c:\windows\assembly\Microsoft.SqlServer.Smo*.dll c:\gacdll /s/r/y/c

The above command will continue even it encounters any “Access Denied” errors, thus copying over the required files.

To copy files using the Windows explorer just disable the GAC Cache Viewer by adding a entry to the registry:
Browse to “HKEY_LOCALMACHINE\Software\Microsoft\Fusion”
Add a Dword called DisableCacheViewer. Set the value of it to 1.

kick it on DotNetKicks.com

May 11, 2010

Entity Framework v1 – tips and Tricks Part 3


The previous posts on Entity framework are available here :
Entity Framework version 1- Brief Synopsis and Tips – Part 1
Entity Framework v1 … Brief Synopsis and Tips – Part 2

General Tips on Entity Framework v1 & Linq to Entities:

ToTraceString()

If you need to know the underlying SQL that the EF generates for a Linq To Entities query, then use the ToTraceString() method of the ObjectQuery class. (or use LINQPAD)
Note that you need to cast the LINQToEntities query to ObjectQuery before calling TotraceString() as follows:

string efSQL = ((ObjectQuery)from c in ctx.Contact
where c.Address.Any(a => a.CountryRegion == "US")
select c.ContactID).ToTraceString();


======================================================================

MARS or MultipleActiveResultSet
When you create a EDM Model (EDMX file) from the database using Visual Studio, it generates a connection string with the same name as the name of the EntityContainer in CSDL. In the ConnectionString so generated it sets the MultipleActiveResultSet attribute to true by default. So if you are running the following query then it streams multiple readers over the same connection:

using (BAEntities context = new BAEntities())
{
var cons =
from con in context.Contacts
where con.FirstName == "Jose"
select con;
foreach (var c in cons)
{
if (c.AddDate < new System.DateTime(2007, 1, 1))
{
c.Addresses.Load();
}
}
}
======================================================================
Explicitly opening and closing EntityConnection

When you call ToList() or foreach on a LINQToEntities query the EF automatically closes the connection after all the records from the query have been consumed.

Thus if you need to run many LINQToEntities queries over the same connection then explicitly open and close the connection as follows:
using (BAEntities context = new BAEntities())
{
context.Connection.Open();
var cons = from con in context.Contacts where con.FirstName == "Jose"
select con;
var conList = cons.ToList();
var allCustomers = from con in context.Contacts.OfType<Customer>()
select con;
var allcustList = allCustomers.ToList();
context.Connection.Close();
}


======================================================================

Dispose ObjectContext only if required

After you retrieve entities using the ObjectContext and you are not explicitly disposing the ObjectContext then insure that your code does consume all the records from the LinqToEntities query by calling .ToList() or foreach statement, otherwise the the database connection will remain open and will be closed by the garbage collector when it gets to dispose the ObjectContext.

Secondly if you are making updates to the entities retrieved using LinqToEntities then insure that you dont inadverdently dispose of the ObjectContext after the entities are retrieved and before calling .SaveChanges() since you need the SAME ObjectContext to keep track of changes made to the Entities (by using ObjectStateEntry objects). So if you do need to explicitly dispose of the ObjectContext do so only after calling SaveChanges() and only if you dont need to change track the entities retrieved any further.


======================================================================

SQL InjectionAttacks under control with EFv1

LinqToEntities and LinqToSQL queries are parameterized before they are sent to the DB hence they are not vulnerable to SQL Injection attacks.


EntitySQL may be slightly vulnerable to attacks since it does not use parameterized queries. However since the EntitySQL demands that the query be valid Entity SQL syntax and valid native SQL syntax at the same time.


So the only way one can do a SQLInjection Attack is by knowing the SSDL of the EDM Model and be able to write the correct EntitySQL (note one cannot append regular SQL since then the query wont be a valid EntitySQL syntax) and append it to a parameter.

======================================================================
Improving Performance
You can convert the EntitySets and AssociationSets in a EDM Model into precompiled Views using the edmgen utility. for e.g. the Customer Entity can be converted into a precompiled view using edmgen and all LinqToEntities query against the contaxt.Customer EntitySet will use the precompiled View instead of the EntitySet itself (the same being true for relationships (EntityReference & EntityCollections of a Entity)). The advantage being that when using precompiled views the performance will be much better.

The syntax for generating precompiled views for a existing EF project is :
edmgen /mode:ViewGeneration /inssdl:BAModel.ssdl /incsdl:BAModel.csdl /inmsl:BAModel.msl /p:Chap14.csproj
Note that this will only generate precompiled views for EntitySets and Associations and not for existing LinqToEntities queries in the project.(for that use CompiledQuery.Compile<>)
Secondly if you have a LinqToEntities query that you need to run multiple times, then one should precompile the query using CompiledQuery.Compile method. The CompiledQuery.Compile<> method accepts a lamda expression as a parameter, which denotes the LinqToEntities query  that you need to precompile.

The following is a example of a lamda that we can pass into the CompiledQuery.Compile() method
Expression<Func<BAEntities, string, IQueryable<Customer>>> expr = (BAEntities ctx1, string loc) =>
from c in ctx1.Contacts.OfType<Customer>()
where c.Reservations.Any(r => r.Trip.Destination.DestinationName == loc)
select c;
Then we call the Compiled Query as follows:
var query = CompiledQuery.Compile<BAEntities, string, IQueryable<Customer>>(expr);

using (BAEntities ctx = new BAEntities())
{
var loc = "Malta";
IQueryable<Customer> custs = query.Invoke(ctx, loc);
var custlist = custs.ToList();
foreach (var item in custlist)
{
Console.WriteLine(item.FullName);
}
}
Note that if you created a ObjectQuery or a Enitity SQL query instead of the LINQToEntities query, you dont need precompilation for e.g.
An Example of EntitySQL query :
string esql = "SELECT VALUE c from Contacts AS c where c is of(BAGA.Customer) and c.LastName = 'Gupta'";
ObjectQuery<Customer> custs = CreateQuery<Customer>(esql);

An Example of ObjectQuery built using ObjectBuilder methods:
from c in Contacts.OfType<Customer>().Where("it.LastName == 'Gupta'")
select c

This is since the Query plan is cached and thus the performance improves a bit, however since the ObjectQuery or EntitySQL query still needs to materialize the results into Entities hence it will take the same amount of performance hit as with LinqToEntities.

However note that not ALL EntitySQL based or QueryBuilder based ObjectQuery plans are cached. So if you are in doubt always create a LinqToEntities compiled query and use that instead


============================================================
GetObjectStateEntry Versus GetObjectByKey

We can get to the Entity being referenced by the ObjectStateEntry via its Entity property and there are helper methods in the ObjectStateManager (osm.TryGetObjectStateEntry) to get the ObjectStateEntry for a entity (for which we know the EntityKey). Similarly The ObjectContext has helper methods to get an Entity i.e. TryGetObjectByKey().

TryGetObjectByKey() uses GetObjectStateEntry method under the covers to find the object, however One important difference between these 2 methods is that TryGetObjectByKey queries the database if it is unable to find the object in the context, whereas TryGetObjectStateEntry only looks in the context for existing entries. It will not make a trip to the database
=============================================================

POCO objects with EFv1:
To create POCO objects that can be used with EFv1. We need to implement 3 key interfaces:

IEntityWithKey
IEntityWithRelationships
IEntityWithChangeTracker

Implementing IEntityWithKey is not mandatory, but if you dont then we need to explicitly provide values for the EntityKey for various functions (for e.g. the functions needed to implement IEntityWithChangeTracker and IEntityWithRelationships).

Implementation of IEntityWithKey involves exposing a property named EntityKey which returns a EntityKey object.
Implementation of IEntityWithChangeTracker involves implementing a method named SetChangeTracker since there can be multiple changetrackers (Object Contexts) existing in memory at the same time.
public void SetChangeTracker(IEntityChangeTracker changeTracker)
{
_changeTracker = changeTracker;
}
Additionally each property in the POCO object needs to notify the changetracker (objContext) that it is updating itself by calling the EntityMemberChanged and EntityMemberChanging methods on the changeTracker. for e.g.:

public EntityKey EntityKey
{
get { return _entityKey; }
set
{
if (_changeTracker != null)
{
_changeTracker.EntityMemberChanging("EntityKey");
_entityKey = value;
_changeTracker.EntityMemberChanged("EntityKey");
}
else
_entityKey = value;
}
}
===================== Custom Property ====================================

[EdmScalarPropertyAttribute(IsNullable = false)]
public System.DateTime OrderDate
{
get { return _orderDate; }
set
{
if (_changeTracker != null)
{
_changeTracker.EntityMemberChanging("OrderDate");
_orderDate = value;
_changeTracker.EntityMemberChanged("OrderDate");
}
else
_orderDate = value;
}
}
Finally you also need to create the EntityState property as follows:

public EntityState EntityState
{
get { return _changeTracker.EntityState; }
}
The IEntityWithRelationships involves creating a property that returns RelationshipManager object:
public RelationshipManager RelationshipManager
{
get
{
if (_relManager == null)
_relManager = RelationshipManager.Create(this);
return _relManager;
}
}
============================================================

Tip : ProviderManifestToken – change EDMX File to use SQL 2008 instead of SQL 2005

To use with SQL Server 2008, edit the EDMX file (the raw XML) changing the ProviderManifestToken in the SSDL attributes from "2005" to "2008"
=============================================================
With EFv1 we cannot use Structs to replace a anonymous Type while doing projections in a LINQ to Entities query. While the same is supported with LINQToSQL, it is not with LinqToEntities. For e.g. the following is not supported with LinqToEntities since only parameterless constructors and initializers are supported in LINQ to Entities. (the same works with LINQToSQL)

public struct CompanyInfo
{
public int ID { get; set; }
public string Name { get; set; }
}
var companies = (from c in dc.Companies
where c.CompanyIcon == null
select new CompanyInfo { Name = c.CompanyName, ID = c.CompanyId }).ToList(); ;

kick it on DotNetKicks.com

Dec 14, 2009

Split SQL 2005 column values into multiple rows

Suppose that you have a column that stored multiple values in a single row using some delimiter as ‘,’. Now you are tasked with splitting these values into a separate row for each value and you need to do this on multiple rows in the table.

   1: select distinct r.items
   2: from Order o
   3: cross apply dbo.split(o.Products,',') r

Here the Order Table has a Products column that has multiple Products stored in a single column separated with commas like ‘Tea,Coffee’ associated with a single Order. What the above query does is it gives a distinct list of names of all Products stored in all rows in the Order table. The Split() is a UDF which splits the string into one row for each value

Similarly if you need to check if these multiple values stored in the Products column contain a particular string from a list of values, i,e,


   1: select o.* from Company c
   2: join Order o on c.ItemID = o.companyItemID
   3: where (select * from dbo.split(o.Products) in ('TBD','Tea', 'Coffee'))

Since the above would not compile hence the right solution to do something like this would be:


   1: select o.* from Company c
   2: join Order o on c.ItemID = o.companyItemID
   3: cross apply dbo.Split(Products,',') p 
   4: where p.Items in ('TBD','Tea','Coffee')

what we are doing here is a inner join on the Split() table valued UDF which returns a table containing one row for each value split and then comparing each Item to see if it matches one of the product names i.e. Tea or Coffee.

kick it on DotNetKicks.com

Mar 6, 2009

Dispose and Finalize methods and .NET Memory optimizations

I have jotted down a few points on implementing the IDisposable and Finalize patterns in .NET.

Please find it here : Dispose and Finalize methods

I also jotted down some .net Memory Optimization tips: Please find them here : Some Tips on .NET Memory optimizations

kick it on DotNetKicks.com

Mar 3, 2009

Creating dynamically generated logfiles with log4net

If you need to generate logfile names dynmically when using log4net then we can do this using the log4net Properties.

To learn more on this please visit : creating dynamic logfile names with log4net

kick it on DotNetKicks.com

Tips on using log4net RollingFileAppender

I have compiled a list of useful properties that you can set on the RollingFileAppender in the log4net XML config file which can determine the order in which log file backups will be generated, whether they will rollover based on dates or size or both.

For more information please check this link out : Tips on using log4net RollingFileAppender

kick it on DotNetKicks.com

Jan 28, 2009

ConfigSectionHandler for Hierarchical configs

Here is a quick way to create a ConfigSection Handler for reading Hierarchical configs. For e.g. suppose you need to read a config file which has the following structure:

MySpace

23
Rohit
Gupta


For this you would create the 2 classes, one for the Parent Config and another for the chold config like this:

[XmlRoot("MainConfig")]
public class MainConfig
{
private static readonly MainConfig instance =
(MainConfig)ConfigurationManager.GetSection("MainConfig");
public static MainConfig GetInstance()
{
if (instance == null)
throw new ConfigurationErrorsException(
"Unable to locate or deserialize the 'MainConfig' section.");

return instance;
}

public string Company { get; set; }

public SubConfig FriendsConfig { get; set; }
}

[XmlRoot("SubConfig")]
public class SubConfig
{
public int ID { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}

Then you would create a configsectionHandler class which reads the config from the .config file:


public class MainConfigSectionHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members

public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
MainConfig typedConfig = GetConfig(section);

if (typedConfig != null)
{
#region Get Sub Configs
foreach (XmlNode node in section.ChildNodes)
{
switch (node.Name)
{
case "SubConfig":
SubConfig friendsConfig = GetConfig(node);
typedConfig.FriendsConfig = friendsConfig;
break;
default:
break;
}
}
#endregion
}


return typedConfig;
}

public T GetConfig(System.Xml.XmlNode section) where T : class
{
T sourcedObject = default(T);
Type t = typeof(T);
XmlSerializer ser = new XmlSerializer(typeof(T));
sourcedObject = ser.Deserialize(new XmlNodeReader(section)) as T;
return sourcedObject;
}

#endregion

}

After this you would add a entry in the app.config for this configsection Handler as the following:





Finally to read this config, you would write the following:

MainConfig config = MainConfig.GetInstance(); Console.WriteLine(config.Company);
Console.WriteLine(config.FriendsConfig.ID);
Console.WriteLine(config.FriendsConfig.LastName);
Console.WriteLine(config.FriendsConfig.Name);

kick it on DotNetKicks.com

Jan 7, 2009

Lucene: Multifield searches

We can run multifield searches in Lucene using either the BooleanQuery API or using the MultiFieldQueryParser for parsing the query text. For e.g. If a index has 2 fields FirstName and LastName and if you need to search for "John" in the FirstName field and "Travis" in the LastName field one can use a Boolean Query as such:


BooleanQuery bq = new BooleanQuery();
Query qf = new TermQuery(new Lucene.Net.Index.Term("FirstName", "John"));
Query ql = new TermQuery(new Lucene.Net.Index.Term("LastName", "Travis"));
bq.Add(qf, BooleanClause.Occur.MUST);
bq.Add(ql, BooleanClause.Occur.MUST);
IndexSearcher srchr = new IndexSearcher(@"C:\indexDir");
srchr.Search(bq);

Now if we need to search a single term across either of the FirstName and LastName fields then we can use the MultiFieldQueryParser as follows:

Query query = MultiFieldQueryParser.parse("commonName",
new String[] { "FirstName", "LastName" },
new SimpleAnalyzer());
srchr.Search(query);

Now if you need to search the term that must exist in both the fields then we use the following:

Query query = MultiFieldQueryParser.Parse("commonName",
new String[] { "FirstName", "LastName" },
new BooleanClause.Occur[] { BooleanClause.Occur.MUST,BooleanClause.Occur.MUST}
, new SimpleAnalyzer());
srchr.Search(query);

Finally if you don’t want a term to occur in one of the Fields (say FirstName) then use:

Query query = MultiFieldQueryParser.Parse("commonName",
new String[] { "FirstName", "LastName" },
new BooleanClause.Occur[] { BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.MUST},
new SimpleAnalyzer());
srchr.Search(query);

so if you need to search a single term across multiple fields then use MultiFieldQueryParser, if you need to search different terms in different fields then use the BooleanQuery as shown first

kick it on DotNetKicks.com

Dec 1, 2008

Dynamically creating types using reflection and setting properties using Reflection.Emit.

I came across a requirement where we needed to create types dynamically based on XML Configuration files, so that in the furture new types are required we dont need to update the application again by creating a new class.
The additional requirement was to populate the property names of the class based on the Cml configuration and its values using the Querystring values from the HttpWebRequest.
I earlier thought about using Dynamic methods from .NET Framework 2.0, but that did not fit my purpose since I didn't need to execute methods from the dynamic type that was created.

So here is the code:
I created three helper methods
1. Generates the AssemblyBuilder and Module Builder objects
2. Generates the TypeBuilder object.. which will be used for generating an instance of the dynamic type using Activator.CreateInstance
3. Create properties using Relection.Emit
   1: public class DynamicType
   2: {
   3:  
   4:     public static AssemblyBuilder asmBuilder = null;
   5:     public static ModuleBuilder modBuilder = null;
   6:     public static void GenerateAssemblyAndModule()
   7:     {
   8:         if (asmBuilder == null)
   9:         {
  10:             AssemblyName assemblyName = new AssemblyName();
  11:             assemblyName.Name = "DWBeacons";
  12:             AppDomain thisDomain = Thread.GetDomain();
  13:             asmBuilder = thisDomain.DefineDynamicAssembly(
  14:                          assemblyName, AssemblyBuilderAccess.Run);
  15:             modBuilder = asmBuilder.DefineDynamicModule(
  16:                          asmBuilder.GetName().Name, false);
  17:         }
  18:     }
  19:  
  20:     public static TypeBuilder CreateType(ModuleBuilder modBuilder, string typeName)
  21:     {
  22:         TypeBuilder typeBuilder = modBuilder.DefineType(typeName,
  23:                     TypeAttributes.Public |
  24:                     TypeAttributes.Class |
  25:                     TypeAttributes.AutoClass |
  26:                     TypeAttributes.AnsiClass |
  27:                     TypeAttributes.BeforeFieldInit |
  28:                     TypeAttributes.AutoLayout,
  29:                     typeof(object));
  30:         return typeBuilder;
  31:     }
  32:  
  33:  
  34:     public static void CreateProperty(TypeBuilder t, string name, Type typ)
  35:     {
  36:         string field = "_" + name.ToLower();
  37:         FieldBuilder fieldBldr = t.DefineField(field, typ, FieldAttributes.Private);
  38:         PropertyBuilder propBldr = t.DefineProperty(name, PropertyAttributes.HasDefault, typ, null);
  39:         MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
  40:  
  41:         MethodBuilder getPropBldr = t.DefineMethod("get_" + name, getSetAttr, typ, Type.EmptyTypes);
  42:  
  43:         ILGenerator getIL = getPropBldr.GetILGenerator();
  44:         getIL.Emit(OpCodes.Ldarg_0);
  45:         getIL.Emit(OpCodes.Ldfld, fieldBldr);
  46:         getIL.Emit(OpCodes.Ret);
  47:  
  48:         MethodBuilder setPropBldr = t.DefineMethod("set_" + name, getSetAttr, null, new Type[] { typ });
  49:  
  50:         ILGenerator setIL = setPropBldr.GetILGenerator();
  51:  
  52:         setIL.Emit(OpCodes.Ldarg_0);
  53:         setIL.Emit(OpCodes.Ldarg_1);
  54:         setIL.Emit(OpCodes.Stfld, fieldBldr);
  55:         setIL.Emit(OpCodes.Ret);
  56:  
  57:         propBldr.SetGetMethod(getPropBldr);
  58:         propBldr.SetSetMethod(setPropBldr);
  59:  
  60:     }
  61:  
  62: }



Then to use these helper methods from the Main program I used the following:

   1: if (DynamicType.asmBuilder == null)
   2:     DynamicType.GenerateAssemblyAndModule();
   3: finalType = DynamicType.modBuilder.GetType("Beacon11");
   4:  
   5: TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, typeName);
   6:  
   7: foreach (XElement e in beaconNode.Descendants())
   8: {
   9:     string pname = e.Attribute("qs").Value;
  10:     string ptype = e.Attribute("type").Value;
  11:     DynamicType.CreateProperty(tb, pname, Type.GetType(ptype));
  12: }
  13: finalType = tb.CreateType();
  14:  
  15: Object obj = Activator.CreateInstance(finalType);
  16: // this sets the properties of the just instantiated class
  17: finalType.InvokeMember("bv", BindingFlags.SetProperty, null, data, new object[] { 1.0 });
  18: finalType.InvokeMember("tp", BindingFlags.SetProperty, null, data, new object[] { 2.0 });
  19: //this sets the properties of the type by using values from the querystring
  20: foreach (XElement e in beaconNode.Descendants())
  21: {
  22:     string pname = e.Attribute("qs").Value;
  23:     object value = context.Request.QueryString[pname];
  24:     finalType.InvokeMember(pname, BindingFlags.SetProperty, null, data, new object[] { value });
  25: }


For more information visit : msdn - PropertyBuilder

kick it on DotNetKicks.com

Nov 18, 2008

Schedule a task using C# code

I needed to schedule a task to run every day at 9:00 p.m. in the night. I had an addition al requirement that the task be scheduled only if the FileSystemwatcher alerts us of new files being available for processing.

Thus the files could be recieved anytime during the day, but despite that the task should be schduled to run exactly at 9:00 p.m. in the night.

So I used the following code to schedule a task(using System.Threading.Timer and TimeSpan classes)

DateTime d = DateTime.Now;

timer = new Timer(new TimerCallback(Update), null,
TimeSpan.FromMinutes(21 * 60 - (d.Hour * 60 + d.Minute)),
TimeSpan.FromMilliseconds(-1));


Note I used TimeSpan.FromMillisecnods(-1) to disable periodic signalling

kick it on DotNetKicks.com

Nov 13, 2008

Compare 2 SQL queries for equality

DECLARE @check1 bigint
DECLARE @check2 bigint

Select @check1 = CHECKSUM_AGG(CHECKSUM(*))
FROM
(
SELECT *
FROM dbo.Orders (nolock)
)
AS Source

Select @check2 = CHECKSUM_AGG(CHECKSUM(*))
FROM
(
SELECT *
FROM dbo.Orders (nolock)
WHERE IsSuccessful = 1
)
As Comparison

IF @check1 = @check2
PRINT 'Queries are Equal'
ELSE
PRINT 'Queries are NOT Equal'


Note:
If order of rows is different it will not effect the result
It does not do execution plan comparisons, simply checks if the rows returned by the two queries are the same or not

More info

kick it on DotNetKicks.com

Nov 12, 2008

Converting SQL2005 DBTimeStamp to Long for Comparison (Convert from Hex to long and decimal to Hex)

If you need to Convert SQL 2005 DB TimeStamp values to Long then follow these steps:
1. Retrieve the DB TimeStamp value in a Byte Array (8 bytes long).
2. Convert the byte array into a Hexadecimal string
3. Convert the hexadecimal string to a long
private long BytesToLong(byte[] bytes)
{
string ts = null;
foreach (byte b in bytes)
ts += b.ToString("X").PadLeft(2,Convert.ToChar("0"));
return Convert.ToInt64(ts, 16);
}


There is another method to convert the DBTimeStamp to Long, but I dont prefer this method since it can return negative values. The method is:

1. Retrieve the DB TimeStamp value in a Byte Array (8 bytes long).
2. Use BitConverter class to convert the byte array into long
long result = BitConverter.ToInt64(dbTimestamp, 0); 

Inorder to remedy this use the following to generate the same long value as in step 1:


public static long FromDbTimestamp(byte[] dbTimestamp)
{
long result = 0;
if (dbTimestamp != null)
{
Array.Reverse(dbTimestamp);
result = BitConverter.ToInt64(dbTimestamp, 0);
}

return result;
}

Finally to convert a long into the correct DBTimeStamp use this:

public static byte[] ToDbTimestamp(long timestamp)
{
byte[] result = BitConverter.GetBytes(timestamp);
Array.Reverse(result);
return result;
}

kick it on DotNetKicks.com

Oct 13, 2008

Passing state to threads via anonymous calls

We already know that we can pass state into to Threads in 2 ways:

1. using ParameterizedThreadStart delegate

2.using global variables(reference or value types) accessible to the main thread as well the instantiated thread. This is also the way to share data amonst multiple threads. Infact the most common way to share data between threads is using static variables where application wide scope is desired.

3.using Thread.QueueUserWorkItem ... This differs from Parameterized ThreadStart delegate since it uses threads from a preconfigured ThreadPool instead of manually creating threads

There is another cool method to pass information to threads is via anonymous methods. For e.g.
static void Main
{
string mainMessage = "Hello from Main !!!";
Thread t = new Thread(delegate() { Print(mainMessage);});
t.Start();
}
static void Print(string message)
{
Console.Writeline(message);
}

Advantage of using this technique is that, it provides strongly typed access to the parameters passed to the thread. The Target Method can accept any number of arguments, and no casting is required (unlike Parameterized Start where the passed state is an “object”) . The flip side, though, is that you must keep outer-variable semantics in mind

kick it on DotNetKicks.com

Sep 26, 2008

Method Hiding... Polymorphism in C#

In the following code:

interface IBand
{
int ID {get;set;}
string Name {get;set;}
void GetStatus();
}
public class Band : IBand
{
public int ID { get; set; }
public string Name { get; set; }

public Band()
{
GetStatus();
}
public void GetStatus()
{
ID = 555;
Name = "Ring";
Console.WriteLine("Base Class Called");
}
}

public class ABand : Band, IBand
{
public string Update { get; set; }

public ABand() : base() {}

public new void GetStatus()
{
ID = 655;
Name = "TEsst";
Update = "ddd";
Console.WriteLine("Derived class Called");
}
}
class Program
{
static void Main(string[] args)
{
IBand band = new ABand();
band.GetStatus();
}

}

We have 2 classes, Band and ABand which implement the IBand interface explicitly.

Now the call to band.GetStatus in Main will display "Derived class called". This will only happen if the Derived class (ABand) also implements the interface IBand explicitly (not in the literal sense in that it does not implement the methods using the IBand.GetStatus syntax)

If the ABand class does not implement the IBand interface explicitly (but implicitly since it inherits from Band which implements IBand) then the above code will output "Base class called"

However note one thing, that when the CTor for ABand is invoked it will invoke the ctor Band class and which will call GetStatus() method from within Band class hence this will output "Base class called" even though the ABand class has implemented the IBand interface explicilty.

kick it on DotNetKicks.com

Sep 9, 2008

Linq To XML : Check for existence of an element

var bookQuery = 
from book in bookXml.Descendants("Item")

let attributes = book.Element("ItemAttributes")

let price = Decimal.Parse((
book.Elements("OfferSummary").Any()
&& book.Element("OfferSummary").Elements("LowestNewPrice").Any()
? book.Element("OfferSummary").Element("LowestNewPrice").Element("Amount").Value
: (attributes.Elements("ListPrice").Any()
? attributes.Element("ListPrice").Element("Amount").Value
: "0"))) / 100

select new {Price = price};


The trick is to use the Elements(“node”) instead of Element(“node”) and then use the Any() function which will return true if an element of that name exists.

Some other LINQ tips:

Some of the extensions that one can use are Intersect,Union && Except
var list1 = new List<int> { 2, 4, 9, 11, 3, 6 };
var list2 = new List<int> { 3, 8, 4, 30, 9, 16 };
var newlist = list1.Intersect(list2);

This returns : 3,4,9
newlist = list1.Union(list2);
this returns : 2,3,4,6,8,9,11,16,30. finally
newlist = list1.Except(list2);
returns : 2,11,6

kick it on DotNetKicks.com