fr en

Use Visual Studio debugger in client side partial classes created on WCF proxy

2012-04-24 2 min read .NET Visual Studio 2010 Aymeric

In application using WCF service, it is not uncommon to create partial classes on client side in order to add properties on objects retrieved by the service. Theses properties are needed by the client application (for interface for example) and they have no reason to present on server side.

For instance, a class name Product has following properties defined on server side : Price, Name, Stock and has the property IsVisible defined on client side in order to display or not the product on UI. Methods can also be defined in these partial classes.

Example :

[DataContract]
public class MyClass
{
    [DataMember]
    public string Label { get; set; }
}

Partial_MyClass.cs (client side) :

public partial class MyClass
{
    public bool IsVisible { get; set; }

    public void Display()
    {
        IsVisible = true;
    }
}

In the Visual Studio 2010 default configuration (Just My Code enabled), breakpoints will not be hit in peviously created partial classes. When disabling “Just My Code” (Debug -> Options -> Enable Just My Code), breakpoints will be hit but StepOver (F10) and StepInto (F11) will not work.

This behavior is caused by DebuggerStepTroughAttribute attribute present in Reference.cs file in order to avoid Visual Studio debugging tools to go into this file (WCF proxy generated by Visual Studio or svcutil/slsvcutil).

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(
    "System.Runtime.Serialization", 
    "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(
    Name = "MyClass", 
    Namespace = "http://schemas.datacontract.org/2004/07/MyApp.Client.Web")]
public partial class MyClass : 
    object, System.ComponentModel.INotifyPropertyChanged
{

    private string LabelField;
    
    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Label
    {
        get
        {
            return this.LabelField;
        }
        set
        {
            if ((object.ReferenceEquals(this.LabelField, value) != true))
            {
                this.LabelField = value;
                this.RaisePropertyChanged("Label");
            }
        }
    }
    
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    
    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = 
            this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, 
                new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

To easily debug, edit Reference.cs file (generated by Visual Studio) and remove[System.Diagnostics.DebuggerStepThroughAttribute()] attribute from classes you want to debug.

Reference

Note : Ask Visual Studio to “Show all files” in solution explorer and don’t forget to repeat this operation when updating your service reference.

Thanks Gaël Covain for his help.