Tuesday, January 19, 2010

WCF errors: No set method for property on Type 'xx'

I was surprised when i first saw this error. This usually happens when you try to publish DataContract on WCF service containing Read only properties. I don't know why exactly this error occurs? As many people are concerned, workaround for this problem looks very obvious.

Add Setter to the property in question.  But, this move will defeat your original purpose of Read only property.

Huh.., Ok, here's solution. just add Private keyword to your setter method. WCF allows this provision. So, client can never see the set method on your property acting much like a read only property.

WCF errors: WCF service has zero application (non-infrastructure) endpoints.This might be because no configuration file was found on your application, or because no service element matching the service name is found in the configuration file, or because no endpoints were defined in the service element.

This WCF error always pops on my computer when ever i'm creating brand new WCF service.  Bluntly, we can understand from error that we haven't configured the service properly. Of course, there can be multiple causes. Handy tool which can help during this crisis :-) is "WCF Service Configuration Editor". You can open WCF Service Configuration Editor (SvcConfigEditor.exe) by right clicking on configuration file in your WCF service library project or accessing it under Tools menu in the Visual Studio IDE.


On left pane in editor, you'll see various service elements already defined.  Click on your Service Endpoint node.  90% of time root cause of this error will be hanging around this Service Endpoint definition. 


Remember, all the WCF service  configuration definition is found under  element


Here's the checklist that should help resolve this error.

  • Confirm your Contract attribute of Endpoint element is pointing to appropriate Service contract instead of "IService1" unless you are still using "IService1" as service contract.  
  • Confirm your Name attribute of Service Element is set appropriately to Service class implementation instead of "Service1" unless you're still using "Service1" as service implementation.
  • Also, Examine ServiceHost directive declaration in service1.svc markup.  Clean up any scrap you see and provide fully qualified namespace for the service class for service attribute.

 I strongly recommend you use Service Configuration Editor for any kind of poking around web.config/app.config related to WCF service. This way, you'll keep service configuration neat and clean. especially with complex configuration files, this tool will come handy in organizing the service elements easily.

WCF errors: Type 'xxx' is an invalid collection type since it has DataContractAttribute attribute.

If you have ever happen to get "Type xxx is an invalid collection type since it has DataContract Attribute attribute" error at any time when ever publishing WCF service, Make sure you annotate your custom collections with CollectionDataContract attribute instead of DataContract attribute. This attribute will appropriate serialize and deserialize collection.


CollectionDataContract attribute is typically utilized in scenarios involving non-WCF providers/clients to extract exact shape of data. But, you loose one important benefit with "CollectionDataContract" is "Cross Collection Interchangeability". For instance, in case of homogeneous environment of WCF client and service though WCF service exposes List collections as response, client expecting different native collection type like ArrayList/Collection can still consume the collection.


You loose this benefit if you use CollectionDataContract attribute with Collection Types.

WCF also imposes additional requirements when using collections. Keep in mind of following items when exposing custom collections via WCF service or else custom collections are not serialized/deserialized.


  • Collection should support Add method.
  • Collection should have default constructor.



Tuesday, January 5, 2010

Unity Framework v1.2 - IoC Dependency Injection

Following is the power point presentation related to IoC and Dependency Injection (DI) pattern. This presentation specifically focuses on Unity Framework v1.2 based IoC container. Integration WCF service with IoC container provides finer control over WCF instance management.


Friday, December 18, 2009

WCF Service Trace Viewer (SvcTraceViewer.exe) tool

image

If you're familiar with WCF application development, some times, you might across this kind of exception thrown back to client after unsuccessful service operation. Above exception in the screen shot doesn't provide any detailed information regarding the source of exception or any stack trace.

Windows Communication Foundation (WCF) Service Trace Viewer is a useful utility to help analyze WCF messages that are exchanged between service and client. Any inner exception buried deep in the stack trace can be traced, analyzed and diagnosed. Some times, due to security concerns, WCF services doesn't emit complete exception details back to the client. Lot of times, cryptic, brief and vague exceptions thrown by WCF service leaves no clue about the error. For example, lot of times you find "Connection closed abruptly/MessageSecurity exception/ Security token" exception being thrown back to WCF client.

Even if you turn on IncludeExceptionDetailInFaults service behavior element to True, Some times, you don't get the complete picture you expect. In such scenarios, Service Trace viewer tool is really helpful increasing productivity and saving time and takes us to the right spot of error occurence. We can utilize Service Trace Viewer Tool (SvcTraceViewer.exe) for WCF service level tracing and instrumentation.
We can configure tracing using either app.config file or web.config file. We can easily enable tracing/message logging using Service configuration Editor tool which can be accessed from Visual Studio IDE (Tools section of MainMenu).

Service Trace Viewer tool generates tracing file (.svcLog). This tool also supports two other file types
(Event Tracing file(.etl) and Crimson Tracing file

WCF service generates .svcLog files.


After enabling tracing/message logging in config files using WCF service configuration editor, exchange couple of messages between WCF service and client for recording trace. You can enable tracing/logging both at the client and service side in their respective config files.

image

image

To open a trace file, navigate to your WCF installation location (C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin), then you can find SvcTraceViewer.exe in bin folder or you can also access it from start menu as shown below.

image

After you open the service trace viewer tool, Now try to open your .svcLog file from the location you specified in the config file.

Service Trace Viewer supports four different views of trace data.
  • Activity view 
  • Project view 
  • Message view 
  • Graph view.
Below screenshot display default activity view from trace viewer. In Activity view, all the related traces are grouped into activities. The activity view displays activity names, number of traces and duration.

To view a trace detail, select a trace in the trace pane. The details are displayed in the Detail pane.
Detail pane which is located in bottom left provides three tabs to view trace details. Formatted view displays the information in a more organized way. You can see formatted view in the screenshot. XML View displays XML related to the trace. The Message view displays the message part of the XML in message log traces. It is invisible when you select a non-message trace.