Monday, November 30, 2009

Managed Extensibility Framework (MEF) Example

MEF is the .NET framework for building Extensible application. I was trying to find some simple Hello World application but the samples I got was not exactly what I was looking for.

I was still interested in Hello World app in a structure where at one place I will define that I need Hello World functionality which will take the name and return Hello #name. Now that contract information can be used by people to implement this functionality separately and then consumer of this functionality can use the contract and different implementations of it in an extensible manner so that the moment there are additional implementations of the same functionality it is directly available to the consumer application simply by copying the implementation dll to the Extension folder.

For this example I have downloaded the MEF Preview 8 from CodePlex.

http://www.codeplex.com/MEF

image

We will start with creating the project which will hold the information about the functionalities (Contracts) that needs to be implemented. Please note that the sample code provided here is in C# (don’t worry, pretty straight forward code), however if you are looking for VB.NET version I would highly recommend checking out the code conversion tool available at:

http://www.developerfusion.com/tools/convert/csharp-to-vb/

We will start by creating a Windows Class Library project and add a new Interface file called ‘IHello.cs’.

image

Let us add two more Class Library Projects to the same solution namely ‘Impl1’ and ‘Impl2’. These two implementation projects will need to have reference to the ‘Contract’ dll in their Bin folder. In addition to that we will also need to add the reference of the ‘System.ComponentModel.Composition.dll’ available from the MEF download from CodePlex. Now we will add ‘Hello.cs’ class that will implement the IHello interface to both of these Projects with the implementation of SayHello method as mentioned below:

Impl1:

image

Impl2:

image

We are ready with the Contract information and there are two implementations of that Contract. Now let us create a project that will consume this information in an extensible manner. We will create a Consol App that will have a reference of the Contract dll along with ‘System.ComponentModel.Composition.dll’. We will also create a folder called ‘Extensions’ in which we will copy the dll’s from the implementation projects that will be dynamically picked by our Consol App.

Let us add a Consol Project named ‘ConsolApp’ to the same solution and add the required references as mentioned above. Add the below mentioned code to ‘Program.cs’:

image

We need to create a folder named ‘Extensions’ in ConsolApp bin in which we will be copying the implementation dll’s.

image

Copy the ‘Impl1.dll’ to the ‘Extensions’ folder. Run the ConsolApp application and enter your name. We will see the result from Impl1.

image

Exit the application and copy the Impl2.dll to the Extensions folder and run it again.

image

Future Enhancements:

#1: Currently we need to restart the ConsolApp in order to reflect the changes in the Extension directory. Code needs to be updated to either Dynamically identify and update or give an option to user something like (e to Exit and r to refresh).

#2: We need to present the user with the implementation options available in the extensions folder and user can choose the extension and only the corresponding implementation is used.

Any suggestion to implement above two things are welcome also the corresponding source code will be helpful.

Sample Code (C#) can be downloaded from:

Saturday, July 4, 2009

IIS Hosting for WCF Service - Issue when Domain Name is used

Last week we ran into some issues with hosting of WCF Service in IIS 6.0. The issue was when we try to browse the service file (e.g https://MySubDomain.MyDomain.com/MyVirtualFolder/MyService.svc) in browser everything looks fine. However the WSDL definition URL mentioned beside svcutil.exe was displaying https://MyComputerName/MyVirtualFolder/MyService.svc

We did some Google search and found the solution in parts at various place. I though it would be good to put together this information at a single place so that it would be helpful to anyone facing the similar issue.

One important point to note here is that the web site where service is hosted is SSL enabled.

Resolution to this issue is stated in steps below:

#1: Make sure Service Behaviour tag in web.config is configured as mentioned below. Notice the keywords in Bold.

<behaviors>
    <serviceBehaviors >
        <behavior name="ExcelClientServiceBehavior" >
            <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
    </serviceBehaviors>
</behaviors>

#2: The issue might still persist is the Header Name is not properly associated with the Site Identifier. Here our observation was even though in IIS you can see that the Host Header Value displaying your domain name, but still we were getting the issue.

  • Open Command Prompt (Start ->  Run -> cmd)
  • Go to the folder for adsutil.vbs (C:\Inetpub\AdminScripts)
  • Run the below mentioned command
  • cscript.exe adsutil.vbs set /w3svc/<site identifier>/SecureBindings ":443:<host header>"

Replace <host header> with domain name e.g MySubDomain.MyDomain.com as mentioned in above example.

Replace <site identifier> with your web site identifier. For Default Web Site it is generally 1.

image

We can run the same command with different identifiers for each web site.

Hope you find this post useful.

If anyone has any correction to be made in this post, let me know and I will correct them ASAP.

Wednesday, May 6, 2009

Using functions from FSharp.PowerPack.dll

While working with F# in either Visual Studio or F# interactive if we try to use functions like print_string will give a compile time error.

This can be avoided by either adding reference to current session in F# interactive window by typing:

> #r "FSharp.PowerPack.dll";;

This way we will be able to execute the code using functions defined in PowerPack.dll however in visual studio it will still show the compile time error.

For resolving this we can put the same statement below #light keyword.

#light
#r "FSharp.PowerPack.dll"

This will not display the error message however a warning message will still be displayed stating the compatibility with OCaml.

image

This will work fine till the time we are working in .fsx file however will give error once you try to build your application. For resolving this FSharp.PowerPack needs to be added to Project Reference which can be found in .Net tab.

image

Friday, April 24, 2009

Blog Title Changed to "Technology Discussions by Ajay Singh"

This blog was initially started to share the thoughts on the PerformancePoint Server product from Microsoft. However with the recent announcement of PPS Planning being discontinued and Monitoring getting integrated to SharePoint, I thought it would be better to have a generic title for the blog. So I changed the blog title to "Technology Discussions by Ajay Singh" from "PerformancePoint Server Information by Ajay Singh".

 

This doesn't mean that I will stop posting the information about PerformancePoint Services. Its just that I may post information related to other technologies as well (which I have already started doing :) )