WPF Themes
November 19, 2008I knew for a time that Visual State Manager from Silverlight had been ported for WPF, via the WPF.Toolkit project.
Today I found out that there some interesting themes based on the Visual State Manager.
Here is the post
I especially like DavesGlossyControls

WPF ShowDialog
September 12, 2008There is an issue with ShowDialog for Windows not always obeying Modal Dialogs rules.
So I have created a static class which all ShowDialog calls will be redirected.
public static bool ShowDialog(Window dialog,Window owner) { if (owner != null) { WindowInteropHelper helper = new WindowInteropHelper(dialog); helper.Owner = new WindowInteropHelper(owner).Handle; } bool? dialogResult = dialog.ShowDialog(); return dialogResult.HasValue ? dialogResult.Value : false; }
I’ve made some corrections so this is the new code
public static bool ShowDialog(Windowdialog, Windowowner)
{
WindowInteropHelper helper = newWindowInteropHelper(dialog);
if(owner != null)
{
helper.Owner = newWindowInteropHelper(owner).Handle;
}
else
{
helper.Owner = GetActiveWindowClass.GetActiveWindow();
}
bool? dialogResult = dialog.ShowDialog();
returndialogResult.HasValue ? dialogResult.Value : false;
}
where GetActiveWindowClass.GetActiveWindow is a interop wrapper
public static class GetActiveWindowClass { [DllImport("user32.dll")] public static extern IntPtr GetActiveWindow(); }
DirectoryEntry Enabled
August 27, 2008I need to check whether an account in the active directory was enabled.
After some searching, I came to understand that the enabled feature is not only for User but for every node in the active directory. That means every DirectoryEntry instance.
Here is how you check if it is enabled
public const string AccountControl="userAccountControl"; public const int EnabledFlag= 0x2;static bool IsEnabled(DirectoryEntry de) { return (((int)de.Properties[AccountControl].Value)& EnabledFlag) == 0; }
Active Directory Connection
August 27, 2008Recently I have been asked to query our Active Directory.
I really believe that the documentation is really poor about the connection, and what the errors mean.
After all the problems, and the never ending errors that I could not comprehend, I stumped upon a post that said LDAP prefix must be Uppercase.
A few thing about the connection string, since I am writing this post.
I do not know of Active Directory administration so, what I will write here is my experience in this situation and hopefully someone will benefit.
At my company we have a domain which from windows we see as DomainName. There are a few places in windows, that I had seen a local suffix in out domain. I do not know what this means. Our domain is located on machine called SERV1 for example with IP 192.168.0.1.
In case I’m not entirely clear about my environment, let me specify that my computer registers on the network as DomainName\PCName and my Domain Account is DomainName\sarafian.
The only valid connection string that works for us is LDAP://192.168.0.1/dc=DomainName,dc=local
Anything else just doesn’t.
Here is the creation of root DirectoryEntry
private static DirectoryEntry GetRoot() { DirectoryEntry de = new DirectoryEntry("LDAP://192.168.0.1/dc=DomainName,dc=local", null, null, AuthenticationTypes.Secure); return de; }
Hopefully someone will benefit and won’t get frustrated as I did, with all the combinations and their incomprehensive error messages.
How to debug a WCF service
June 28, 2008Introduction
WCF default debugging does not support edit and continue features, as I have said in my blog post
Background
In that post I said that there is an easy way to do it, but I didn’t give any code. So this article is about how to it. The reason I wrote this article, is because a fellow member wrote a useful article about wcf, and in tip1 he suggested using a console application.
My scenario for wcf service consuming and debugging is a Client Server application. I believe my solution can apply and for other scenarios.
How to do it
You need this class DebugServiceHost
internal class DebugServiceHost:IDisposable { private static string serviceExecuteTypeName = "ExecutionService.ServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; private static string iServiceExecuteTypeName = "ExecutionService.IServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; public static string Address = @"http://localhost:9999/ExecutionService/ServiceExecute/"; public static Binding Binding = new WSHttpBinding(); static DebugServiceHost() { isEnabled = Type.GetType(serviceExecuteTypeName) != null; } public DebugServiceHost() { Type serviceType = Type.GetType(serviceExecuteTypeName); Type iServiceType = Type.GetType(iServiceExecuteTypeName); this.debugHost = new ServiceHost(serviceType); this.debugHost.AddServiceEndpoint(iServiceType, Binding, Address); this.debugHost.Open(); } private System.ServiceModel.ServiceHost debugHost; private static bool isEnabled = false; public static bool IsEnabled { get { return isEnabled; } } #region IDisposable Members private bool disposed = false; public void Dispose() { if (this.disposed) { return; } this.debugHost.Close(); GC.SuppressFinalize(this); } #endregion }
where ServiceSide.ExecutionService.ServiceExecute is the namespace of the wcf service and ServiceSide.ExecutionService the assembly containing it.
Using the code
I use a provider class, but its up to you how to implement the above feature.
DebugServiceHost has a static property IsEnabled, which basically checks whether the service is locally located to the running client execution folder. If that is true then using an instance of the class, host the service as well, which is automatically disposed.
This a sample code from my project that consumed the true or debug hosted service.
Collapse
public static class Provider { private static DebugServiceHost dbh; public static void CheckAndRunDebugService() { if (DebugServiceHost.IsEnabled) { dbh = new DebugServiceHost(); } } public static string RemoteURL { get; set; } private static IServiceExecute iServiceExecute = null; private static IServiceExecute GetInterface() { if (iServiceExecute != null) return iServiceExecute; iServiceExecute = CreateClient(); return iServiceExecute; } private static ServiceExecuteClient CreateClient() { string address = ""; if (DebugServiceHost.IsEnabled) { address = DebugServiceHost.Address; } else { address = String.Format("{0}/Service.svc", Provider.RemoteURL); } return new ServiceExecuteReference.ServiceExecuteClient("DefaultEndPoint", address); } }
where
IServiceExecuteis the Interface created when you add the service Reference.ServiceExecuteReference.ServiceExecuteClientis the client created when you add the service reference- DefaultEndPoint is the endpoint in my config file.
Points of Interest
At first I at the first call to the service, I create the host. But as I mentioned in my post there was problem with that implementation.
Last problem hopefully
Finally a point of interest which was my last obstacle that took me a half day to find. I had implemented a provider class for the remoting part, which if needed, fired up the service host. Everything worked just great in the test projects, but at some point I tried to extend the framework with WPF.
The trick was that at first request, through static constructor I checked if the service was required to be hosted, and did that. But this did not work when I made the call from WPF. The only error was a timeout exception. I was going crazy, and then it kicked in. Never trust completely a 3 party library.
Solution
I made three clients, one Console, one Windows one WPF and stripped down the functionality of my framework to test. On each UI Client I made the call (and raised subsequently the host) after a UI reaction. I turns out that neither Windows Forms played correctly, when for example the call is made through a button click event. When I saw that, I made the host come up before the UI part was ever initialized and guess what? It all played just fine.
I really can’t understand how this has not been mentioned.
So I refactored the code, and at the program main entry method I call Provider.CheckAndRunDebugService()
Final thing is that, you must implement a trick in order for all serverside assemblies to be found in the client execution folder when you are debugging. There are a number of ways to do achieve this mainly by using post build event or by just adding the references and deleting them when in deployment face. I use the first.
Apologies
There is not project code, because the above resides in a framework project, I have written for the company I work for.
LINQ to SQL not Suitable for LOB
June 17, 2008In my previous post I mentioned that some post or blogs on the Internet are hugely misleading about the available technologies, since they tend to hide basic facts and focus on the superficial magic, which sometimes simply don’t matter when the technology is unusable.
In the company I work for, we rejected LINQ to SQL about a month ago, after trying to solve its biggest problem that of performance combined with thread safe when cashing the Data Context. So any info I have found from various sources are not available.
What is LINQ?
LINQ is basically a collection of extension methods to any Enumerable object.
What is LINQ to SQL?
LINQ to SQL is LINQ over the enumerable objects created while drag and dropping tables and procedures in a dbml file. This classes are known as Entities and the object that manages them as a DataContext.
My Sin
I really don’t like SQL. It reminds me of procedural programming which I stopped writing a decade ago. So any technology that will allow me not write SQL is more than welcome for me. So when I first started reading about LINQ to SQL, I started thinking at last an ORM from Microsoft herself.
My Sin was that in spite of the objections of our more experienced programmer, I was standing by LINQ to SQL, mainly using arguments that came from the notion that everything was great in LINQ to SQL. This notion existed because I believed the posts on the Internet.
One of the biggest objections was that of security. It was unacceptable for him, for an application to have access to the tables of a database. My lack of SQL knowledge, didn’t take that in mind, so as then, supposedly there is no security objection for the rest of the post.
First Impressions
At first I was really disappointed because there was no support for DataSet manipulation through LINQ to SQL. At first I hadn’t realized what LINQ is exactly, so I implemented a library that did this job. When building N-Tier applications, Typed DataSets are the most effective solution for the business model. I wrote about it here. Having wrote this library I was really convinced that we had a great tool for LINQ to SQL, and that it was the choice for our Data Access Layer.
Problems arising
Having spent time to build this library, it now came to check whether LINQ to SQL was the valid choice for out Application. So we created a huge table in SQL Server and started case testing and comparing with know DataSet methodology.
A Data Access Layer will be used by a Web or a Server Side of an application. In order to be thread safe, you need to create the Data Context with each call. But this is slow when data are huge, and there were some posts saying that there would be a way to cache the data context through a configuration setting. So I automatically assumed that caching and thread safe has been taken into account.
But that wasn’t the case. The sad truth is that, if you cache the Data Context you must create and maintain one for every thread or Http Context in order to make them thread safe, with a trick I read from someone else. Practically you don’t make them thread safe, but thread specific.
With caching, performance improved greatly. In some cases it was quicker than DataSet methodology. The main reason was that, while the queries run, the Data Context of each thread kept its entities in memory so no queries to server where required after a number of iterations. After this was noticed I immediately thought what about memory consumption in the server as the execution time passed. How the hell do you manage this side effect.
Conclution
LINQ to SQL might be very appealing when reading about simple objects and simple applications, but when the application gets big the coordination problems that might occur and produce data corruption, are clearly the death tomb of LINQ to SQL for LOB. And the sad part was that, I fell victim to all those glorious posts. I believe this is a risk that no company should ever take. Data integrity is something that one must never mess with. This includes the security objection mentioned above.
LINQ to SQL is a proof of concept. It is one of the things that in IT theory look great but when put in practice, it proves once again that theory sometimes doesn’t relate to practice.
WCF Debugging and a WCF review
June 17, 2008A comment
At some point, people need to understand that, business applications are not like console applications. So all these comments on the net, in blogs and forums, should be a little more double checked for real life development process because they mislead other people. Especially for LINQ to SQL and WCF, I am really bored reading about how great they are and how easy they are, but the truth is that LINQ to SQL is not for applications (reason in another post) and I was one of the victims that tried to adopt it, and WCF although is great, it can also be a pain in the ass. When searching through the net, keep in mind always that Internet is not always correct as I have wrote here. I really suspect that many of the blog post although, they can be helping, are not revealing entirely the truth and the disadvantages. I have always been fun of Microsoft Technologies but there is a limit to the indirect advertisement.
Following instructions
Back to the post subject. Many of you have done what everybody on Internet says. Add a WCF Service and a Service Reference and Great, all is ready and done. Even for debug, every time I execute debug the application, a dummy Debug Host is raised and I can debug the service. All is great? No.
Questions
First of all. What if I’m not running parts of the solution that require the service. Why should I be punished with the overhead of raising the host?
Second and most serious. Has really anyone tried to debug the service, and every underlying class used by it? Has anybody tried to utilize edit and continue, among all these guys who say how great WCF is?
I tried and as you can guess from my attitude, I could not utilize Edit And Continue. When I am developing a big Application, usually there will be at least 2 layers behind the service. Should I restart the application each time? And don’t let me talk about the debug through the dummy client, when an Operation Contract of mine, will use a complex data type. It is just not possible.
So what is the solution?
Easy someone can say, but easier said than done. As you would with .Net Remoting, If the service is located in your output directory, by whatever trick in the solution, then just raise the host programmatically in an address of your choice and then tell the client to hit this address. This way you have always simulating data transferring through WCF channels (very important), and you can of coarse use the feature of Edit and Continue.
The problem is that the dummy service the solution raised keeps coming up, which is very annoying. I haven’t found a solution, mainly because in the framework I’m developing, there is a single Operation Contract handling abstract Message Types. This was another great milestone for me in WCF. I really can’t understand why they have made DataContractSerializer as complicated, and not simple as the one used in plain old fashioned remoting. Having this service in my framework, I do not have in my solution the WCF Service so , there is no penalty overhead from the Debug Host that is raised.
Last problem hopefully
Finally a point of interest which was my last obstacle that took me a half day to find. I had implemented a provider class for the remoting part, which if needed, fired up the service host. Everything worked just great in the test projects, but at some point I tried to extend the framework with WPF.
The trick was that at first request, through static constructor I checked if the service was required to be hosted, and did that. But this did not work when I made the call from WPF. The only error was a timeout exception. I was going crazy, and then it kicked in. Never trust completely a 3 party library.
Solution
I made three clients, one Console, one Windows one WPF and stripped down the functionality of my framework to test. On each UI Client I made the call (and raised subsequently the host) after a UI reaction. I turns out that neither Windows Forms played correctly, when for example the call is made through a button click event. When I saw that, I made the host come up before the UI part was ever initialized and guess what? It all played just fine.
I really can’t understand how this has not been mentioned.
Conclusion
For me WCF is good for the plumbing. It is much more complicated than .Net remoting, really hard to troubleshoot if you are doing something outside the ordinary, which are discussed in all those praising posts and articles. Maybe I haven’t studied it as much, maybe I’m missing something but if the case is true, tell me how something that is advertised as easy and all remoting-problem solving can be this hard to utilize and debug. You will say that WPF, has as much difficult learning curve but WPF is not advertised as the magic trick that the programming world was missing. Since I have read about it, everyone mentioned that it is hard and difficult to adopt, and it is not for all kinds of applications. For me WPF is the star of .NET3.
Despite all these problems I really believe in WCF, because of other great stuff that it supports. Regarding security MS says here that
You should not use WCF Service Host to host services in a production environment, as it was not engineered for this purpose. WCF Service Host does not support the reliability, security, and manageability requirements of such an environment. Instead, use IIS since it provides superior reliability and monitoring features, and is the preferred solution for hosting services. Once development of your services is complete, you should migrate the services from WCF Service Host to IIS.
Having this in mind, I can’t stop thinking whether WCF is a great overhead on development, when .NET is remoting is a well tested solution under IIS. But choices have been made, mainly because the new technology I believe is here to stay.
Links
Same Article at code project.
Set a value to a control only in design mode
June 14, 2008I have been working on making my project localizable. I made it using Resources just like skins but this will be another post.
The final problem I encountered was that there was no design time support to show a dummy text, in a Button for example. Everything was going well except from the design part which was not that significant for me, but as I’m the writer of the company’s new framework it might be for others.
So can you set a value to DependencyProperty of a DependencyObject only to in DesignMode?
Using a custom MarkupExtension you can. Here is the implementation
[ContentProperty("Value")] public class DesignTimeDummy : MarkupExtension { public DesignTimeDummy() { } public object Value { get; set; } public override object ProvideValue (IServiceProvider serviceProvider) { if (Helpers.Designer.InDesignMode) { return Value; } return DependencyProperty.UnsetValue; } }
And this is how you can use it
<Button Style=”{DynamicResource styleName}” Content=”{CoreME:DesignTimeDummy Value=DesignContentValue}”/>
or
<Button Style="{DynamicResource cancelButtonStyle}" > <Button.Content> <CoreME:DesignTimeDummy> Dummy </CoreME:DesignTimeDummy> </Button.Content> </Button>
where CoreME is an xmlns defined namespace shortcut
In design mode
June 14, 2008WPF project has begun and I have been continuously finding new things. I’m very excited, mainly because what I have accomplished to today. Maybe I will write about it.
Many thanks to fellows like Josh,Karl and Sasha for the knowledge I have acquired from them since Summer 2007.
One of the things I needed, for my next post, is to know if code is running in DesignMode or not.
So here is the class I wrote
public static class Designer { private static DependencyObject dummy = new DependencyObject(); public static bool InDesignMode { get { return DesignerProperties.GetIsInDesignMode(dummy); } } }
Posted by Sarafian Alex
Posted by Sarafian Alex
Posted by Sarafian Alex