Tuesday, August 4, 2009

Probing PrivatePath

I recently dented my forehead and keyboard trying to figure out why my probing privatePath was not working. Here is my correctly configured config section.


<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin\subBin"/>
</assemblyBinding>
</runtime>


I was getting this error.
[InvalidOperationException: The type 'MyService.Blah', provided as the Service attribute value in the ServiceHost directive could not be found.]

I could move the culprit DLL up to my bin directory and everything would work but that isn't the way the system is designed.

Turns out what is needed is this little tidbit.

<assemblies>
<add assembly="MyService.Blah"/>
</assemblies>


Once I told .Net what to look for in the bin\subBin directory, life continued on for me and the healing process began, and a new keyboard is now on order. If there are typos, blame it on my now malfunctioning keyboard.

Sunday, February 8, 2009

LINQ - Detaching DataContext

I took the code from here by and modified it so I could just use an extension class instead of having to change the model. Besides, it is more fun to use the new features of .NET 3.5 :)

using System.Data.Linq;
using System.Data.Linq.Mapping;

public static class ExtendedDataContext
{
public static T Detach(this DataContext dc, T entity) where T : class, new()
{
if (entity == null)
return null;

//create a copy of the entity
object entityCopy = new T();

//enumerate the data member mappings for the entity type
foreach (MetaDataMember member in dc.Mapping.GetMetaType(typeof(T)).DataMembers)
{
//skip associations and deferred members
if (member.IsAssociation || member.IsDeferred)
continue;

//copy the member value
member.StorageAccessor.SetBoxedValue(ref entityCopy, member.StorageAccessor.GetBoxedValue(entity));
}

return (T)entityCopy;
}
}



Code in use
User detachedUser = ((DataContext)db).Detach(user);
db.Users.Attach(detachedUser, true);
db.SubmitChanges();