Tuesday, December 23, 2014

WCF: Identifing late exceptions in the pipeline

Hi.
Recently I’ve run into  a strange WCF service issue. The logs in the service method were indicating a successful completion of the call, but the call was still failing and the clients were getting exceptions.
 The exception message was: “An error occurred while receiving the HTTP response to <serviceEndpoint>. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).“.
The service API which was called had a return type (not void), and the type was serializable. So the only explanation I could come up with was that something wrong goes with serialization. THAT WAS IT!
Luckily it’s possible to simulate the same serialization what WCF does behind the scene, and verify the assumptions. So I did it: created a test method within the unit-test project, called the API being called by the client to get properly built result object, and then tried to serialize it:
[TestMethod]
public void GetDataUsingDataContract_SerializesResults()
{  Service1 serviceInstance = new Service1();
  CompositeType argument = new CompositeType();
  CompositeType result = serviceInstance.GetDataUsingDataContract(argument);

  using (MemoryStream stream = new MemoryStream())
  {
 
   DataContractSerializer serializer = new DataContractSerializer(typeof(CompositeType));
    serializer.WriteObject(stream, result);
  }
}
Note: you’ll need to add a reference to System.Runtime.Serialization.dll.
In my case the “serializer.WriteObject(stream, result);” call threw with the detailed exception.
 
You can download the full sample project: http://1drv.ms/1sJljCd
 
Hope this helps.

Saturday, September 6, 2014

Windows Phone 8: The Map control

Recently I've been hitting an issue with integrating the Map control from the Windows Phone Toolkit into the UI. The problem was related to centering map to the desired location. I was using MVVM pattern for the app, so I had a proper public property in the data context, which I was trying to set as a binding source for the Center property of the Map control.

As I was just interested in centering map based on the property value, and not vice-versa, I've had explicitly specified the Mode property for the binding to be "One Way". And that was the problem.
It turns out, that for the Center property to work properly, you have to have the binding mode set to "Two Way".

So the final property and the XAML should be similar to the following to work:

The following property is the one defined in the data context:
public GeoCoordinate MapCenterLocation
{
 
get
  {
   
return this.mapCenterLocation;
  }
 
set
  {
 
   if (value != this.mapCenterLocation)
    {
 
     this.NotifyPropertyChanging(MapCenterLocationPropertyName);
      this.mapCenterLocation = value;
      this.NotifyPropertyChanged(MapCenterLocationPropertyName);
    }
  }
}


and the appropriate XAML for the map control should be:
<maps:Map x:Name="map"
  HorizontalAlignment="Stretch"
  VerticalAlignment="Stretch"
   Center="{Binding Path=MapCenterLocation, Mode=TwoWay}" />

As mentioned above - the "Mode=TwoWay" is critical for the Center property binding to work. Otherwise, the map will never be centered like the binding is not there.
Interestingly, you don't even get an error/warning for that.

Thursday, April 10, 2014

Dynamic Reflection in C#

Hi,

More and more I started to feel the need for dynamic reflection - reflection working not based on subject names (properties, methods, fields ,...) but delegates.
So got time to submit and idea on Microsoft Connect about it. The following sample shows what the idea is about:
internal class Sample
{
 public int PropA { get; set; }
 public void SomeMethod()
 {
  PropertyInfo info = GetPropertyInfo(() => this.PropA);
  // Some other code, using the "info" variable
 }

 public static PropertyInfo GetPropertyInfo(Func<object> property)
 {
  // This is the method I'm asking about.
 }
}
If you like it, please go and vote for it at: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5755035-ability-to-get-propertyinfo-dynamically-passing-th


Update:
It turns out what I've asked in this post is possible using Expression trees. The following is the working example on how to achieve it:
    public static class PropertyExtensions
    {
        public static PropertyInfo GetPropertyInfo<TSource, TProperty>(this TSource instance, Expression<Func<TSource, TProperty>> propExtractor)
        {
            MemberExpression mexp = propExtractor.Body as MemberExpression;
            if (mexp == null)
            {
                throw new ArgumentException("Invalid");
            }
            PropertyInfo result = mexp.Member as PropertyInfo;
            if (result == null)
            {
                throw new ArgumentException("Invalid expression");
            }
            return result;
        }
    } 

Tuesday, March 11, 2014

Breaking the debugger session like an exception was thrown

Today I cam across a situation, which made me think about an new feature in Visual Studio Debugger.
But first about the scenario: I've been debugging a component test, and wanted to stop the debugger. But the same time - I couldn't do that, as that won't take the execution through the normal cleanup flow, defined in a finally block.
Then I thought about the feature I've started with, and here is the link to it: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5624050-stopping-debug-session-to-go-through-the-defined

So please read it and vote for it !

Thursday, February 20, 2014

How I've fixed the periodic crashing IE problem

Hi there,

Today I've finally managed to fix the crashing IE problem, under Windows 8.1. So decided to share my experience, hoping for some of you out there - this post will be in help.
But first, let me give you a bit of history.

Frankly saying, I'm a Microsoft fan. Yes, I do work in Microsoft as a software developer.
So you guessed already, that I do use Windows - I love it ! I have had a chance to play with other OS-s as well, so please - no comments about "you haven't seen others".
So, my machine was running Windows 7 for a while already. And one day - Microsoft announces the availability of Windows 8 Preview. Of course - I do install it (on a VM for now). I guess it took some time for me to get used to it, and eventually, I liked it as well. Then - the release, and, as expected, I'm upgrading my main OS to it immediately.
Let me tell you the truth - that was the best OS upgrade experience I've ever went through. No issues, no hassles - and everything works: everything except the most used product - the Internet Explorer, which keeps crashing periodically - about 50 times a day.
Well, I now realize, that my laziness was keeping me away from trying to look into the actual problem, and fix it. So I was just tolerating that, and using it again and again. By the way, the non-desktop version of IE was ok, no crashes, no issues. And I started using that one more and more.
And finally, the final crash made me so angry, that I've forgot about my laziness, and decided to dig deeper.
First thing was to open the Windows Event Log (Windows Logs \ Application), and look for most recent errors: luckily the only errors were from IE (those I've been hitting). And the error message was very similar to:

Faulting application name: IEXPLORE.EXE, version: 11.0.9600.16518, time stamp: 0x52f347b2
Faulting module name: <module name>, version: <module version>, time stamp: 0x513d9755
Exception code: 0xc0000005
Fault offset: 0x00046ce7
Faulting process id: 0x4280
Faulting application start time: 0x01cf2df55b897962
Faulting application path: C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
Faulting module path: <the path to the failing module>
Report Id: 9aa55680-99e8-11e3-bf29-c48508058295
Faulting package full name:
Faulting package-relative application ID:


Looking to the "Faulting module path" I guess will give you a clear picture - of what is really failing. In my case that was a file download application. I've then just uninstalled that application, and it's already 4 hours (I would have about 15 crashes already) - no issues.
Hopefully you'll be able to handle this now.

Sorry for non development related post.

Regards,
Artak