Tuesday, November 15, 2011

KnownTypeAttribute & ServiceKnownTypeAttribute...

Recently I was debugging a problem with below error:

There was an error while trying to serialize parameter http://tempuri.org/:GetProcessResult. The InnerException message was 'Type BusinessObject2' with data contract name 'BusinessObject2:' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

I found the detailed explanation of what actually I am missing here from following links:

It solved my issue of inheritance DTO issue in SOA world!

#trivedimehulk@gmail.com

Tuesday, September 13, 2011

An error occurred creating the configuration section handler for exceptionHandling: Could not load file or assembly ‘Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ or one of its

Today I was exploring Enterprise Library 4.1 and encounter this error. I created a sample application and added reference to few DLL from "EntLib41Src¥bin" folder to my application. When I hit F5 to run the application I got following error,
"An error occurred creating the configuration section handler for exceptionHandling: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0×80131040)"
      While searching for the reason I found a link here. According to this link assemblies in "EntLib41Src¥bin" folders are not strong name assemblies. So when you run the application you will encounter error as shown above. While "Program Files¥Microsoft Enterprise Library 4.1 – October 2008¥Bin" folder contains strong name assemblies. You have to add reference from "Program Files¥Microsoft Enterprise Library 4.1 – October 2008¥Bin" folder in your application.
Happy Debugging !!
#trivedimehulk@gmail.com

Thursday, September 1, 2011

Find column from T-SQL...

SELECT TABLE_NAME
ORDINAL_POSITION
,COLUMN_NAME
,DATA_TYPE
,CHARACTER_MAXIMUM_LENGTH
,IS_NULLABLE
,COLUMN_DEFAULT,*
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME like '%patientrespons%'
ORDER BY
ORDINAL_POSITION ASC;
#trivedimehulk@gmail.com

Wednesday, August 3, 2011

An easy code to serialize and de-serialize objects in .net....


An easy code to serialize and de-serialize objects in .net….
        public static object ConvertXmlToObject(string deserializedXml, System.Type ObjType)
        {
            XmlSerializer ser;
            object obj = null;
            try
            {
                ser = new XmlSerializer(ObjType);
                StringReader stringReader;
                stringReader = new StringReader(deserializedXml);
                XmlTextReader xmlReader;
                xmlReader = new XmlTextReader(stringReader);
                obj = ser.Deserialize(xmlReader);
                xmlReader.Close();
                stringReader.Close();
            }
            catch (Exception exp)
            {
Throw; 
            }
            return obj;
        }
        public XmlDocument ConvertObjectToXmlDocument(object obj, System.Type objectType)
        {
            XmlDocument doc;
            try
            {
                XmlSerializer s = new XmlSerializer(objectType);
                StringWriter sw = new StringWriter();
                XmlTextWriter xw = new XmlTextWriter(sw);
                s.Serialize(xw, obj);
                doc = new XmlDocument();
                doc.LoadXml(sw.ToString());
            }
            catch (Exception exp)
            {
Throw; 
            }         
            return doc;
        }
#trivedimehulk@gmail.com

Thursday, June 23, 2011

opening a window in dual monitor next monitor using javascript...


I was running through a requirement in which client needs to make sure the payment bill window is opened in next monitor if user is using dual monitor.
Sounds tricky in web world right? Without any controls / active X?
After running through some research (google research) I understood that its just you move your  IE window out of your screen resolution and it will start appearing in next monitor hehe!
Use following javascript to help…
<script>
function popup_params(width, height) {
    var a = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
    var i = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
    var g = typeof window.outerWidth!='undefined' ? window.outerWidth : document.documentElement.clientWidth;
    var f = typeof window.outerHeight != 'undefined' ? window.outerHeight: (document.documentElement.clientHeight - 22);
    var h = (a < 0) ? window.screen.width + a : a;
    var left = parseInt(h + ((g - width) / 2), 10)+1500;
    var top = parseInt(i + ((f-height) / 2.5), 10)+1500;
    return 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=1';
}  
window.open("http://google.com", "windowname", "location=1,toolbar=0," + popup_params(300, 300));
</script>
#trivedimehulk@gmail.com

Tuesday, June 21, 2011

"Error in line 1 position 2130. Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. "

“Error in line 1 position 2130. Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''.”
 
I was working on a BT+WCF integration project and I was continuously getting this error though I thought I did all right.
 
At times google is helpful but this time, it could not help me much. May be I tried wrong search key words. So I digged myself into what actually is happening and found out that this error means your service at some position expects element whereas you are passing something else.
 
For my case for instance my service was expecting a schema like below…
 
<student>
        <schoolInfo>
                <schName></schName>
</schoolInfo>
</student>
 
Instead I was passing
<student>
        <schoolInfo>My school</schoolInfo>
</student>
 
This was a fault in my biztalk map where I by mistake dropped a string to a element root which BT does not validate.
 
I removed the thing and mapped to correct leaf and it fixed the piece!
 
trivedimehulk@gmail.com
 
 

Wednesday, June 8, 2011

Date conversion issue in BT when parsing to WCF data contract....

There are many situations in BT applications where you receive the date as “20110506” (yyyymmdd) and when you pass such date to a date time variable to a WCF reference it will throw a conversion error at .net WCF channel level which you can look into a trace log.
 
Now to overcome this, you can use below function in script fuctoid to convert it into the date format that .net understands.
 
public string MyConcat(string param1)
{
        return System.DateTime.ParseExact(param1, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd");
}
 
trivedimehulk@gmail.com
 
 

Friday, June 3, 2011

"message part data does not contain at least one of the node"

Error: “…message part data does not contain at least one of the node….”
 
While working on BizTalk projects sometimes, when you want to manipulate a schema output (with promoted/distinguish properties), when you normally try to assign the property values in orchestration you get errors like above.
 
Easiest fix to such scene is:
 
  1. Have your schema distinguished properties
  2. Initialize a temporary variable with following syntax in orchestration expression builder
 
TempDoc=new System.Xml.XmlDocument();
TempDoc.LoadXml("<ns0:OutMsg StdName='' xmlns:ns0='http://PropSchema.OutSchema' /> "); //you can generate whole of this middle portion for your schema by using generate instance ;)
Out=TempDoc;
Out.StdName="Mehul";
 
  1. Try to access properties and assign some value.
 
 
Base rule for BT is you can’t directly access any object or its properties til its loaded in memory using a map or an intializer as shown above. J
 
Mehul Trivedi
trivedimehulk@gmail.com
 
 
 

Monday, May 23, 2011

A simple javascript for face book share button...

javascript:var a=window.open('http://www.facebook.com/sharer.php?u='+prompt('Enter URL',window.location.href)+'&t='+prompt('Enter Title',''),'sharer','toolbar=0,status=0,width=626,height=436');
 
copy and paste above in your browser and hit enter J
 
Mehul Trivedi
trivedimehulk@gmail.com
 
 
 

Friday, May 20, 2011

Some useful instant javascripts for web developers....

All these scripts must run in browser address bar.
 
  1. This will ask you to enter the javascript code you want to execute from browser against current page
 
javascript:var a=eval(prompt('Enter JS'));
 
  1. This will show you cookies planted by current site
 
javascript:alert((document.cookie));
 
  1. This would show you the size of viewstate generated on an ASP.NET page
 
javascript:alert(document.getElementById(prompt('Enter variable name:','__VIEWSTATE')).value.length/1024 +'kb');
 
  1. This would resize your browser to standards 1024 layout to check the page layout constraints
javascript:window.resizeTo(1024,768);
 
I normally keep them handy in favorites!
 
 
Mehul Trivedi
trivedimehulk@gmail.com
 
 
 

Friday, May 13, 2011

SOA - convert any exception to a fault exception for your clients...

Mates,
 
I have been reading an interesting article that struck my mind while designing an SOA architecture in .NET 3.5/WCF services.
 
The objective was to convert all the exception (type any) as a service host, convert them to a generic fault contract type and send it to client, so that they can process it.
 
I found an easy way by using Microsoft Application Blocks and “Fault Contract Exception Handler”, we can actually trap the exception using MABs, transform / map the fields from exception to the custom fault contract we have defined and reThrow it J
 
 
Mehul Trivedi
 
 
 
 
Mehul Trivedi
Office: +1 (954) 514-4667
 
 
 

Tuesday, May 10, 2011

How to use Microsoft Enterprise Library Exception Handling and Logging Application blocks to log exceptions in rolling log files

  1. Configure your configuration file.
  1. Add exception handling block
  2. Add exception policy
  3. Add exception type
  4. Add exception logging handler
  5. Modify the trace listener by removing the default to a rolling log file
  6. Configure all the values

  1. Add reference of MABs (exception and logging) in your app as shown below

  1. Handle the exception…

Running the code would generate the rolling log file in your web application directory.
trivedimehulk@gmail.com

A simple javascript solution to trap onblur before it happens... IE only...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script>
var onblurf = false;
function acmeup() {
if (window.event.keyCode == 9) {
onblurf = true;
} else {
onblurf = false;
}
}
function acme(e) {
if (onblurf) {
alert('fired on tab out');
}
}
</script>
<input type="text" id="txtname" onblur="acme(event);"
onkeydown="acmeup();"
/>
</body>
</html>
 
trivedimehulk@gmail.com
 
 
 

Tuesday, May 3, 2011

How to create C# data transfer object (DTO) class from a given XML?

  • Have your xml ready
  • On visual studio command prompt
 
        xsd <ur xml file path> - this will generate xsd
        xsd <ur just generated xsd file> /classes- this will generate cs
 
and finally use following sample code to load xml
 
StreamReader str = new StreamReader("cd_catalog.xml");
      XmlSerializer xSerializer = new XmlSerializer(typeof(CATALOG));
 
      CATALOG myCdCatalogue = (CATALOG)xSerializer.Deserialize(str);
 
      foreach (CATALOG.CDRow cd in myCdCatalogue.CD)
      {
        Console.WriteLine(cd.TITLE);
        Console.WriteLine(cd.ARTIST);
        Console.WriteLine(cd.COUNTRY);
        Console.WriteLine(cd.COMPANY);
        Console.WriteLine(cd.PRICE);
        Console.WriteLine(cd.YEAR);
        Console.WriteLine();
      }
      str.Close();
 
>>> xml to C#, C#, .net 3.5, XML
 
trivedimehulk@gmail.com
 
 

External configuration source issue in Unit Testing in Visual Studio...

The unit test project in visual studio by default does not support external configuration file. Basically when you debug / run your tests the external configuration files you have used are not copied over to out directory.
 
The simplest work around to this is as follows:
 
  1. Add a test run configuration file at solution level
  2. Select deployment leaf in the test run configuration
  3. Add extra files that you want to be copied over to out folder in test run config
 
Now you should have the extra files being copied over to output directory making the test run successfully.
 
trivedimehulk@gmail.com