DCSIMG
C# - Miscellaneous Debris

Miscellaneous Debris

Thoughts and Snippets

Browse by Tags

All Tags » C# (RSS)
ArcGIS–Getting the Legend Labels out
Working with ESRI’s ArcGIS package, especially the WPF API, can be confusing. There’s the REST API, the SOAP APIs, and the WPF classes themselves, which expose some web service calls and information, but not everything. With all that, it can be hard to find specific features between the different options. Some functionality is handed to you on a silver platter, while some is maddeningly hard to implement. Today, for instance, I was working on adding a Legend control to my map-based WPF application...
The Case of the Unexpected Expected Exception
“NUnit is being problematic again”, they told me when I came to visit the project. “When running unattended it’s not catching assertions properly and the test is coming up green, but when stepping through in the debugger, it works fine.”. It’s nice, when getting a passing test is acknowledged as a bad thing, at least when you don’t expect it to be. In this case, though, the fault wasn’t really with NUnit. 1: [Test] 2: [ExpectedException] 3: public void DoTheTest() 4: { 5: _myComponent.RunMethod(...
Moq, Callbacks and Out parameters: a particularly tricky edge case
In my current project we’re using Moq as our mocking framework for unit testing. It’s a very nice package – very simple, very intuitive for someone who has his wrapped around basic mocking concepts, but tonight I ran into an annoying limitation that, while rare, impeded my tests considerably. Consider the following code: 1: public interface IOut 2: { 3: void DoOut ( out string outval); 4: } 5: 6: [Test] 7: public void TestMethod() 8: { 9: var mock = new Mock<IOut>(); 10: string outVal = "...
שלישיה חזקה
פיני דיין כותב בבלוג שלו על שני classים נידחים שמתחבאים להם ב-System.Web כדי לשמור צמדים ושלשות של אובייקטים: Pair ו- Triplet . אני חושב ששימוש באובייקטים הללו הוא רעיון רע, ועדיף שיקברו יחד עם ה-.NET Framework 1.0 שהוליד אותם. יש לי שתי סיבות: א) המחלקות הללו יושבות ב-System.Web.UI, שיושב תחת System.Web.dll. אם אני לא כותב מערכת Web, זה מיותר לחלוטין להוסיף Reference ל-System.Web רק בשבילהם. וזה גם בלי להזכיר שהם לא היו אמורים לשבת ב-Namespace הזה מלכתחילה – אין בהם שום קשר ל-UI. גם אין בקלאסים...
Upgrading all projects to .NET 3.5
A simple macro to change the Target Framework for a project from .NET 2.0 to .NET 3.5, hacked together in a few minutes. This will fail for C++/CLI projects, and possibly VB.NET projects (haven't checked). Works fine for regular C# projects, as well as web projects:   For Each proj As Project In DTE.Solution.Projects Try proj.Properties.Item( "TargetFramework" ).Value = 196613 Debug.Print( "Upgraded {0} to 3.5" , proj.Name) Catch ex As Exception Debug.Print( "Failed...
System.Diagnostics.EventLogEntry and the non-equal Equals.
Just a quick heads-up in case you're stumped with this problem, or just passing by: The System.Diagnostics.EventLogEntry class implements the Equals method to check if two entries are identical, even if they're not the same instance. However, contrary to best practices, it does NOT overload the operator==, so these two bits of code will behave differently: EventLog myEventLog = new EventLog( "Application" ); EventLogEntry entry1 = myEventLog.Entries[0]; EventLogEntry entry2 = myEventLog...
Displaying live log file contents
I'm writing some benchmarking code, which involves a Console application calling a COM+ hosted process and measuring performance. I want to constantly display results on my active console, but since some of my code is running out-of-process, I can't really write directly to the console from all parts of the system. Not to mention the fact that I want it logged to a file as well. So I cobbled together a quick Log class that does two things - it writes to a shared log file, keeping no locks...
Another minor C#/C++ difference - adding existing files
Another minor detail that bit me for a few minutes today. I'm posting this so I'll see it and remember, and possibly lodge it in other people's consciousness. Using Visual Studio 2005, adding an existing .cs file to a C# project will cause a copy of that file to be created in the project directory. If I want to link to the original file, I need to explicitly choose Add As Link from the dropdown on the Add button. C++ projects, however, have the opposite behavior. Adding an existing item...
Dynamically creating a Sharepoint Web-Service proxy instance
In my current code, I find myself accessing many Sharepoint web services. In one run of code I can access 5-6 different web services on dozens of different sites, not necessarily even on the same server. Given that I find myself writing a lot of boilerplate code looking like this: using (SiteData siteDataWS = new SiteData()) { siteDataWS.Credentials = CredentialsCache.DefaultNetworkCredentials; siteDataWS.Url = currentSite.ToString() + "/_vti_bin/SiteData.asmx"; // Do something with the WS. } Seeing...
Wrong constructor called in COM+: sneaky, sneaky bug
Here's another nasty one that tried to bite me today. Let's say I have the following classes: public class COMPlusClass : ServicedComponent { public COMPlusClass() { // Default initialization. } public COMPlusClass (string data) { // Parameterized initialization. } } public class Client { public Client() { COMPlusClass cpc = new COMPlusClass("I am a client"); } } Which constructor do you think will be called? Naturally, I wouldn't ask if it was the second one. Much to my surprise, the first constructor...
More Tales from the Unmanaged Side - System.String -&gt; char*
Today, I had a very tight deadline to achieve a very simple task: pass a managed .NET string to an API function that expects a null-terminated char*. Trivial, you would expect? Unfortunately it wasn't. My first though was to do the pinning trick that I mentioned in my last post, but in this case I needed my resulting char* to be null-terminated. Second thought was to go to the System.Runtime.InteropServices.Marshal class and see what it had for me. I found two contenders: 1) Marshal::StringToBSTR...
A C# coder in a C++/CLI world, part 1
Recently I've found myself stumbling around some C++/CLI code. C++ is a language which I learned years ago and never really worked with seriously, so I've been cursing and moaning as I worked. Strange for me to go back to a (partially) unmanaged environment now, with all sorts of assumptions that I have proven to be false. I'll try to go over some pitfalls and insights I'm having during the visit. This is the first: The Garbage Collector really spoiled me. I'm not talking about deleting what I instantiated...
Allowing timeout on long-running operations - possible bug
A while ago, I wrote about a simple pattern to allow us to put a timeout limitation on a long running operations. Simply put, it allows us to replace this: public MyObject GetData() { try { MyObject data = BigComponent.LongRunningOperation(); return data; } catch (Exception ex) { // Log and rethrow. throw; } } with this: public MyObject GetData() { try { MyObject data; Thread t = new Thread( delegate() { data = BigComponent.LongRunningOperation(); }); t.Start(); bool success = t.Join(timeoutForOperation...
Some more glaring omissions in the Sharepoint 2007 SDK and Web Services
I downloaded the new Sharepoint 2007 SDK today, published last week though it's dated from April 2007. There appears to be no change history for this version of the SDK. I was hoping for more information on new Web Service methods available in 2007 - hoping that the lack of 2007-specific functionality was more of a documentation error than an actual oversight. It seems this is not the case. I've written about the lack of item-level permissions in the WSSv3 web services before, but I've...
Retrieving all properties for a Sharepoint list
When we call the GetListItems or GetListItemChanges methods in Sharepoint's Lists Web Service, we pass an XmlNode parameter specifying which fields we want to return, in this format: <ViewFields> <FieldRef Name="ows_ID"/> </ViewFields> Now, if we leave this field as null , we get a certain set of properties back - Name, Last Modified and so forth. Basic, commonly used properties. However, if we want to return ALL properties, and we don't want to explicitly state them, we need to...
More Posts Next page »