THINK

that’s how i naturally know

Archive for the ‘csharp’ tag

WCF Tutorial: WCF Service with Windows Service (Part 3 of 3)

leave a comment

Just a stub

Creating Service Contract
Creating DataContract
Creating FaultContract

Creating Windows Service Host
Creating Proxy
Creating Client

Creating app.config with WCF config Editor in Microsoft Visual Studio 2010
Installing/Uninstalling/Starting/Stopping Windows Service Host with sc.exe
Use client to connect proxy to host, and consuming services.

Programatically configure service and proxy using and without using app.config

Concepts beyond: Port sharing, IIS hosting, behaviors, tracing, message logging, using MSVS tools to do Metadata extraction.

Written by Jake

December 7th, 2011 at 2:55 pm

Posted in Programming

Tagged with ,

WCF Tutorial: WCF jargon (Part 2 of 3)

leave a comment

Just a stub:

Service
Host
Endpoint
Binding
Metadata
Behavior
Protocol

Tracing
Message
Fault

Proxy
Client

Written by Jake

December 7th, 2011 at 2:47 pm

Posted in Programming

Tagged with ,

WCF Tutorial: WCF Overview (part 1 of 3)

leave a comment

Just a stub first:

WCF is Windows Communications Foundation
By default, it sends message using SOAP protocol by serializing data structures into XML.
For serialization, it specifies the DataContract and DataMember attributes.
For Interface programming, it specifies the ServiceContract and OperationContract attributes.
For Error and Exception handling, it specifies the FaultContract attribute.

Language agnostic (the only thing fixed is SOAP) such that JAVA or other apps can consume the services.
Hide implementation and expose only interface specific faults (as oppoosed to Exceptions)

A WCF service is typically compiled as an independant DLL that can be hosted by one of several methods i.e. IIS, Windows Service or Self-hosted e.g. in a WinForms application, WPF application.

The Microsoft .NET 4.0 framework with Microsoft Visual Studio 2010 provide some magic code and convenient designer UI to build both the WCF service and the client.

Written by Jake

December 7th, 2011 at 2:44 pm

Posted in Programming

Tagged with ,

One-to-many mappings

leave a comment

A lot of times I had to look for a data structure to store one to many relationships such as a list of products keyed by their categories. I remember the last time I coded in C++ I used a whole lot of std::multimap to do just this.

With C#, there isn’t such a container. The only way is to construct your own container by creating some sort of Dictionary<Key, List>.

Better still, if you only need to add elements once you can just use a simple List<data>, add your data by List.Add() then make a LINQ call to List.ToLookup(). And you immediately have a one to many mapping. This is much more elegant since you do no need to check the dictionary for existing key each time you want to add data.

Written by Jake

October 3rd, 2011 at 11:52 pm

Posted in Programming

Tagged with , ,

C# static initialization sequence

leave a comment

Object orientated code could break down dangerously when using static class members in an unorthodox manner. Static initialization sequence goes like this:

1. Program starts, mark all classes that has static members as statically uninitialized.
2. Enters a scope where static calls are made.
3. Check, in order of execution sequence, get the first class in scope that is statically uninitialized.
5. Enter class scope, mark current scope as visited and Goto (7) if one of these are true:
- No static calls in current scope
- All classes have been marked as initialised
- All classes have been marked as visited.
6. Goto (3)
7. Initialize all class static members with values in sequence
8. Resume (2)

Written by Jake

August 24th, 2011 at 11:12 am

Posted in Programming

Tagged with

IEumerable cannot be used as a single object datasource

leave a comment

In the case of binding a an object and its property to, say, a control using the feature

control.DataBindings.Add("Text", myObject, "PropertyName");

will always work except when myObject is of the IEnumerable type. Once that happens, the data binding architecture will attempt to use the list of objects returned by the Enumerator for updating of UI and data source, which is not desirable for our purposes. For example, with

 public class MyObject : Hashtable
{
    public string PropertyName { get; set; }
} 

the DataBinding will fail logically because Hashtable is in fact an IEnumerable in disguise! Unfortunately there seems to be no way of telling .NET that we want it to treat MyObject as a single object, not unless we wrap it in another BindingSource or IEnumerable

Written by Jake

August 16th, 2011 at 10:02 am

Posted in Programming

Tagged with ,