Per Lundberg

Random thoughts from a BizTalk developer
posts - 10, comments - 11, trackbacks - 0

My Links

News

Archives

Post Categories

Tuesday, July 07, 2009

TFS and branching

TFS (Team Foundation Server) seems to have some very peculiar ideas regarding branching. Previously, I've used the branching feature, and it's worked just fine. But today, it went absolutely crazy with me.

What I did was this: I tried to create a branch of a solution. Seemed to work fine. Now, the problem was that the branched solution still referred to some of the old projects. And, even worse, the non-branched solution referred to the new, branched projects!

(Branching in TFS works like this: a directory is essentially copied on disk, so you get a separate working copy for each branch. Pretty good when it works, but today... a complete disaster)

Just a few minutes ago, I was so angry regarding this. I mean, this is crazy! This is kind of the feature regarding branching, the key issue is that you want to separate the two different branches from each other. But by doing this, VS2008/TFS essentially destroyed this idea.

Grmbl..

posted @ Tuesday, July 07, 2009 12:43 PM | Feedback (2) |

Wednesday, April 29, 2009

Instantiating a Dictionary from a LINQ projection, part 2

(This posting is a continuation of this blog posting.)

If you want to implement this the OOP way, here you have the subclass code (which I'll use myself).

using System.Collections.Generic;

// Remember to set this to your proper namespace (or use a using statement in
// the class with the LINQ projection)
namespace MyNamespace
{
    /// <summary>
    /// Extension methods for the Dictionary generic class.
    /// </summary>
    public class ImprovedDictionary<T1,T2>: Dictionary<T1,T2>
    {
        /// <summary>
        /// Add a key/value pair to the dictionary and return the dictionary reference.
        /// </summary>
        /// <param name="key">the key</param>
        /// <param name="value">the value</param>
        /// <returns>the dictionary</returns>
        public ImprovedDictionary<T1, T2> AddWithReturn(T1 key, T2 value)
        {
            this[key] = value;
            return this;
        }
    }
}

posted @ Wednesday, April 29, 2009 9:43 AM | Feedback (1) |

Instantiating a Dictionary from a LINQ projection

Ever needed to instantiate a Dictionary from a LINQ projection? It is certainly doable, although not extremely straightforward. My first thought was to try and do this:

var result = from o in ctx.Orders
where o.ID = OrderID
select new MyOrderClass
{
ID = o.ID,
Created = o.Created,
ExtraData = new Dictionary<string,string>().
Add("key1", o.Value1).
Add("key2", o.Value2")
};
If this would work, it would probably be the most clean-looking solution. The only problem with this though is that the Add() method in the Dictionary class doesn't return the Dictionary object itself... so unfortunately, this makes it unusable from within a LINQ projection.

So, what do we do? If you've done your C# 3.0 homework, the answer should be fairly obvious: An extension method!

Extensions method, albeit not 100% OOP-pure, are a convenient way to be able to "inject" new methods into an existing class. Of course, this is mostly "syntactic sugar". The methods aren't really inserted into the existing class, but the C# compiler lets you call the methods as if they were a part of the original class.

When writing this blog posting, I realize that in this specific case, we don't actually have to use an extension method. We could just subclass the Dictionary<string,string>, and add our own method to this subclass. If you want to do it the OOP way, this is preferrable. On the other hand, if you want to practice your C# 3.0 skills, keep reading. :-)

Create a new class, with this content:

using System.Collections.Generic;

// Make sure the namespace below is the same for both your extension method class and the class
// where you are writing the LINQ projection, or use a using statement in the latter one.
namespace MyNamespace
{
    /// <summary>
    /// Extension methods for the Dictionary generic class.
    /// </summary>
    public static class DictionaryExtension
    {
        /// <summary>
        /// Add a key/value pair to the dictionary and return the dictionary reference.
        /// </summary>
        /// <param name="dictionary">the dictionary to add the key/value pair to</param>
        /// <param name="key">the key</param>
        /// <param name="value">the value</param>
        /// <returns>the dictionary</returns>
        public static Dictionary<string, string> AddWithReturn(this Dictionary<string, string> dictionary, string key, string value)
        {
            dictionary[key] = value;
            return dictionary;
        }
    }
}

That's it. Now, change the above LINQ query to this:

var result = from o in ctx.Orders
where o.ID = OrderID
select new MyOrderClass
{
ID = o.ID,
Created = o.Created,
ExtraData = new Dictionary<string,string>().
AddWithReturn("key1", o.Value1).
AddWithReturn("key2", o.Value2")
};

Convenient, huh? So, when should we use the extension method approach, and when should we adhere to the OOP standard way of doing it? Well, I'd say like this: If we need to do this on different types of dictionaries (with different key/value types), the extension method approach might be better. I'm not sure if you can subclass a generic class (and have the subclass be generic as well). Maybe you can, and if so, this might be the most elegant way to do it.

If you want to do it OOP (which you should strive for, if possible), only use the approach above when you have to (i.e. when subclassing isn't possible, because the parent class is sealed or similar).

posted @ Wednesday, April 29, 2009 9:37 AM | Feedback (2) |

Tuesday, March 24, 2009

ASP.NET 3.5/AJAX.NET-enabled datepicker snippet using the ASP:Calendar control

Looking for a simple datepicker that uses AJAX.NET (to avoid full-page postbacks), for your date fields? Look no further! I was on the same "hunt", and ended up writing my own code for it (inspired by other examples/code I was looking at).

The code does all the work using ASP:NET, nothing is done using JavaScript. Below is the code for the solution. This could (and should) be made into a UserControl if you need to use in more than one place

In the .aspx file, put this: (As you see, I am referencing a calendar icon here - use your own icon for this)

                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:TextBox ID="requestedDeliveryDateTextBox" runat="server" Width="100" />
                        <asp:ImageButton id="imageButton" runat="server" ImageUrl="~/Images/IconCalendar.png" AlternateText="calendar"
                            OnClick="ImageButton_Click" CausesValidation="false" />
                        <br />
                        <div id="calendar" class="calendar" visible="false" runat="server">
                            <asp:Calendar ID="requestedDeliveryDateCalendar" runat="server" OnSelectionChanged="RequestedDeliveryDateCalendar_SelectionChanged" />
                        </div>
                    </ContentTemplate>
                </asp:UpdatePanel>

In the code-behind (aspx.cs file):

        /// <summary>
        /// The calendar icon was clicked.
        /// </summary>
        protected void ImageButton_Click(object sender, EventArgs eventArgs)
        {
            // Toggle visibility of the calendar.
            calendar.Visible = !calendar.Visible;
        }

        /// <summary>
        /// The selected date in the calendar was changed. This method is called via AJAX.
        /// </summary>
        protected void RequestedDeliveryDateCalendar_SelectionChanged(object sender, EventArgs eventArgs)
        {
            requestedDeliveryDateTextBox.Text = BOMisc.GetDatePart(requestedDeliveryDateCalendar.SelectedDate);

            // We hide the calendar here. You can disable this hiding by removing the following line.
            calendar.Visible = false;


            // Move the focus to the textbox below the calendar, to make things convenient for the user filling in the form.
            // Remove this line or change it to reference your own control instead.

            someTextBox.Focus();
        }

And finally, the .css portion. We don't do very much styling of the actual calendar here, we just place it on top of the rest (the z-index part), with a white background to make sure the stuff below doesn't "shine through". We also make the calendar be absolutely positioned so that it becomes a true "dropdown" calendar (it will appear "on top" of everything else on the page).

.calendar
{
    position: absolute;
    width: 250px;
    padding: 5px;
    background-color: White;
    border: 1px solid #858585;
    z-index: 10;
}

That's it for now, hope this helps. Happy datepicking! :-

posted @ Tuesday, March 24, 2009 8:50 AM | Feedback (3) |

Thursday, March 19, 2009

Trouble getting TargetInvocationException on your ASP.NET web application?`

Sometimes, you get pretty unclear exception messages. The message below is one example of such:

    Exception type: TargetInvocationException
    Exception message: Exception has been thrown by the target of an invocation.

    [...some stuff removed...]

    Stack trace:    at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
   at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.ListView.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
   at System.Web.UI.WebControls.ListView.CreateChildControls()
   at System.Web.UI.Control.EnsureChildControls()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
 
I mean, this doesn't tell you anything about where in your code the problem occured. Or does it?

Well, actually, it doesn't. But by experience, you can learn that when working on stuff in an ASP.NET context, this usually (in my experience) means that the problem was caused by an ObjectDataSource. It might happen with other controls as well, but this is one case where I have seen it.

The ObjectDataSource "invokes" the method on the class you are telling it to get the data from (or perform the update, etc). The problem with this is that the "real" exception is not shown. My guess is that it would be in the "inner" exception in IIS, and this is not shown here, in the event viewer.

Anyway, when debugging the project inside Visual Studio, I still didn't get any exception information (maybe since I have a custom error handler page set up, that could be removed but it turned out not to be necessary). But, what I did get was this:

'WebDev.WebServer.EXE' (Managed): Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ff657321\e4c5c948\App_Web_vtrilm_y.dll', Symbols loaded.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Diagnostics.ServiceModelSink\3.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Diagnostics.ServiceModelSink.dll'
A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpUnhandledException' occurred in System.Web.dll
A first chance exception of type 'System.Web.HttpUnhandledException' occurred in System.Web.dll
The thread 0x2bb4 has exited with code 0 (0x0).

The red line at least gives you the name of the exception, which in this case was enough to lead me to the real cause of the problem.

Anyway, that's all for now!

posted @ Thursday, March 19, 2009 1:26 PM | Feedback (2) |

Thursday, March 12, 2009

Scheduled tasks - task stopped running when I renamed it

Well, the title says it all... I was doing some work on one of our customer's servers, when I found a task in the Scheduled Tasks function in Windows that had the wrong name.

Alright, I renamed it. The next time it was supposed to be run, Windows just ignored it! Very weird indeed.

The solution? As always, the "toggle" trick. :-) We do it with Visual Studio (with the Copy Local setting for referenced assemblies), and, apparently, it can be done with Scheduled Tasks as well. Open the task, uncheck the "enabled" box, Save, open it again, and enable it back again...

Let's hope this helps someone of you guys out there!

posted @ Thursday, March 12, 2009 10:54 PM | Feedback (0) |

Wednesday, March 11, 2009

Be careful when setting GenerateCSFiles to 1

If you, like me, have set the GenerateCSFiles property to 1 (as described here) to get BizTalk to keep the .cs files after they have been generated from the orchestrations and maps, be a bit aware that it can cause some weird problems sometimes.

I just had one of my orchestrations indicating that there was an error with some of the shapes. The map was missing, seemingly. The odd thing was that the whole solution was compiling correctly, so the problem seemed only to affect the editor.

When looking at the "configure map" settings, the map the shape was referring to had just disappeared. Yeah, you heard me. It was completely gone. Why? I don't know. The file was located in the project, it was set to Compile on build, but... it didn't work.

The solution: close down Visual Studio. Remove all .cs files from the folder with the orchestration and the folder with the maps. Open the solution, and the problem was gone...

posted @ Wednesday, March 11, 2009 12:19 PM | Feedback (0) |

Monday, December 15, 2008

Script calling external assemblies - be aware that it uses "dynamic binding"

I've been having an error lately that looks like this in the event log:

Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'SomeRoutines.OrderHandler(1f254f9a-5838-1051-30a3-ca1c40b2906f)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: 875947d5-5c01-4ad7-9d62-a033902a3133
Shape name: ConstructUpdateMessage
ShapeId: 43a0909e-5877-4e6e-a6a0-c4bbb7c5c644
Exception thrown from: segment 1, progress 12
Inner exception: Error encountered while executing the transform SomeRoutines.SomeTransformation. Error:Unable to create the transform..
      
Exception type: XTransformationFailureException
Source: Microsoft.XLANGs.Engine
Target Site: Void ApplyTransform(System.Type, System.Object[], System.Object[])
The following is a stack trace that identifies the location where the exception occured
   at Microsoft.XLANGs.Core.Service.ApplyTransform(Type mapRef, Object[] outParams, Object[] inParams)
   at SomeRoutines.OrderHandler.segment1(StopConditions stopOn)
   at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)
Additional error information:

        Could not load file or assembly 'Xxxxxxxxxxx.XxxXxxxxx.Integration.HelperFunctions, Version=1.0.0.1, Culture=neutral, PublicKeyToken=aaaaabbbbbccccdddd'
or one of its dependencies. The system cannot find the file specified.
      
Exception type: FileNotFoundException
Source: mscorlib
Target Site: System.Reflection.Assembly nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.Assembly,
System.Threading.StackCrawlMark ByRef, Boolean, Boolean)
The following is a stack trace that identifies the location where the exception occured
   at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at Microsoft.XLANGs.BaseTypes.TransformBase.get_TransformArgs()
   at Microsoft.XLANGs.RuntimeTypes.TransformMetaData..ctor(Type transformBaseType)
   at Microsoft.XLANGs.RuntimeTypes.TransformMetaData._creator(Type t)
   at Microsoft.XLANGs.RuntimeTypes.MetadataCache._slowFor(Type t)
   at Microsoft.XLANGs.RuntimeTypes.MetadataCache.For(Type t)
   at Microsoft.XLANGs.RuntimeTypes.TransformMetaData.For(Type t)
   at Microsoft.XLANGs.Core.Service.ApplyTransform(Type mapRef, Object[] outParams, Object[] inParams)

The problem with this was that the .dll version number had been increased from 1.0.0.1 to 2.0.0.0, causing the above error. I had recompiled the project in question and deployed it again, but I still got this error and today I decided to take a look at its cause. It turned out that what I have in the .btm file (BizTalk map) is a scripting function, calling an external assembly. Now, when you do that, this is what it looks like in the .btm file:

<ScripterCode>
<Script Language="ExternalAssembly" Assembly="Xxxxxxxxxxxx.Xxxxxxx.Integration.HelperFunctions, Version=1.0.0.1, Culture=neutral, PublicKeyToken=aaaabbbbbbcccccdddd"
Class="Xxxxxxxx.Xxxxxxxxxxx.Integration.HelperFunctions.WrapperClass" Function="GetDiscount"
AssemblyPath="..\IntegrationFunctoids\obj\Debug\Xxxxxxxxxxxx.Xxxxxxxxx.Integration.HelperFunctions.dll" />
</ScripterCode>

What this means in reality is that the binding is not done using the project's "assembly references"; rather, the .dll is loaded at runtime. Sure, that's a pretty convenient thing at times. Right now in this case though, it would have been better to have the .dll files be more "statically" bound to each other.

The problem with dynamic binding (or "dynamic references" might be a better phrase) is that you don't get the errors when you want them: at compile time. Rather, you get it at run time. However: what you do get is that you get the error when you try the "Test Map" function in Visual Studio. Once again, clearly, this nice function can not be over-emphasized. :-)

posted @ Monday, December 15, 2008 1:48 PM | Feedback (0) |

Friday, November 28, 2008

Remember to make callable orchestrations "public"

If my previous posting was kind of sophisticated, this one is a lot more down-to-earth and basic...

I've been struggling during the morning trying to use the "Call Orchestration" feature of BizTalk. A pretty straightforward feature. But, I couldn't just get it to work. If the called orchestration was in the same project as the calling orchestration, it worked Just Fine<tm>, but as soon as I tried to put the called orchestration in its proper place, it just failed.

The solution? Well, I got the answer from someone who is much more experienced with BizTalk than me: Make the called orchestration public. This is pretty straightforward if you think about: orchestrations are really standard CLR classes (even though not generated manually by writing C# or VB.NET code), and as such, they have visibility parameters just like classes usually have.

If it doesn't immediately work after making sure the callee orchestration is public, try rebuilding the project that is referring to the callee. Yes, you heard me right. Even thought it might fail (since you are likely to have a Call Orchestration shape that is unconfigured), something happens when you build it that sometimes makes it work.

Restarting Visual Studio is also something that can be helpful.  For me, in this specific case, building the project didn't work straight away - one of the files was being locked. But when I restarted Visual Studio, it found the callee orchestration anyway, so - problem solved.

posted @ Friday, November 28, 2008 10:09 AM | Feedback (0) |

Weird problems with document with only one node, the root node (no content nodes)

I ran into a strange issue the other day with BizTalk Server 2006 R2. Imagine a map (.btm file) that takes multiple messages as its input. Imagine two of those messages being of the same document type, having the same schema. Now... make the first message be normal, like this (this is an excerpt from the full multipart input message to the map):

  <InputMessagePart_2>
    <ns4:stock_warehouse_fetch_response xmlns:ns4="http://StockWarehouse">
      <ns4:stock_warehouse stock_warehouse_id="170A9532-BD20-11DD-85B6-3DFC55D89593"
company_id="1EBB0C44-BD20-11DD-9025-69FC55D89593" 
[... More attributes existed but have been removed to preserve
the integrity of the customer ]
/>
    </ns4:stock_warehouse_fetch_response>
  </InputMessagePart_2>
  <InputMessagePart_3>
    <ns4:stock_warehouse_fetch_response xmlns:ns4="http://StockWarehouse">
      <!-- This is the part that causes the problems -->
    </ns4:stock_warehouse_fetch_response>
  </InputMessagePart_3>


When I used the message above (with more messages in the multipart message and so forth), a very weird thing occured: the map output was completely empty, the only thing that remained was the root node! Obviously, many of the input elements existed in the input messages, so this is clearly a bug in BizTalk (IMO). The problem first occured when running the application in BizTalk Server 2006 R2, but was reproduced with the Visual Studio-integrated "test map" function.

The solution? Add a "content node" in the document, like below. It seems like BizTalk really, really dislikes documents with only a root node. There must be at least one content node in all the input documents to a map. Otherwise, the map output is completely eradicated, except for the root node.

  <InputMessagePart_3>
    <ns4:stock_warehouse_fetch_response xmlns:ns4="http://StockWarehouse">
      <ns4:stock_warehouse></ns4:stock_warehouse>
    </ns4:stock_warehouse_fetch_response>
  </InputMessagePart_3> 

Well, that's it for this time. I hope this helped you, it would have helped me.

posted @ Friday, November 28, 2008 9:47 AM | Feedback (1) |

Powered by: