April 2006 - Posts
When you install WinFX, you also get a long list of Windows Communication Foundation Technology Samples.
Just go to C:\Program Files\Microsoft SDKs\Windows\v1.0\samples and locate AllWinFXsamples.zip there. If you extract this zip, you'll get all the technology samples provided with the SDK.
To see the list of samples + documentation about them, visit this page.
Enjoy!
How To: Create Virtual Directory for your Service using Script
When you want to host your WCF Service in IIS, you should follow several steps:
1. Your service should compile to \bin directory instead of \bin\debug
2. You must change the name of the configuration file from app.config to web.config
3. Create a .svc file in a specific format.
and
4. Create a virtual directory in IIS that points to the physical location of you service (probably not a subdirectory of wwwroot..)
I am using 2 scripts for creating and removing virtual directories for my WCF Services. You can find them here. These scripts also create and remove an application for your service.
Usage:
CreateServiceVirtualDir relativePath folderName
RemoveServiceVirtualDir relativePath folderName
Note: You should run these scripts from the directory of your solution.
relativePath is the name of physical directory under the solution folder that your service is located.
folderName is the name you want for your virtual directory.
Enjoy!
After spending a few hours trying to figure out the responsibilities of all the elements of CAB, I found out that I am not the only one who had difficulties...
Szymon Kobalczyk has built a Pet Shop application upon CAB in order to learn the new application block. During that application develpment, he published his conculsion in a series of posts:
Understanding Composite UI Application Block:
Part 1, Part 2, Part 3, Part 4, Part 5, Part 6, Part 7.
Finally you can download the PetShop sample from here, using the installation notes from here.
Enjoy!
The lock keyword can be used to ensure that a block of code runs to completion without interruption by other threads. This is accomplished by obtaining a mutual-exclusion lock for a given object for the duration of the code block.
the lock keyword is implemented with the Monitor class. For example:
lock
(x)
{
DoSomething();
}
Is equivalent to:
object obj = (object)x;
System.Threading.Monitor.Enter(obj);
try
{
DoSomething();
}
finally
{
System.Threading.Monitor.Exit(obj);
}
Using the lock keyword is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock insures that the underlying monitor is released, even if the protected code throws an exception. This is accomplished with the finally keyword, which executes its associated code block regardless of whether an exception is thrown.
Enjoy!
WCF Custom Message Headers
Often do we want to pass some data to some or maybe all our service operations. This data is usually context data such as user tokens, or environmental preferences of the user or machine. In these kind of situations, we would rather not add additional context parameters to the contracts or our services, because we don’t want to involve implementation data / context data with the business parameters of our services.
A nice and easy way to pass that data is to use MessageHeaders. In order to do this we follow these steps:
-
Add the context data to the outgoing message headers.
-
Call the operation (Nothing special here).
-
Extract the data from the incoming message headers.
Using an OperationContextScope
In order to add the message header, you should have a OperationContext for your call. An OperationContext will automaticly be created for the call, and will be available in the service side. If one wishes to add the message headers to the OperationContext before the actual operation call, he should use an OperationContextScope, like the following piece of code shown as Code Block 1.
Notice that the value that is being passed to the constructor of OperationContextScope must implement IContextChannel interface.
Adding the context data to the outgoing message headers (client-side)
In the example shown in Code Block 1, there is a Guid token that had to be passed in the message headers. In order to pass it, I created a typed MessageHeader, and than produced an untyped MessageHeader with a name and namespace.
Code Block 1:
using (ChannelFactory<IMyServiceChannel> factory =
new ChannelFactory<IMyServiceChannel>(new NetTcpBinding()))
{
using (IMyServiceChannel proxy = factory.CreateChannel(...))
{
using ( OperationContextScope scope =
new OperationContextScope(proxy) )
{
Guid myToken = Guid.NewGuid();
MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
proxy.DoOperation(...);
}
}
}
Extract the data from the incoming message headers (service-side)
The following code (Code Block 2) shows how simple it is to extract the typed message header from the incoming headers collection of the OperationContext.
Code Block 2:
Guid myToken =
OperationContext.Current.IncomingMessageHeaders.
GetHeader<Guid>("token", "ns");
You can download the sample project that shows the usage of MessageHeader in the OperationContext from here.
The Samples are all based on the Base Service Solutions that I posted earlier in the Blog.
Enjoy!
We decided to use the Composite UI Application Block for our Smart Client side, so I am happy yo announce that this blog will also include thoughts, examples and conclusions about it.
To briefly introduce CAB:
The Composite UI Application Block is designed to help you build complex Windows Forms-based applications. It assists you by providing an architecture and implementation that uses the common patterns found in front-end, line-of-business (LOB) applications.
Building an application with the Composite UI Application Block provides the following benefits:
- Quality and consistency for architecture teams
- Increased productivity and faster ramp-up time for large developer teams
- Consolidation of operational efforts for operations teams
The application block is designed to support the development of smart client line-of-business applications, such as those used in the following scenarios:
- Online transaction processing (OLTP) front-ends
- Rich client portal scenarios
- UI-intensive, information worker standalone applications (such as those used in call centers)
Relevant links:
Composite UI Application Block Home
Download Composite UI Application Block (C# version)
Download Composite UI Application Block Hands-on Labs (C# version)
Visit Community page of CAB: http://practices.gotdotnet.com/projects/cab.
Enjoy!
Everytime I sit down to write some WCF code to check out a new idea of mine, I find myself spending the first few minutes writing the same code, just to create a basic service to begin with.
I created 2 solutions that will be used as basic sevice templates for me: EchoServiceTemplate and EchoServiceLight.
EchoServiceTemplate seperates the service project from the host project, and seperates the client project from the service agent project (=Client Proxy). Additionaly, this project usese configuratino files to config the bindings, endpoints, behaviors etc.
EchoServiceLight is a smaller solution that doesn't contain configuration files at all.
Here are the solution contents:
EchoServiceTemplate:
|
EchoServiceLight:
|
All my following samples will be based upon theses solutions. I recommend that you use it as well.
You can download the solution from here.
Enjoy!
Resolver must be specified. The default resolver (PNRP) is not available
PeerChannel is part of the Windows Communication Foundation (WCF) framework that enables development of managed Peer-to-Peer applications.
More details about Peer Name Resolution Protocol (PNRP) here.
If you're developing a P2P application using WCF, and you run into the following exception when trying to open the service host:
"System.InvalidOperationException: Resolver must be specified. The default resolver (PNRP) is not available."

Since PNRP service is not enabled by default on Pre-Vista platforms you need to follow the steps below to make sure PNRP is up and running:
1. Go to Start > Run. Type Appwiz.cpl. This opens the ControlPanel - Add Remove Software dialog box. Choose Add Remove Windows Components and then choose Networking > Details. Make sure Peer-to-Peer is checked. Click Install.
2. Then, open up a cmd prompt and type "net start pnrpsvc". This should start up the installed pnrp service for us.
3. Run your project now. This should solve the problem.
Good luck!
WCF configuration schema
Feb CTP installation does not update the WCF Configuration Schema, therefore the Intellisense will not match the changes that were made in this version.
If you want to have Intellisense support to modify the WCF Configuration files in the February CTP, you will have to download that schema and replace the existing schema in C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas.
This is a temporary fix for a bug in the February CTP.
Enjoy!
As a client that wants to communicate with the service, there are several ways to do so:
1. Use ChannelFactory<T> and call the service:
ChannelFactory
<IContract> cf =
new ChannelFactory<IContract>("endpoint");
IContractchannel = cf.CreateChannel();
channel.DoOperation();
2. Using svcutil tool that generates a typed proxy for you.
3. Implement a typed proxy that inherits from ClientBase<T>.
Here is a code sample of a generic proxy. This proxy also inherits from ClientBase<T>, but allows you not to created a specific class for each service. Additionaly I extended the class by overriding the CreateChannel method, where i added the credentials for the call.
///
<summary>
/// Generic Proxy for service calls.
/// </summary>
/// <typeparam name="T">The service Contract</typeparam>
public class GenericProxy<T> : ClientBase<T> where T : class
{
public GenericProxy(string endpoint)
: base(endpoint)
{
}
/// <summary>
/// When creating the channel, set the credentials
/// </summary>
/// <returns></returns>
protected override T CreateChannel()
{
this.ChannelFactory.Credentials.UserName.UserName = "user";
this.ChannelFactory.Credentials.UserName.Password = "123456";
return base.CreateChannel();
}
/// <summary>
/// Returns the transparent proxy for the service call
/// </summary>
public T Proxy
{
get
{
return this.InnerProxy;
}
}
}
Using this class, your client code should look similar to this:
using
( GenericProxy<IContract> service =
new GenericProxy<IContract>("endpoint"))
{
service.Proxy.DoOperation("Guy");
}
Enjoy!
Guy
IDesign chief architect Juval Lowy conducts the WCF Master class as a public event. A unique opportunity for WCF architects and developers...
The seminar dates: 9-13/7/2006.
More details comming soon...
WCF team has posted some new documentation updates, and will keep these releases biweekly.
You can get the latest update from here (~20MB download).
Note that this documentation comes in a set tf .chm files, and do not integrate directly into Visual Studio.
WCF and DataSet? Use maxReceivedMessageSize
Today I tried to figure why the following code stucks:
using
(MyServiceProxy proxy = new MyServiceProxy())
{
using (
OperationContextScope scope =
new OperationContextScope(proxy.InnerChannel))
{
myDataSet = proxy.GetData(filter);
}
}
While debugging the WCF and DataSet sample, the debug never passed the final curly bracket ("}").
Digging more into it, I found out that when the called operation returns an empty DataSet or a small-size DataSet, it all worked fine.
When I placed the break-point just before that final curly bracket, I noticed that the debugger reaches it, but never passes.
After digging some more, I realized that there is a default maximum message size of 65536 bytes. When the filled DataSet exceeded this size limit, the call never ended.
The idea behind this limit is to limit the exposure to denial of service. Using the limit of 65536 bytes, WCF Only allocates this static size of memory for each message. Then, if in incoming message is larger than that size, the message is dropped.
To change the default maxReceivedMessageSize, set that attribute in the binding configuration, as shown here:
<
bindings>
<netTcpBinding>
<binding name="SecureNetNamedPipeBinding"
maxReceivedMessageSize="
2000000"/>
</netTcpBinding>
<bindings>
Enjoy!
Moving from Enterprise Library 1.0 to 2.0, Many of the security application block feature were taken off since ASP.NET 2.0 gave them out of the box. Then, the source code of these providers was no longer available for developers for review, learn and change if needed.
Today, ASP.NET 2.0 providers (including Membership, Role Management, Site Navigation, Session State, Profile, Web Events, and Web Part Personalization) are now available to download as source code.
Enjoy!
Juval lowy has recently uploaded 16 new WCF samples for some interesting concepts in WCF, such as Timer service, Data Contracts versioning and more. Check them out, thier very helpful...
You can find and download the samples here.
More Posts
Next page »