2007年7月26日

[.Net] Reflection for Generic Class

To dynamic create an object instance of generic type. Here's the sample code:

namespace GenericTypeNS{
public class GenericType{
......
}
}
...

Assembly asm = Assembly.LoadFrom(asmPath);
object o = asm.CreateInstance("GenericTypeNS.GenericType'1[[%TypeName%]]");

Replace %TypeName% to the specificed class name, for example if we want to create an GenericType object instance with System.Int32, it should look like this:

object o = asm.CreateInstance("GenericTypeNS.GenericType'1[[System.Int32,
mscorlib,Version=2.0.0.0,Culture=neutral,
PublicKeyToken=b77a5c561934e089]]");

2007年7月25日

[ASP.Net] CS0433 Error

When deplpying ASP.Net web site, you might get a CS0433 error with the following message:

Type [TypeName] already exists in both %ASP.Net Temp Folder%\[AssemblyName.dll] and %ASP.Net Temp Folder%\[AssemblyName.dll]%

This is confirmed a well known bug in ASP.Net.

The reason you got this error is that some of your ASP.Net pages/controls are cross referenced.
Say we have a ASP.Net web application with the following file structure:


AppRoot\Dir_a\PageA.aspx
AppRoot\Dir_a\ControlA.ascx
AppRoot\Dir_b\PageB.aspx
AppRoot\Dir_b\ControlB.ascx

If in PageA.aspx references ControlB.ascx in folder Dir_b, and PageB.aspx references ControlA.ascx in folder Dir_a then CS0433 error well be reported.

There are 2 ways to avoid this kind of error, one is to re-structure your file structure to prevent the cross reference. The other one is to add the following configuration setting in your web.config:

<configuration>
<system.web>
<compilation batch="false">
...


The reason we have to disable batch compilation is that when batch compile is enabled (ASP.Net default), ASP.Net compile each folder to an individual dll. So in our case, Dir_A/Dir_B will be compiled into 2 dlls, and it's clearly that they do cross reference.

2007年7月24日

[BizTalk]Message engine failed to process a message submited by adapter ...

When implementing custom pipeline components, sometimes we might get this error after we hook the component onto the pipeline:

The Messaging engine failed to process a message submitted by adapter:FILE...
Details:The published message could not be routed because no subscribers were found


This most likely cuase by some missing content properties we forget to promote after we created a new message and return it to the BizTalk engine. If those missing content properties participated in message routing, then the routing failed and you got above error message.

To correct this, we need add following codes in our custom pipeline to ensure every promoted properties in the origional message content is also promoted in newly created message content:


for (int iProp = 0;iProp < originalMsgContext.CountProperties;iProp++)
{

string strName;
string strNSpace;
object val = originalMsgContext.ReadAt(iProp,
out strName, out strNSpace);
// promote properties
if (originalMsgContext.IsPromoted(strName, strNSpace))
newMsg.Context.Promote(strName, strNSpace, val);
else
newMsg.Context.Write(strName, strNSpace, val);
}

string systemPropertiesNamespace = "http://schemas.microsoft.com/BizTalk/2003/system-properties";
string messageType = "http://testDisassmbler.testSchema#Tables";

newMsg.Context.Promote("MessageType", systemPropertiesNamespace, messageType);
return newMsg;

[BizTalk]Change character encoding in BTS pipeline

To change character encoding in BizTalk pipeline, first we have to know how BizTalk determined the character encoding of an incoming/outgoing message.

BizTalk Server 2006 uses following rules to find out the encoding of a message:
  1. If Byte Order Mark exists, then the encoding is determined
  2. If content property BodyPart.CharSet is set, then the encoding is determined
  3. If xml declaration exists, then the encoding is determined
  4. If none of above matches, UTF-8 is used

So to change message encoding, here are several ways:

  1. Add byte order mark
  2. Set BodyPart.CharSet content property
  3. Add Xml declaration

So it is NOT enough if you only change the actual data in the stream to specificed encoding, one of the above must be matched!

2007年7月23日

[SQL]Not associated with a trusted SQL Server connection

We had a integration issue while building a operation web to manage BizTalk Server 2006 comfigurations. Bellow is our environment:



  • BizTalk Server 2006 installed on Machine1
  • SQL Server installed on Machine2
  • OpWeb installed on Machine1, Using Forms authentication mode
  • Both machines in same domain
We want to configure BizTalk Server 2006 via op_web.The problems here is that I keep getting this error:
[Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'. Reason: Not associated with a trusted SQL Server connection]



Obviously it's a complaint about the user has no access to the database server. But, why it's trying to connect to database server using the Anonymous Logon ?

When connecion to a database server which using windows authcentication mode from a ASP.Net web applicaiton,

  1. If impersonation is enabled, the user account used to connect to database server is the use account specificed in impersonate section in web.config.
  2. If impersonation is disabed, the user account used to connect to database server is which specificed in the application pool which the web application is running under.

So here is my solution:

  1. Use a Sql server logon instead of a windows account to access the database. You will have to ask your DBA to create a sql logon for you. This is the easist way.
  2. Use a Windows user account to access the database. To do this,
    1. Ask your administrator to create an account has similar priveleges with Network Service or ASPNET account.
    2. Set that account as anonymous access acount in op web if you need the anonymous access to be enabled.
    3. Ask your DBA to grant database access to that account and [Anonymous Logon] if necessary.
    4. Or, if you dont want to mess up with the IIS configuration, you can simply set the impersonate on in your web.config

2007年7月22日

New Blog

This blog is a branch from my live space : http://michaelspace.spaces.live.com/ , I'll post .Net Framework and Microsoft Server System related articles here.

Basicly these posts is about troubles I experience in our project , and the solutions or walk around I found. And maybe we can discuss those troubles and find a better solution here.

Blog Archive

About Me