Tuesday, December 28, 2010

W

WebUtility Class

.NET Framework 4

Provides methods for encoding and decoding URLs when processing Web requests.


 NameDescription
Public method Static memberHtmlDecode(String)Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
Public method Static memberHtmlDecode(String, TextWriter)Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to aTextWriter output stream.
Public method Static memberHtmlEncode(String)Converts a string to an HTML-encoded string.
Public method Static memberHtmlEncode(String, TextWriter)Conver

Monday, December 13, 2010

ASP.NET MVC + Entity Framework: The type or namespace name 'Entity' does not exist in the namespace 'System.Data'

add

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

to Web.Config

Using the Entity Model in a Console Application

Using the Entity Model in a Console Application
We will now use the model we created above, in a Console Application.
Step 1: Open Visual Studio 2010 > File > New Project > In the templates, select Windows > Console Application. Make sure the target framework is ‘.NET Framework 4.0’. I have called the application ‘ConsoleAppUsingMyModel’. Click Ok.
Step 2: Let us now add a reference to the model we created earlier, in this console application. Right click the project > Add Reference > Browse Projects Tab and browse to the MyEntityModel project. Go to the 'bin' folder > Release and select the MyEntityModel.dll as shown below. Click OK.

Entity Model 
Also make sure you add a reference to the System.Data.Entity, this time using the .NET tab

System.Data.Entity 
Step 3: In this final step, our console application needs to have the information about the connection string which contains the metadata and details of the database connection.
Note: If you skip this step and directly query the model, you will get an error The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Now you can either create your own .config file in this project and add the connectionstring entry of your model or simply copy the App.config file that you created in the MyEntityModel project, in your Console App project. We will adopt the simpler way. Right click the project > Add Existing Item > Go to the MyEntityModel project and add the App.config file.
Step 4: Once the steps shown above are completed, you now need to query against the model

Friday, December 10, 2010

Sqlite & Entity Framework 4


To get it working, you need to include the following code in your web.config file:
1.<startup useLegacyV2RuntimeActivationPolicy="true">
2.<supportedRuntime version="v4.0" />
3.</startup>
And then everything magically works.  

Friday, December 3, 2010

c# sqlite


System.Data.SQLite is the original SQLite database engine and a complete ADO.NET 2.0/3.5 provider all rolled into a single mixed mode assembly.  It is a complete drop-in replacement for the original sqlite3.dll (you can even rename it to sqlite3.dll if you're using it natively).
Here is a brief overview of its features:
Complete ADO.NET 2.0 Implementation
The provider was written from scratch on VS2005/2008 specifically for ADO.NET 2.0, using all the most recent changes to the ADO.NET framework.  That includes full DbProviderFactory support, automatic distributed transaction enlistment, connection pooling, extensive schema support, Entity Framework support and more.
Supports the Full and Compact .NET Framework as well as native C/C++
Whether you're programming in .NET or straight C/C++ on the desktop or a mobile device, we've got a build for you.
Mono support
A managed-only version of the provider is also available that works on Mono against the official SQLite library fromhttp://www.sqlite.org/.  Requires 3.6.1 or higher.
Support for the ADO.NET 3.5 Entity FrameworkSupports nearly all the entity framework functionality that Sql Server supports, and passes 99% of the tests in MS's EFQuerySamples demo application.
Visual Studio 2005/2008 Design-Time SupportYou can add a SQLite connection to the Server Explorer, create queries with the query designer, drag-and-drop tables onto a Typed DataSet and more! SQLite's designer works on full editions of Visual Studio 2005/2008, including VS2005 Express Editions.
* NEW You can create/edit views, tables, indexes, foreign keys, constraints and triggers interactively within the Visual Studio Server Explorer!

MSMQ – Sending Messages to Remote Queues

The situation is simple – you want to send a message to a remote MSMQ. To do so you might write some code that looks something like:
1.using (var queue = new MessageQueue(@"FormatName:Direct=TCP:192.168.2.157\Private$\TheQueue"))
2.{  
3.var message = new Message(shipment);
4.message.Formatter = new BinaryMessageFormatter();
5.queue.Send(message, MessageQueueTransactionType.Single);   
6.}

This code works fine... most of the time. The problem is that, for remote queues, MSMQ sends messages in a "fire and forget" type of mode by default, i.e. if the message gets there, great! If not, oh damn well. This was not apparent to me at first. In workflow situations where you cannot let things "slip through the cracks" it is critical that you receive acknowledgement of delivery. For example, consider the following pseudo-code:

1.using (var transaction = new TransactionScope())
2.{
3.// Perform numerous database operations here
4. 
5.// Send message to remote queue here
6. 
7.transaction.Complete();
8.}

What if the send operation to the remote queue does not succeed? Well, in a workflow with state tracking, we absolutely must rollback all of the database operations and leave the system in the exact same state that existed before we attempted the send operation. The only way we can do this is to know whether the message arrived in the remote queue. Thankfully MSMQ provides a fairly easy way to accomplish this through the use of an Administrative Queue. The purpose of this queue is to provide a "callback" location for the target queue to send an acknowledgement message. This message is uniquely identified by its Message ID property. The high-level steps for guaranteed delivery are:

1) Create a message and define an Administrative Queue 
2) Send the message 
3) Wait a little 
4) Check the Administrative Queue for the message's unique ID
The code to do this is:
01.using (var queue = new MessageQueue(@"FormatName:Direct=TCP:192.168.2.157\Private$\TheQueue"))
02.{
03.var message = new Message(shipment);
04.message.Formatter = new BinaryMessageFormatter();
05.message.AdministrationQueue = new MessageQueue(@"FormatName:Direct=TCP:192.168.2.148\Private$\AdminQueue");
06.message.AcknowledgeType = AcknowledgeTypes.PositiveArrival;
07.queue.Send(message, MessageQueueTransactionType.Single);
08. 
09.Thread.Sleep(100);
10. 
11.bool acknowledged = ReceiveAcknowledgment(message.Id, @".\Private$\AdminQueue");
12. 
13.if (!acknowledged)
14.{
15.throw new InvalidOperationException("Acknowledgement was not received");
16.}
17.}
Yes, the above code uses a Thread.Sleep call. I realize that this is somewhat of an anti-pattern but it is called for here as you have no way of gauging network latency.  The best you can really do is supply a sleep time that is relative to your queue architecture, i.e. if your queues are on an Intranet then you can probably get away with 100 ms (or less).  If you’re using queues over the Internet then you will probably need a longer wait time.  This is the price you pay for guaranteed delivery acknowledgement – a small one in my opinion.
The "ReceiveAcknowledgment" helper method is defined as follows:
01.private static bool ReceiveAcknowledgment(string messageId, string queuePath)
02.{
03.var queue = new MessageQueue(queuePath);
04.queue.MessageReadPropertyFilter.CorrelationId = true;
05.queue.MessageReadPropertyFilter.Acknowledgment = true;
06. 
07.while (queue.PeekByCorrelationId(messageId) != null)
08.{
09.Message message = queue.ReceiveByCorrelationId(messageId);
10.return true;
11.}
12. 
13.return false;