Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)
WCF Contrib v2.1 Mar07

A new release had been published - WCF Contrib v2.1 Mar07.

This release is the final version of v2.1 Beta that was published on February 10th, you can check the entire updates made from v2.0 further in this post.

You can distinguish between this release and the v2.1 Beta Feb10 by checking the assembly file version, in this release it was incremented to v2.1.0.1.

Changes from v2.1 Feb10 Beta:
There is a one small change which is a breaking change for those who used the asynchronous invocation pattern using the "InvokeChannelAsync" or "InvokeAsync". (protected methods of ClientChannel)
This is a seldom used pattern so the chances of that affecting you are pretty minimal.
The change is very simple, instead of working with AsyncServiceInvokeCallback in these methods, it gets the general AsyncCallback which it then calls by passing it a ChannelInvokeAsyncResult which contains all the information needed.

WCF Contrib Questions

I decided to blog about common questions I receive regarding WCF Contrib.

You can find all questions in the WCF Contrib FAQ.

Communication Patterns

  1. What's the best practice for web applications that call services?

    Research has shown that in concurrent environments such as web it is more scalable and has better performance to use a dedicated communication object per a sequence of invocations (sequential - one call after another in the same execution chain flow).

    It's important to note that for few users, you may notice that using a single communication object to address them all may work faster, however, as soon as more requests come in you should see a noticeable decrease in your application performance, which is bad.

    In essence, you should use the singleton instance for single calls or instantiate a new client channel with Sequential/SequentialAsyncOwnChannel to perform a sequence of calls at a given time and dispose it when you're done with it.

Client Channel

  1. When should I set the GlobalEndpointName?
    ClientChannel<T>.GlobalEndpointName = "myEndpointName";

    This needs to be set only once since it's the global name that will be used for all client channels created for the specific 'T' contract.
    It is usually placed in the process main entry point.

    Note that in WCF Contrib v2.1, if you have a single endpoint in the configuration for the given contract, you don't have to specify the endpoint name at all, it will pick it up automatically.
  2. When should I call LoadRuntime?
    ClientChannel<T>.LoadRuntime();

    This also needs to be called once, it too will usually be placed in the process main entry point.
    It is a good practice that you call it whenever you can spare time to load the runtime behavior information, this isn't a necessity though. If you don't call it the infrastructure will automatically load the runtime information upon first access.
  3. Is disposing the singleton instance mandatory?
    using (ClientChannel<T>. Instance) { }

    The implementation of IDisposable in the client channel disposes the channel factory and communication object associated with the client channel.

    The necessity of calling the dispose depends on the channel management mode.
    The singleton instance is set with SingleCall, this means that behind the scenes upon every call a communication object is created and terminated right after.

    That indicates it is not a necessity to call the dispose for such management mode. (The dispose of the factory isn't critical in this point since it just disposes the communication objects associated with it which were already disposed by the client channel infrastructure)

    As a general approach, I'd say "If it's IDisposable and you're done with it, dispose it".
    If you have control at your application/process exit point and you know which client channels you were using, I don't see a reason not disposing it.
Posted: Feb 10 2010, 12:22 PM by Amir Zuker | with no comments
Filed under: ,
WCF Contrib v2.1 – Customize Output Caching Provider

Via http://wcfcontrib.codeplex.com/wikipage?title=ClientChannelOutputCache

Output Cache Provider Factory
This is the factory class that is responsible for creating the actual output cache provider.
This is where you extensibility point kicks in, you can replace the built-in factory with your own, read about it here.

The factory needs to have a public parameter-less constructor and implement 'IOutputCacheProviderFactory'

public interface IOutputCacheProviderFactory
{
    IOutputCacheProvider CreateProvider();
}

As you can see, this is a simple factory interface for providing the actual provider implementation.

Output Cache Provider

Output cache provider is where the magic really happens.
This is where you should implement your custom output caching solution if you like.
public interface IOutputCacheProvider
{
    void AddItem(OutputCacheItemKey key, OutputCacheInterval interval, object value);
    bool TryGetItem(OutputCacheItemKey key, out object value);
}
  • AddItem - this method is supposed to add a new item to the output cache solution.
  • TryGetItem - this method should look up the output cache solution for the item that was stored.
  • OutputCacheInterval - you're getting the desired interval as a parameter.
    • Consider setting the fields "LastVisitedAt" and "AddedAt" to the proper values at a given time so you can reference it later.
    • You can use "IsActive()" method if you like to check whether the interval is active for the current time according to its policy and time properties.
Note: If the provider implements IDisposable, it will be disposed once the OutputCacheController is disposed.
The built-in provider is called "IntervalBasedCacheProvider", it is important to know this since this too is written in a reusable way where you can plug-in your custom store.
The provider gets two parameters in its constructor:
  1. purgeTimerIntervalInMilliseconds - The interval in milliseconds for the timer that is in charge of iterating the items and purging those which are inactive.
  2. store - An implementation for IOutputCacheStore
    1. AddItem - The provider basically saves the key and interval in its local state, sets the time properties of the interval to the current time and adds the item to the store.
    2. TryGetItem - When an item is requested from the cache, the provider checks if there's an active key in its local state, if so, sets the last visited time to the current time and gets the item from the store.
    3. Removing Items - In the timer callback, it iterates the keys in its local state and removes the inactive ones from the store

Output Cache Store
The store is responsible for storing the items.
public interface IOutputCacheStore
{
    void SetItem(OutputCacheItemKey key, object value);
    void RemoveItem(OutputCacheItemKey key);
    bool TryGetItem(OutputCacheItemKey key, out object value);
}

The store that is provided with the built-in solution is using the SynchronizedDictionary that is a part of WCF Contrib as well.

Implementing your own custom output caching solution
You can go about it in two ways:

  1. Plug-in your own factory and provider and implement a different scenario - you can use this approach to write a provider that uses an interval-based caching solution such as HttpRuntime Caching, Enterprise Library, etc
  2. Plug-in your own factory and still use the built-in IntervalBasedCacheProvider only with your own custom store, such as a AppFabric caching, file-system, database, etc
Output Cache Item Key
It's important to recognize the description of the item keys if you like to implement you own solution since you will need a way a way to correlate between the item and key.
  • The key contains the operation action and parameters and custom equality comparers if any were provided.
  • It can be used as a dictionary key since it overrides Equals and GetHashCode by calculating it over the action and parameters using the appropriate comparer.
  • ToString() is also overriden to return the aggregation of the action and parameters.
Posted: Feb 08 2010, 09:15 PM by Amir Zuker | with no comments
Filed under: ,
WCF Contrib v2.1 – Client Channel Output Caching

Via http://wcfcontrib.codeplex.com/wikipage?title=ClientChannelOutputCache.

A new feature that arrived at WcfContrib v2.1 Feb10 Beta.
You can use the output caching to cache the operation output with the setting of your desire.
This is extremely useful and may save you quite some time from building client caching stores.

The built-in mechanism implemented here is In-Proc in-memory cache using the synchronized dictionary that comes with WcfContrib as well.
You can fully customize it and change the underlying provider or store. (E.g. EntLib/ASP.NET Caching/AppFabric caching/Database/File system and whatnot)

If the result is present in the cache and is still active according to its policy and interval, this result is returned without going through the WCF pipeline whatsoever which enables very good performance.

Usage Guideline
If you're using the output caching feature, you should dispose the controller instance at your process exit point:
using (OutputCacheController.Current) { }

Features

  • Output Cache on several elements - The behavior can be attached to a client channel for either a specific client channel instance or contract or specific operations.
  • Different cache policies - Absolute / Sliding / Indefinite.
  • Ignore Default Value - There may be services which return null when a certain entity isn't found for example, in this case you can use this mode to avoid caching it so next time it will still call the service to try and fetch the entity. (defaults to false)
  • Compare Enumerable Items - When set to true (the default), A custom equality comparer will be used with IEnumerable parameters since in this case you usually don't want to check equality by reference.
  • Equality Comparers - You can provide your own set of equality comparers to be used, this is index-based.
  • Provider Factory Model - The model is fully customizable

What is NOT supported

The behavior doesn't support operations with out/ref parameter definitions.
This is due to the fact that the 'IClientOperationInvokerBehavior' (the core implementation for this behavior) wasn't designed while taking this into account in the first place.
This will be addressed in a future release.

Usage Scenarios

Contract-level Definition
[ServiceContract]
//This will affect only 'Do' in this case

//since the other operations have the behavior set explicitly on them [OutputCacheBehavior(OutputCachePolicy.Absolute, 3000)]
interface IService { [OperationContract] void Do(Foo foo); [OperationContract] [OutputCacheBehavior(OutputCachePolicy.Sliding, 2000)] Foo GetFoo1();



//Both the synchronous and asynchronous need to be tagged with the behavior,
//It will use the same underlying source for both of them, no worries. [OperationContract(AsyncPattern = true)] [OutputCacheBehavior(OutputCachePolicy.Sliding, 2000)]
IAsyncResult BeginGetFoo1(AsyncCallback callback, object asyncState);
Foo EndGetFoo1(IAsyncResult result); }

Client-channel based definition:
ClientChannel<IService> myChannel = new ClientChannel<IService>(...);

//Add output cache to a specific operation
//You can add it to the entire description or contract behaviors which will affect
//all operations that were not set with the behavior explicitly on them //You can also change the description of the global instance (ClientChannel<IService>.Instance) myChannel.Description.Contract.Operations[0].Behaviors.Add(new OutputCacheBehaviorAttribute(...));


Derived ClientChannel definition:
You can attach the behaviors in the InitializeRuntime or through attributes on your own derived client channel.
For more information see ClientChannel documentation file on the Client Channel Extensions model.

Customizing the output cache provider

I will explain about it in a future post

Posted: Feb 06 2010, 07:05 PM by Amir Zuker | with no comments
Filed under: ,
WCF Contrib v2.1 Configuration Section

Via http://wcfcontrib.codeplex.com/wikipage?title=WcfContribConfigurationSection.

Full-Blown Configuration

<configuration>

  <configSections>

    <section name="wcfContrib" type="WcfContrib.Configuration.WcfContribConfigurationSection, WcfContrib" />

  </configSections>

 

  <wcfContrib>

    <client applyBoosting="true" />

 

    <services applyBoosting="true" />

 

    <extensions>

      <logTrace logFolderPath="C:\MyLogsFolder\" />

      <outputCache providerFactoryTypeName="MyOutputCacheProviderFactoryTypeName" />

    </extensions>

  </wcfContrib>

</configuration>

Client

Apply Boosting
You can configure the default value of the "ApplyBoosting" of all Client Channels (defaults to 'false').
Boosting is a common need for internal communications, it sets all the following to the maximum value:
  • MaxItemsInObjectGraph
  • Buffer sizes, MaxReceivedMessageSize and Reader Quotas. Please note this is set upon known standard bindings. If you're using a different binding or a custom one you need to apply it yourself.

Services

Apply Boosting
You can configure the default value of the "ApplyBoosting" of all the hosts using the Service Host that comes with WcfContrib (defaults to 'false').
  • Increases throttling to a large number ONLY if it has the default throttling, meaning if the developer hasn't set it to something else.
  • MaxItemsInObjectGraph
  • Buffer sizes, MaxReceivedMessageSize and Reader Quotas. Please note this is set upon known standard bindings. If you're using a different binding or a custom one you need to apply it yourself.

Extensions

Log Trace
You can configure settings related to logging and tracing extensions.
  • logFolderPath - Change the folder path used for the tracing and logging files. (The default is writing it to the same location as the Entry assembly)

Output Cache
You can configure settings related to the output caching extension.
  • providerFactoryTypeName - You can set the type name for your custom output cache provider factory
Posted: Feb 06 2010, 06:58 PM by Amir Zuker | with no comments
Filed under: ,
WCF Contrib v2.1 Feb’10 Beta

I am happy to announce a new version of WcfContrib was published today.

It has been tested and it is stable. However, there were some core optimizations so this is considered to be a small test-run to make sure it is alright before tagging it as a stable release.
Please provide feedback, your input is important.

Changes from last version:

  • New Output Caching module for Client Channel – Very useful feature!
  • New behavior attributes and extension elements (ActivationContextInitializerAttribute, ActivationContextExtensionBehaviorElement, ErrorContextHandlerExtensionElement)
  • A userful commonly-needed synchronized (thread-safe) dictionary was added: SynchronizedDictionary
  • Internal Optimizations and Mode Fixes. (Sequential channel management option work with asynchronous operations now)
  • ChannelInvokeContext was made an extensible object
  • Client Channel supports default single endpoint - No need to specify an endpoint name of the client endpoint in the configuration if there is only one for the specific contract
  • Configuration-less services hosting API improvements
  • When deriving from ClientChannel, you can override OnChannelFactoryCreated (new method) to apply anything you need.
Updated Showcases from last time:
  • Miscellaneous - Configuration Section - A sample of the new Configuration Section
  • ClientChannel - Basic - Simple comment indicating the ApplyBoosting can be set globally through the new configuration section.
  • Services - Hosting - Simple comment indicating the ApplyBoosting can be set globally through the new configuration section.
  • Services - Service Extensions - Simple comment indicating about the new behavior attributes and extension elements you can use to apply the extensions declarative or in configuration.
  • Services - Service Error Logging - An example for attaching the behavior in the configuration using the new extension element was added.
  • Client Channel - Output Caching - A new sample was added demonstrating the basic usage of the output caching

I plan to write a series of posts demonstrating the features and implementations of WCF Contrib.
This isn’t a graphical control library but rather a communication library, thus I understand the need for proper documentation and guidance.
Feel free to contact me for any information you need.

Posted: Feb 05 2010, 07:31 PM by Amir Zuker | with 1 comment(s)
Filed under: ,
Wcf Contrib v2 - New Release - August 2009

It's been ages since my last post, I feel so bad about it.
I've been swamped at work, I hope I will get the blogging frenzy again soon :)

Don't forget this is my old blog though! I post here only on WcfContrib, for my new blog make sure you visit my current blog.

I'm very glad to announce a new release of WcfContrib!

New stuff:

    * Client Channel - Closes channels asynchronously after invoking O/W operations (Check the post WCF OneWay - Abort or Not regarding why)
    * Service Extensions - Mocking & Tracing with the ability to trigger from the client side
    * Configuration-less Services
    * Integrated Endpoint and Binding Boosting - Service and Client side
    * Enhanced Helpers

Go have a look, recommended :)

Posted: Aug 07 2009, 01:16 AM by Amir Zuker | with no comments
Filed under: ,
WCF Contrib - A new release, May 2009

This release delivers a better packaged assembly.
XML documentation was added, showcases don't require password for compiling the solution (because of the key file)

Context extensions were added - Session and Call carried items similar to ASP.NET.

Wcf Contrib

Posted: May 03 2009, 01:26 AM by Amir Zuker | with no comments
Filed under:
WCF Contrib - Packaging
There was a problem with the zipped archive with the showcases (In the Downloads section)
It was fixed, please download it again if you wish.
Posted: Apr 30 2009, 09:22 AM by Amir Zuker | with no comments
Filed under:
WCF Contrib

I no longer update this blog, blog has moved Here.

I decided to post here also about the release of WCF Contrib.

There are a log of useful extensions and components for you to use with WCF communications, be sure to check it out.

There's a lot to come in future releases too.

Posted: Apr 26 2009, 01:00 AM by Amir Zuker | with no comments
Filed under:
Blog has Moved!

My blog has moved!

Please invest a couple of minutes to update your feed subscription.

The new location:
http://blogs.microsoft.co.il/blogs/zuker

Syndication link: http://feeds.feedburner.com/ZukerOnFoundationsMs

See you there ;)

SQL Reporting Services - Query Report Server

It took me quite a while to find a proper example of how to communicate with SQL Reporting Services in order to execute a specific report, I though I might share it.

We have Report Server 2008 installed over SQL Server 2005.
I needed to extract data of a specific report in runtime (desired output in my case - XML).

I found 2 suitable ways of doing so:

1) Use ReportExecution2005 Service

SQL Reporting Services include the following:

ReportService2005 - A service which exposes the realm of the entire reports
ReportService2006 - Represents the SharePoint integration mode

ReportExecution2005 - A service used to execute reports - my main focus.

TalkWithWs() method in the attached project illustrates the use of the ReportExecution2005 service.

2) Use the Report Server ReportViewer page directly

This is a very easy way to extract the data.

I could extract any report's data through the ReportViewer page, E.g.
http://<Server>/<ReportServer Instance>/Pages/ReportViewer.aspx?%2fSandBox%2fadv_testforAmir&rs:Command=Render&rs:Format=xml

This will yield the XML data for the following report path: /SandBox/adv_testforAmir

Very clean and handy.

This mechanism also support parameters (through the QueryString)

The source code is attached.
In order to view the attached files, make sure you enter the specifc post page.

Posted: Jan 11 2009, 05:11 PM by Amir Zuker | with no comments
Filed under:
WPF Popups and ToolTip Behavior - Implementation

In continuation to my first post -
WPF Popups and ToolTip behavior - A Journey

I decided to approach the implementation of such a popup like the ToolTip and ContextMenu are doing it themselves.
I did not inherit from Popup but rather created my own control and I used the Popup.CreateRootPopup which sets the Popup.Child property to my control and it picks up on all the ToolTipService properties defined on my control.

I started from reading the following post:
Popup your control

There were some problems with this pattern though, I had to overcome these with quite many event hooking which I'm not very happy about. At least it works for now :)

There is a way to improve the implementation, I could use mouse capturing but that introduced another set of problems so I left it as it is for the time being.

I will post my solution soon enough.

Posted: Jan 11 2009, 03:24 PM by Amir Zuker | with no comments
Filed under:
LINQ to XML - XElement - Descendants vs. Elements

Someone asked me what the difference between descendants and elements is, I thought I'd post the details here for common interest.

Descendants will yield you elements of your choice from the entire source element sub-tree, whereas Elements will yield the child elements only.

E.g.

 string xml = @"
                <Root>
                    <Item>
                        <id>1</id>
                    </Item>
                    <Item>
                        <id>2</id>
                    </Item>
                    <Item>
                        <id>3</id>
                        <Items>
                            <Item>
                                <id>5</id>
                            </Item>
                            <Item>
                                <id>6</id>
                            </Item>                            
                        </Items>
                    </Item>
                    <Item>
                        <id>4</id>
                    </Item>
                </Root>";

            XDocument doc1 = XDocument.Parse(xml);

            var q1 = from e in doc1.Root.Descendants("Item")
                     select e;

            var q2 = from e in doc1.Root.Elements("Item")
                     select e;

            int c1 = q1.Count(); //6
            int c2 = q2.Count(); //4

Posted: Jan 11 2009, 03:18 PM by Amir Zuker | with no comments
Filed under:
WPF Popups and ToolTip behavior - A Journey

I am currently working on a WPF project which I needed a ToolTip with a certain behavior:

  • It should act like a ToolTip
  • Support content template
  • Appear upon hovering with a small interval
  • Appear once - hovering another element with a tooltip should hide the last shown tooltip.
  • Support Rich Content 

The latter is marked as bold since that was the essential requirement that made me not use the ordinary WPF ToolTip.

I needed the ToolTip to hold rich content - such as clickable buttons.
This also implicates that I wanted the ToolTip to remain shown until either one of the following

  • The window was moved/size changed
  • The window was deactivated (E.g. Alt+Tab)
  • A click was made outside the boundary of the shown tooltip
  • Otherwise - Keep being shown! - Unsupported by the built-in ToolTip.

I went over and examined my possibilities along with my close friend Aelij Arbel who worked together with me on this.
The options we were considering are as follows:

1) Built-in ToolTip - As indicated above - unsuitable.

2) Context Menu - The behavior I was looking for resembled the Context-Menu behavior, so I thought about perhaps I could template the Context menu to look like I wanted and try to change it's core Right-Click functionality but it turned out to be quite complicated and I'm unsure it's possible.

3) Use Popup - Popup is a great thing. It allows you to place content apart from the window Visual Tree - floating.
It supports most of the ToolTip placement properties which I needed - Let the popup control the placement positioning, take a lot of pain off my back.

Obviously, the third way appeared to be the best way to go.

Good news - Popups did do the trick I needed. However, they wouldn't work properly in some cases.
Unfortunately, bad news - It didn't in my case.

I attached a project sample showing some behavior disfunctions in the Popup, just so you see for yourself there are issues with that.

That was the starting point of my journey - I was going to create such RichToolTipPopup behavior that would be supported in every case!

I will post about my solution in the following days.

In order to view the attached files, make sure you enter the specifc post page.

Posted: Jan 07 2009, 12:04 PM by Amir Zuker | with 2 comment(s)
Filed under:
Next page »
Page view counter