Dot Net Tips and Tricks

Custom Search

C# 3.0 LINQ Wow!

I just read this months article in the MSDN magazine (C# Evolution) and I’m excited about this addition to C-Sharp in the next version. I have worked as a DBA and find adding the from, where, select, orderby, group by, let, and join to objects in .NET most interesting. Alot of times, I’ve used techniques like this to accomplish the goals but the verbage becomes lengthy. C# 3.0 solves this problem.

LINQ has the following parts:

  • Lambda Expressions are a language feature that is similar in many ways to anonymous methods. The basic idea is that you can treat code as data.
    • StaticMethods.Where(customers, c => c.ZipCode == 91822);
  • Extension methods are basically static methods that are callable through an instance syntax.
    • The above Where method would be an Extension Method. It would be exposed on an instance member though. customers.
    • public static IEnumerable<T> Where<T>(this IEnumerable<T> items, Func<T, bool> predicate)
  • Anonymous Types and Implicitly Typed Local Variables can be used together to specify types “On the fly” inside a method.
    • Instead of create a placeholder class (Customer name and address) for each time you want to pull something out of a collection, you declare a var variable and use an anonymous type to populate it.
    • val custAddress = new { FullName = c.FirstName , HomeAddress = c.Address };
  • Object initializers basically allow the assignment of multiple properties or fields in a single expression.
    • Create an instance of a class without overloading the constructor a million times.
    • Customer customer = new Customer() { Name = bob, Address = Wilco Way };
  • These all lead up to Query Expressions. SQL like ways of accessing .NET objects.
    • var locals = from c in customers

      where c.ZipCode == 91822

      select new { FullName = c.FirstName, HomeAddress = c.Address };

Coming from both a coding and dba background, I find these enhancements in “Orcas” wonderful. It could save a ton of time in coding.

Do yourself a favor and go checkout C# 3.0 and “Orcas”.

No comments

VisualSVN and Windows Workflow Problem

After installing the V1.2 upgrade, I’ve found that any solution with Windows Workflow used will not open and will crash Visual Studio 2005. I’ve submitted a request to their site and will post back more when I have it.

05/09/2007 –>A bug fix release has been release: V1.2.1 that now works with Windows Workflow projects.

No comments

Windows Workflow Foundation and ThreadSafety

When adding ExternalDataExchange services to the runtime it needs to be noted that theses methods are all NOT threadsafe. The runtime will allow you to add multiple instances of the same exchange service to the runtime, but when you call the GetService<T>() method on the runtime, an exception will be thrown. The following will protect you against these problems.

private static object _lockObject = new object();

T service;
lock (_lockObject)
{
service = (T) defaultService.GetService(typeof (T));
if (service == null)
{
service = (T) TypeLoader.Load(typeof (T).AssemblyQualifiedName);
if (service == null)
{
throw new ArgumentException(EXCHANGE_MANAGER_NOT_LOADED);
}
defaultService.AddService(service);
}
}

No comments

Windows Workflow Call External Method and IClonable

Here’s a little gotcha that Microsoft put in Windows Workflow Foundation.

The CallExternalMethod activity is defined as

Defines a workflow communication activity that is used to call a method on a local service. This activity is used to send data from the workflow to the host through the local service.

You select the interface of the service where the method exists and then select the method name. The designer automatically creates properties that correspond to the parameters of the method to call. You can then double-click the little blue “Bind Property” tag in the property window. This allows you to bind the property to any member in the workflow of the same type. This all seems great, until you use it at runtime. What Microsoft fails to state is if the object you bind to impliments IClonable, at execution time, the object will be Cloned then passed to the service’s method. Noware else can I find where an object is Cloned prior to being passed.

I’m still struggling to find the reason for this one, but it’s a gotcha for sure since the documentation states nothing about it.

1 comment

File Tracing in Windows Workflow Foundation

After fighting with the windows workflow (WWF) runtime for a few hours, I finally found and article in the WIKI that showed the tracing switches changed in the released version.

The following will log everything to a file on the computer where the runtime is located:

<system.diagnostics>
<switches>
<add name=”System.Workflow LogToTraceListeners” value=”1″ />
<add name=”System.Workflow.Runtime.Hosting” value=”Verbose” />
<add name=”System.Workflow.Runtime” value=”Verbose” />
<add name=”System.Workflow.Runtime.Tracking” value=”Verbose” />
<add name=”System.Workflow.Activities” value=”Verbose” />
<add name=”System.Workflow.Activities.Rules” value=”Verbose” />
</switches>
<trace autoflush=”true” indentsize=”4″>
<listeners>
<add name=”myListener”
type=”System.Diagnostics.TextWriterTraceListener”
initializeData=”D:\Remoting\TextWriterOutput.log” />
</listeners>
</trace>
</system.diagnostics>

And contrary to most of the postings on the internet, this is current for the RTM release of Windows Workflow Foundation.

No comments

How to use Windows Certificate Authority to Strong Name Assemblies

I recently wanted to use the internal Certificate Authority for all our code signing requirments. After several hours of pain I was able to get it to work but thought I’d document the procedures so other’s won’t have the same problems.

  1. Ensure a Certificate Authority is installed and running on the Active Directory domain.
  2. The windows certificate authority uses a web based request form so open IE and browse to the site. (ex http://someserver/certsrv)
  3. When requesting the certificate, you will need to choose advanced for certificate type and select code signing as the type. Also ensure you choose Microsoft Enhanced Cryptographic Provider and not the Basic or RSA provider. (I’ll explain below) Figure 1
  4. Choose to Mark Keys as Exportable and choose a password. Save the file with a *.pvk extension.
  5. Click submit. You will be prompted to save the pvt and then click to save the certificate. Do not save the full certificate path as strong naming does not support certificate chaining.
  6. Open a Visual Studio command prompt and navigate to the location you saved the files.
  7. Type cert2spc yournewfile.cer yournewfile.spc where yournewfile is the filename you gave the pvk when saving it. This will convert the cer to a compatible spc file.
  8. Type pvk2pfx -pvk yournewfile.pvk -spc yournewfile.spc A wizard will open, follow prompts making sure to check the box to make the private keys exportable.
  9. The new *.pfx file can be used to sign assemblies and the computers inside the network will be able to verify the certificate against the internal Certificate Authority.

You will receive the dreaded “Error Importing Key – Object already existing” error when trying to compile if you choose the wrong provider, ie the provider is incompatible with Visual Studio code signing.
Figuring this was a straight forward procedure, I requested a certificate and private key pair from the Certificate Authority and that’s where the trouble started.

6 comments

Mantis bugtracker

We have recently installed Mantis bugtracking system which integrates nicely with Subversion. The previous post talked of BugId feature in TortoiseSVN and we use this to tie the committing of code with a bug. I’ve added the following code to a PostCommit.bat hook in the Subversion repository.

REM Post-commit hook for MantisBT integration SET REPOS=%1 SET REV=%2 SET DETAILS_FILE=D:\PHPUploadTemp\svnfile_%REV% SET LOG_FILE=D:\PHPUploadTemp\svnfile_%REV%_Log echo ****** Source code change ******>>%DETAILS_FILE% svnlook log -r %REV% %REPOS%>>%DETAILS_FILE% echo SVN Author:>>%DETAILS_FILE% svnlook author -r %REV% %REPOS%>>%DETAILS_FILE% echo SVN Revision:%REV%>>%DETAILS_FILE% svnlook diff -r %REV% %REPOS%>>%DETAILS_FILE% "D:\Progra~1\PHP\php.exe" D:\Progra~1\Apache~1\Apache2\htdocs\Tracker\core\checkin.php

No comments

VisualSVN Visual Studio Plugin for SVN

We have now moved to the VisualSVN product for a Subversion plugin for Visual Studio. Ankh did not support BugId integration. VisualSVN uses TortoiseSVN as the backend for connecting to the Subversion repository. If you’re using Subversion in a Visual Studio environment, due yourself a favor and try VisualSVN.

No comments

Follow-up to Subversion Conversion

Well 2 weeks after the migration from Visual SourceSafe to Subversion, we are very pleased with the results. It has cut our remote developer’s download times by 70%. The merge capabilities in TortoiseSVN are outstanding. Below you will find links to all the resources we used to move the data from VSS and the executables for the software.

If you’re looking for a solid source control at a great price, Subversion is the way to go.

No comments

Source Control Alternatives to VSS

We have been looking at alternatives to Visual SourceSafe over the last few weeks and we’ve settled on the open source solution called Subversion. This product is the successor to CVS and adds a good set of features. We’ve documented a 8x improvement in remote performance when doing full gets of the entire project structure. There are many good open source clients: TortoiseSVN, SmartSVN, VisualSVN etc. Several have good IDE plugins providing the same or close to the feature set of the SourceSafe plugin. Also the VSSMigrate script , here was very easy to move all of the files and history from VSS to the new SVN repository.

For those looking for an alternative to VSS, due yourself a favor and look at Subversion. Best of all the price is just right, FREE!

No comments

« Previous PageNext Page »