Posts Tagged ‘ C# ’

Requesting resource xyz failed because it is complex

Trying to set the background image (drawable) of a View programatically I ran into this ‘complex‘ error.

Weird…it’s just an id!

First, what worked…then a few things I tried that didn’t work…

What Worked

TypedArray array = view.Context.ObtainStyledAttributes(Resource.Styleable.AppTheme);
var image = array.GetDrawable(Resource.Styleable.AppTheme_ic_action_dice1);

Some explanation…

  1. view is a View type variable.
  2. AppTheme is the name of the theme defined in the attrs.xml file.
  3. The important thing to note here is the use of Resource.Stylable.
  4. I can’t directly reference the Drawable image because there are about 5 versions of the same image across the drawable-hdpi, drawable-ldpi etc folders.¬†There is ONE entry in the attrs.xml file for each of the images (i.e. not one for each size), they are part of the theme.

So it seems to access the ‘correct’ resourceId of the Drawable, you have to first load up the theme ‘TypedArray’ and do a GetDrawable against that.

Makes sense now I finally figured out what the hell is going on but I couldn’t find any blogs to guide in the right direction, it was all trial and error…thus this post ūüôā

What Caused the ‘Complex’ error?

I also tried the following which were dead ends…

Logger.Debug(“Fails because too complex…”);

Logger.Debug(“Fails because not found…”);
// Exception of type ‘Android.Content.Res.Resources+NotFoundException’ was thrown.

One – I thought the properties in Resource.Attribute would be what I needed. There was only one entry per image, that is essentially how I was setting my default (using background=”?attr/ic_action_dice1″ ) in the .axml layout file. But no, using that resource causes the complex error. Don’t know why…curious to know but no time to investigate…

Two – I thought that since Resource.Styleable.AppTheme_someImage was a valid resource to use, that I could make a call to SetBackgroundResource and directly use that resourceId. Can’t find it. Even though as you can see the resourceId is exactly the same as what works.

So it seems you DO need to create the Drawable first…then call SetBackgroundDrawable.

Not sure if that is a Xamarin limitation…seems to be, otherwise why does SetBackgroundResource exist?

Or maybe I’m doing something wrong still? If you are in the know here…please share ūüôā




Learning PostSharp

AOP from the Trenches

Aspect Oriented Programming feels alot like applying style sheets to a web site. Changing your ‘aspects’ will change the way your program works without having to make any changes to your code.

A few months ago I saw a great demo on PostSharp by Omar Besiso and I remember thinking, must ‘check that out someday‘.

Well amazingly, someday just rolled around and I started looking at PostSharp more closely and see if it could help me. My project needs logging and exception handling to be added so we are using the enterprise library blocks and I was keen to see how PostSharp might make my life easier.

The documentation on PostSharp is good but it’s a bit thin on best practice examples and little ‘gotchas’ so this blog entry is just a little collection things I am learning along the way…note that I am completely new to Aspect Oriented programming so please correct me if I say somethings really stupid or ignorant ūüôā

There are also a couple of good articles on CodeProject to help you get started.

1. Restart Visual Studio

So I installed PostSharp, followed the quickstart sample exactly, compiled…didn’t seem to do anything. Ok so open up Reflector, did it actual make any changes? Nope.

After scrounging around the documentation for a while and trying various things to figure out what I was doing wrong I finally went back to the website ‘getting started’ section and in step two it clearly says ‘restart visual studio’.

Of course! Did that…all works.

And how good is this….I tweeted a hotip to restart VS after installing PS and within an hour I got message from Gael Fraiteur (the author) saying he’d raised a bug to warn users to restart VS after installing!

2. Don’t Try/Catch

These next few points all relate to handling exceptions with Aspects.

Let’s say you add this attribute to a method (and you’ve set them up properly):

public void MyMethod()
// exception happens here
catch(Exception ex) {// do something with the error

If you then do a try/catch like I have in the method then that try/catch takes over and the Aspect no longer ‘handles’ the exception. Which is good I think, you want to be able to ‘override’ what the aspect does in some instances. It feels a bit strange though, NOT wrapping a potentially naughty bit of code in a try/catch block, you have to have faith that the aspect will do its job.

NOTE: I’m not saying never use a try/catch block anymore, you will often still want to catch and specifically handle some exceptions.

3. Where Does FlowBehavior.Continue From?

I used eventArgs.FlowBehavior = FlowBehavior.Continue; and I expected the code execution to continue from (next line) where the exception occured. It didn’t, it returned to the line of code that called the method where the exception occurred. Consider the following:-


When the exception occurs in the Third method, my aspect handles it by doing whatever and then sets eventArgs.FlowBehaviour = FlowBehaviour.Continue; The result of this is execution continues in the Second method…and the Fourth method is not called.

Cool, not quite what I expected but it’s fine, the main thing is understanding how it works.

So what happens if we move that attribute?


Well, here exception occurs in Third, but the aspect handles it in Second and so execution continues in First. Nice, just be aware of where you are putting the attribute.

We could also do this…


Here, the exception occurs in Third and the aspect handles in Third also, so execution resumes in Two.

Note you can also put the attribute on the class, which saves you from putting it on individual methods. Seems to me that the best place to wire up the exception handlers are in the AssemblyInfo.cs files. This is what you need to add:-

[assembly:¬† MyExceptionHandlerAspect(“MyExceptionPolicy”)]

which to me is also much less confusing than attributes here and there randomly scattered through your code. Plus one of the big advantages of using PostSharp is that there is almost zero change required to existing code. So my ‘guess’ as to best practice here is to only apply an Exception Handling Aspect Attribute to individual methods when there is a very specific need to handle raised exceptions differently (i.e. use a different aspect or perhaps the same aspect with different parameters supplied – eg. in the case above I might want to use a different exception policy).

4. Continue After Exception – ‘Problem’

Just to be clear I don’t see this as a problem with PostSharp, it’s just a stumbling block I ran into.

I’m using the enterprise library, specifically for this problem I’m using the Exception Handling block. I have a policy to ‘handle’ System.Exception’s by logging them and then rethrowing an new exception that just wraps the original one.¬† Since all exceptions derive from System.Exception this handler will catch all exceptions except those that have their own explicitly defined handler (including the null reference exception below).

To the relevant AssemblyInfo.cs f iles I have added [assembly:¬† MyExceptionHandlerAspect(“MyExceptionPolicy”)].

Now I am also using CAB and discovered this ‘continue problem’ in a command handler.


I’ve added a dodgy bit of code that forces a null reference exception.

So, my PostSharp aspect looks like this


and it nicely does intercept my null reference exception but what happens when the call to HandleException is made? The enterprise library takes over and does it’s thing, logs and wraps the exception in a new exception which is returned in exceptionToThrow….great, so far so good.

The next thing that is going to happen is exceptionToThrow…will be thrown. But let’s see what the call stack looks like BEFORE it gets rethrown….


At the top of the stack is the breakpoint I set in the OnException method in the OnExceptionAspect class. No problem, next is the OnDoSomething method where the exception occured. The next three are all CAB related (including the Infragistics line), then before that is essentially the call to Start().

So what happens is my policy causes the rethrow of the new exception, which gets thrown ‘back’ to OnExecuteAction in CAB code…which (currently) doesn’t have any PostSharp goodness to automatically wire up our exception handling policies. This means the exception will keep getting bubbled up the stack until it hits either a try/catch (that doesn’t rethrow) or an assembly with an exception aspect defined (PostSharp goodness). Let’s see what the call stack looks like after the new exception gets thrown.


So it’s now Continuing execution from the next point that ‘handles’ the thrown exception. Problem is, that’s in the Start method and once execution continues beyond the call to base.Start then the application shutsdown (just the way a CAB app works).


Ok so I’ve gone away and thought about this for a bit and my solution is to change my policies. I now have a UI Layer Policy which never rethrows, it just logs and reports a message to the user. So all of my Modules and the main Shell use this Policy, there are other policies for other layers where required and an all purpose one that still does the log, wrap and rethrow…which eventually gets handled by some UI layer handler.

5. Lose Edit and Continue

I guess, due to the nature of how PS works (‘injecting code’, creating delegates etc Post compilation), you’ll see this kind of message popping up alot…


when you try to edit code whilst debugging. So no more edit and continue if you add any Aspect Attributes to your class. What I’ve discovered so far is that if you have any methods, properties etc¬† in a class marked with an aspect attribute then you won’t be able to edit and continue anywhere in the class. If the class is ‘clean’ of aspects then you can edit and continue.

This is a heavy price to pay in my eyes, fine if your app is small and fast to start but my last app took 3 mins to build and compile so having to restart much more frequently when debugging would be very frustrating. Fortunately this problem is very easy to almost completely eliminate.

This straight from the documentation:

Disabling PostSharp

It can happen that you have an assembly referencing PostSharp.Public.dll, but that does not need to be processed by PostSharp. This is the case, for instance, when your assembly contains only aspects. Aspects don’t need to (and can not) be themselves transformed.

The easiest way to disable PostSharp in a project is to define the compilation symbol (aka constant) SkipPostSharp.

Therefore, it is possible to have a project with many build configurations and enable PostSharp in only some of these.

So what I’ve done is created a DebugWithPostSharp build configuration, which is a normal debug build. Then on the Debug configuration I added the SkipPostSharp symbol to the relevant projects. The Release configuration always gets PostSharp (really important because unit tests are run against the Release build).


There are so many useful ways you could use PostSharp, just make sure you take the time to thoroughly understand how it does it’s thing….experiment alot! This is just a few things I have learnt along the way, if I find anymore (and some spare time…) I’ll do a part 2.

SCSF ‘Add View’ Recipe Problem

Smart Client Software Factory

We are using the SCSF on my current project and I have to say I like the idea but I hate the number of hoops you have to jump through in order to get it to work properly. One problem I recently experienced was with the Add View (with presenter)… recipe.

Add View (with presenter)…

The problem was it was not appearing in the context menu. So I looked in the Guidance Package Manager to see if I could glean any details about what I was doing wrong.

Guidance package manager

Guidance package manager

So as you can see the recipe applies to ‘any project referencing CAB libraries and the Infrastructure.Interface library.

SCSF has a solution template for creating SmartClient applications, which I had used and so I did have an Infrastructure.Interface project.

The specific ‘CAB libraries’ that the recipe cares about are:

  • Microsoft.Practices.CompositeUI.dll
  • Microsoft.Practices.ObjectBuilder.dll

So…to my project to check my references…

Project References

Project References

…hmmm, looks like all the references are good? Looks like this is one of those things that ‘should work but doesn’t’.

Debug Time

So, to get to the bottom of this, I had my solution open (VS1) and opened another instance of Visual Studio with the GuidancePackageManager.sln open (VS2). I then did an ‘attach to process’, attaching the process of the other visual studio.



Then find the right place in the GPM solution…here

breakpoint location

breakpoint location

then right click on a project in VS1, which causes the breakpoint to be hit in VS2.

The Cause of The Problem

Turns out that its happy with the CAB references but has a problem with the Infrastructure.Interface.


Because when the SCSF created the project it (quite correctly I think) created the Infrastructure.Interface project, setting the default namespace to ‘MySolution.Infrastructure.Interfaceand the assembly name to ‘MySolution.Infrastructure.Interface‘. But this means when it is comparing project references (in the ContainsReference method) if fails because it is looking for a reference to ‘Infrastructure.Interface‘ but finds ‘MyProject.Infrastructure.Interface‘.

To fix it we just went to Project Properties and in ‘Application’ set the Assembly Name back to Infrastructure.Interface. (we don’t expect to have any naming conflicts given the nature of the app).

Silverlight Communication Exception

Cross Domain Access

I hope this article can save someone some of the pain I have just experienced trying to get my Silverlight 2 app talking to my ‘cross domain’ WCF service (that is hosted in a console app).

I’ll leave discussing why I didn’t just host my SL app with my WCF service and avoid all of hassles until the end of the article. I’m also not going to discuss how to set this up, I’ve provided links to articles that I found useful for this.

The Error

An error occurred while trying to make a request to URI ‘http://localhost:4522/MyService’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. Please see the inner exception for more details.

Existing Blog Articles

I found many useful articles, many of them with at least one little extra thing to try, these are the most useful:-

  1. Silverlight forum discussion on the cross domain policy
  2. Great step by step article to showing how to setup an SL project to consume a WCF service hosted in a Console (I thought this for sure would have all my answers)
  3. MSDN has what looks like the content needed for both the crossdomain.xml and clientaccesspolicy.xml (but it’s not quite right).
  4. Interesting discussion on crossdomain.xml and clientaccesspolicy.xml.
  5. StackOverflow thread.
  6. Cross-domain Policy File Specification. This is what eventually led to me trying something that finally it all working!

All of this (and a number of others I looked at) helped, they really are great articles and I could see comments at the bottom saying things like “thanks got it working now!”. Problem was I had very closely checked and rechecked what I had done vs what was in these blogs and still I kept getting the error!

The Solution

After many hours of trying many different things I came across the¬† and tried an extra little line and voila…working service! That’s a great feeling isn’t it…when you’ve been trying to find a solution to a problem for hours…and hours…you know there must be an answer but everything you try should work but dammit it doesn’t…and then…suddenly…you tick some obscure buried box somewhere and the universe is back to the way it should be…

anyway I digress…this is what MSDN says you should put in your crossdomain.xml file

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "">
  <allow-http-request-headers-from domain="*" headers="*"/>

This is what actually works!

<?xml version=”1.0″?>
<!DOCTYPE cross-domain-policy SYSTEM “”&gt;
<allow-access-from domain=”*” />
<allow-http-request-headers-from domain=”*” headers=”SOAPAction” />

I’ve highlight the important line. You’ll notice that the MSDN sample has headers=”*”,¬† this simply does not work. In fact if I add the extra line that makes it work and headers=”*” is still in the first line, then everything still fails. So delete headers=”*” and add the new line.

Another thing to note, I noticed amongst the articles posted on this subject that if you have a clientaccesspolicy.xml file then you don’t need a crossdomain.xml file. Maybe this is true with their setup but I have found that you must have a crossdomain.xml file, in fact after a bit more trial and error I have come to the conclusion that the clientaccesspolicy.xml just does NOT WORK! At least for me. If my SL app finds it then I get the error…no matter what I put in there. Only when my service does NOT return it but DOES return a crossdomain.xml file with the correct format above does it work.

Maybe this was all a peculiarity of hosting the WCF service inside a console app? Not sure. Don’t care at this point, the thing finally works…yay!

Do I Need a File

You can choose to create the required xml in code or you can have the physical file, your choice. Most of the examples just create it in code. It’s not the existence of the file that matters, just the contents and the web GET request must be able to retrieve the content.

By the way, all examples I have seen (including mine) leave your service completely unsecured. I strongly recommend learning how to set these policy files up properly so that your service isn’t nakedly exposed to the outside world.

What Didn’t Help

In case you have some other problem, these are the things I can say didn’t make a difference. Once I got it working I tried with and without these things and my cross domain connection still worked.

1. Does setting the ContentType help?

WebOperationContext.Current.OutgoingResponse.ContentType = “application/xml”;

Nope doesn’t matter.

2. Some blog samples show a return type of ‘Message’ and others a return type of ‘Stream’ for the method that this attribute goes on:-

[WebGet(UriTemplate = “ClientAccessPolicy.xml”, BodyStyle = WebMessageBodyStyle.Bare)]

Does the return type matter? Nope doesn’t matter (well I only tried these two types, I guess it should be one of these)

3. I read you should decorate your services with this attribute…is it needed?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

Nope, didn’t help. (note that it didn’t help with THIS problem, I’m sure there is some good reason for why you would use it)

So Why Bother With Cross Domain?

  1. In an enterprise environment it is very likely that you will need to call a number of different services. Sure you could have one WCF service that is just for your SL app that then goes off and talks to all of the disparate services for the SL app but that just doesn’t seem right to me. (just an opinion and hey in some cases it probably is appropriate…)
  2. Isn’t it all about separation of concerns? When I’m working on server side code I only want server side projects in my solution and vice versa for client solutions. This keeps the number of projects in my solution down…thereby keeping Visual Studio (and resharper) at least a little bit responsive.
  3. When the server is working I just want it running. I don’t want to have to compile and start the server every time I want to debug something in my client code.

Hmmm, I’m sure there’s other reasons but I can’t think now given it’s 3am ūüė¶

Anyway I hope this helps someone save some time ūüôā

WCF Concurrency Mode Tip

Don’t Keep the Default Setting!

When you mark your WCF endpoints with the ServiceBehaviour attribute, are you setting the ConcurrencyMode to Multiple?

The default is Single, meaning only a single thread will invoke the instance method on the reciever object. This may be desirable in certain situations however if you want your service to be scalable then Multiple would be the recommended choice.

Checkout this great article that offers a great explanation of the differences…this is an excerpt from the ‘thoughts’ at the end of the article

ConcurrencyMode should be set to Multiple. In my view, if a receiving application is going to scale, it must set concurrency mode to Multiple. If single or Reentrant are used, messages stack up as they wait to be processed, and that is badness.”

WCF CommunicationException: Error Serializing Parameter

WCF Serialization Issue

I really need to learn more about WCF.

I recently had a problem with serializing my business objects when trying to send to a WCF web service. The fix was the most simple thing in the world but it took me a considerable amount of time to discover it because I thought the problem was something else which led to a whole bunch of unnecessary changes which made matters worse…you know how the story goes ūüôā

This is the lovely errror I was getting when calling my Save method on my web service:-


Ok, so

  1. what was I trying to do
  2. what caused the problem
  3. and what was the (annoyingly simple) fix??

The Setup

First of all, I setup up a pretty much ‘out of the box’ WCF web service with a couple of simple changes shown here (You can download the sample project (with some dummy biz objects etc) here.)

Changes to WCF configuration

Changes to WCF configuration

(The relevant part is I’m reusing the types in the Biz assembly client side and server side.)

So just briefly, architecturally I have a Biz project which is referenced by my WCF Service and my client side Service Access Layer (SAL). My unit Test project is my ‘client’ in this case and is referencing both the SAL and Biz projects.

What I was trying to do was save a ‘Dog’ (one of my biz classes) using the web service. This Dog class looks something like this:

Dog Biz layer class

Dog Biz layer class

The Problem

So what, in this most simple of classes, was causing my serialization issue? The combination of two things…

The Cluprits

  1. The [Serializable] attribute
  2. and the FetchingBall event

When I create a new Dog and do NOT add a handler for FetchingBall, the Dog serialized to and from the WCF service no problems at all. Once you add an event handler….boom, the nice SerializationException occurs.

Now my ‘real’ biz objects implement INotifyPropertyChanged so 99% of the time there will be handlers and so 99% of the time they would fail to be serialized…not particularly useful!

The Fix

Mark your object with the [DataContract] attribute.


Well Ok you also need to now mark all of your properties/fields that you want to be included in the serialization with the [DataMember] attribute as using the DataContractSerializer is an ‘opt-in’ approach.

You can take off the [Serializable] attribute or you may have them both. Personally I want my Biz objects to be serializable in case I want to save them client side.

Extra Notes

If you want to see this in action just download the test project. The first two tests in Tests.cs are the important ones. It is currently setup so the second one will fail (as it registers a handler for the event).

Try it and then try uncommenting the [DataContract] attribute on the Dog class, then run them again and the second one should also work!

Creating Low Maintenance Methods

Great Article

I just wanted to post a link to this great article, it uses a great little example to teach a principal we should all be conscious of when writing methods….

This is a¬†quote from the last paragraph of the post….

“Whenever you write a method think about the contract of that method. What burdens are you imposing upon the caller? Are they reasonable burdens?¬† The purpose of a method should be to make the caller‚Äôs life easier; the original version of Lines() makes life harder on the caller. The new version makes life easier. Don’t write high-maintenance methods.”