Archive for category WCF

Using WCF without configuration files

I have recently published new project on GitHub, WcfWithoutConfigFile. This post is intended as a ‘readme’ file for this repository.

It’s just a collection of simple projects that I’ve put together when a coworker asked me to show how he can use WCF without configuration files. He got feed up working with XML in web.config and wanted a cleaner and more understandable way to configure Windows Communication Foundation.

What’s in:

  • How to use WCF in your unit tests (WcfWithoutConfigFile.Tests project). Sometimes you want to get your code tested at service boundaries and you want to involve also WCF. It contains, among a simple unit test, a base class for your tests so the test cases are clean and don’t contain plumbing code.
  • Hosting WCF services in IIS (WcfWithoutConfigFile.WebHost project). Defining the service in .svc files and all required infrastructure to instantiate the service via code-only configuration.
  • My favorite – hosting WCF service in IIS using Castle’s WcfIntegration facility  (WcfWithoutConfigFile.WebHost.Castle project). It leverages Castle Windsor container and WcfFacility to easily host/run/consume your WCF services. It also configures the service to use Windows authentication and shows how to retrieve client’s WindowsIdentity in the service. There is also a client project, WcfWithoutConfigFile.WebHost.Castle-Client that connects to service and does some operations.

WcfWithoutConfigFile.WebHost.Castle requires some configuration to get it working properly. Be sure to:

  • Run it in IIS Express
  • Enable Windows Authentication in project properties

1 Comment

WCF Security Guidance

It seems to be that Patterns & Practices team continues to do what they do the best – creating guidance within different development areas.

The last release of this group is the WCF Security Guidance. It contains description for a few application scenarios & a list of various How-Tos, including few video walkthroughs of common WCF solutions.

It’s different from usual way to dig into WCF infrastructure details. It’s not crawling over hundreds of MSDN pages, it’s not like reading MSDN samples trying to understand what’s going on.

It is a very detailed step-by-step description on how to do some tasks touching different aspects of WCF, starting from opening a certificates store, and ending with deployment advices.

Keep it going, P&P!

Leave a Comment

HttpContext idiom for Windows Communication Foundation.

WCF infrastructure allows you to store context sensitive data in InstanceContext of the service instance. For that you should implement from IExtension<InstanceContext> and plug that class into WCF’s infrastructure in any of available ways.

When I worked on a class that can store contextual information in Web context or WCF context depending on some configuration parameters, I preferred to have similar idioms, and I wrote an HttpContext-like class for WCF.

///<summary>
/// This class incapsulates context information for a service instance
///</summary>
public class WcfInstanceContext : IExtension<InstanceContext>
{
    private readonly IDictionary items;

    private WcfInstanceContext()
    {
        items = new Hashtable();
    }

    ///<summary>
    /// <see cref="IDictionary"/> stored in current instance context.
    ///</summary>
    public IDictionary Items
    {
        get { return items; }
    }

    ///<summary>
    /// Gets the current instance of <see cref="WcfInstanceContext"/>
    ///</summary>
    public static WcfInstanceContext Current
    {
        get
        {
            WcfInstanceContext context = OperationContext.Current.InstanceContext.Extensions.Find<WcfInstanceContext>();
            if (context == null)
            {
                context = new WcfInstanceContext();
                OperationContext.Current.InstanceContext.Extensions.Add(context);
            }
            return context;
        }
    }

    /// <summary>
    /// <see cref="IExtension{T}"/> Attach() method
    /// </summary>
    public void Attach(InstanceContext owner) { }

    /// <summary>
    /// <see cref="IExtension{T}"/> Detach() method
    /// </summary>
    public void Detach(InstanceContext owner) { }

}

Now, you can use this class to store and retrieve data in the same manner as you’re working with HttpContext:

    WcfInstanceContext.Current.Items["key"] = new MyClass();
    MyClass myClass = WcfInstanceContext.Current.Items["key"] as MyClass;

 

Of course, when doing this you should be inside of WCF session…

Enjoy!

8 Comments

Follow

Get every new post delivered to your Inbox.