Tips and Tricks in a world of Mix

Archive for November, 2010

Diagnosing Errors with Managed Debugging Assistants – MDA Exception handling

When your code is executing in the Visual Studio integrated development environment (IDE), you can avoid the exception dialog box that appears for specific MDA events. To do that, on the Debug menu, click Exceptions. (If the Debug menu does not contain an Exceptions command, click Customize on the Tools menu to add it.) In the Exceptions dialog box, expand the Managed Debugging Assistants list, and then clear the Thrown check box for the individual MDA. For example, to avoid the exception dialog box for a contextSwitchDeadlock MDA clear the Thrown check box next to its name in the Managed Debugging Assistants list. You can also use this dialog box to enable MDAs.

So if you are getting something like this – PinvokeStackImbalance when you are using some invoke – such as
MethodInfo mi = rs.GetType().GetMethod(“YourUmanagedFunction”,
new Type[] { typeof(int), typeof(string), typeof(StringBuilder) });
mi = mi.MakeGenericMethod(typeof(TEntity));

//TODO: Implement List
results = (TEntity)mi.Invoke(rs, new object[] { 110, this.Path, query });

You should unmark it in the Debug-> Exceptions -> Managed Debugging Assistants :

That should solve the problem!

Advertisements

Instantiate a new generic object inside a generic class

Suppose you want to instantiate a new generic object inside a generic class. The problem is the C# compiler does not know whether the type argument the client will use has a matching constructor, and it will refuse to compile the instantiation line.

To address this problem, C# allows you to constrain a generic type parameter such that it must support a public default constructor. This is done using the new() constraint. For example, here is a different way of implementing the default constructor of the generic Node from Code block 3.

class Node where T : new()
{
public K Key;
public T Item;
public Node NextNode;
public Node()
{
Key = default(K);
Item = new T();
NextNode = null;
}
}
You can combine the constructor constraint with derivation constraint, provided the constructor constraint appears last in the constraint list:

public class LinkedList where K : IComparable,new()
{…}

TrustAllCertificatePolicy – Handles a call to a web service via SSL that does not have a valid server certificate

    
/// <summary>
    /// Handles a call to a web service via SSL that does not have a valid server certificate
    /// </summary>
    /// <example>
    /// locate this code at the authorization class before validating the web service
    /// System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
    /// </example>
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        { }

         public bool CheckValidationResult(ServicePoint sp,
         X509Certificate cert, WebRequest req, int problem)
        {
            return true;
        }
    }

Check URL Availability

Private WebServiceStatusCode As String

Public Function ConnectionAvailable(ByVal strServer As String) As Boolean
Try
Dim reqFP As HttpWebRequest = DirectCast(HttpWebRequest.Create(strServer), HttpWebRequest)

Dim rspFP As HttpWebResponse = DirectCast(reqFP.GetResponse(), HttpWebResponse)
If HttpStatusCode.OK = rspFP.StatusCode Then
' HTTP = 200 - Internet connection available, server online
WebServiceStatusCode = rspFP.StatusCode.ToString()
rspFP.Close()
Return True
Else
' Other status - Server or connection not available
WebServiceStatusCode = rspFP.StatusCode.ToString()
rspFP.Close()

Return (False)
End If
Catch generatedExceptionName As WebException
' Exception - connection not available
WebServiceStatusCode = generatedExceptionName.Message
Return False
End Try
End Function

Fluent Nhibernate Trigger-Identity problem

In our case we work with Oracle 10g with FluentNhibernate 1.0.0.597 and NHibernate 2.1.2.4000 .
We have a legacy system based on the db used in our new website .
It means any change on the db may influence the old system.
When we mapped through GeneratedBy.Sequence(“My_Sequence”) , the sequence progressed but it progressed twice – once by NHibernate and once by the trigger on the table.
solution :

Free PostSharp 1.5 doesn’t work with Framework 4

Well , that is a very big disappointment ! The PostSharp 1.5 doesn’t work anymore !

The only solution is to buy PostSharp 2 or some illegal action about the dll’s of 1.5.

Nevertheless PostSharp 1.5 can work with VS 2010 with Framewrok 3.5 so if you are not using dynamics or some fancy sample architecture using only Framework 4 , you still can enjoy this genius  framework of PostSharp.

You must enter the project file and add those lines :

Stand on your project –> Unload Project –> Edit cproj  ->

  1: <PropertyGroup>
  2:   <PostSharpUseCommandLine>True</PostSharpUseCommandLine>
  3:   <DontImportPostSharp>True</DontImportPostSharp>
  4: </PropertyGroup>

and also :

  1: <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
  2: <Import Project="C:\Program Files\PostSharp 1.5\PostSharp-1.5.targets" />

That will enable you to work with PostSharp 1.5 in VS 2010.

P.S.

If anyone has a solution for PostSharp 1.5 with Framework 4 it would be just splendid . Please send me a reference or an explanation..

Calling a WCF service from Javascript – very good

source : http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html

In a previous post I pointed out how to call an ASP.NET ASMX web service from javascript in the browser. Although this still works fine, the world has moved on to .NET 3.5 and WCF services have now replaced ASMX services. Luckily, these are also callable from javascript although the procedure is now a little bit different. In this sample I presume you can use Visual Studio 2008 and know a little about WCF.

(more…)

Tag Cloud

%d bloggers like this: