May 29 2011

I unlocked Level 2 on Empire A…

I unlocked Level 2 on Empire Avenue, where I am for sale on the Social Media Exchange! http://t.co/CKpcLZb

May 29 2011

I just unlocked the ‘Connect a…

I just unlocked the ‘Connect a Network’ Achievement on EmpireAvenue.com http://t.co/CKpcLZb

May 22 2011

Comment by Seth Spearman on Can I listen to a WCF event from a web client?

Rowan,I have this exact question. Did you ever figure out how to do this using WCF. If so do you think you could answer your own question?
May 20 2011

How do you get a WCF proxy to ctor a child collection?

I am studying today and I did my very first very simple WCF Service. I created a couple of very simple classes as follows (this is simplified a bit) ...

//contact class  
public class Contact
{
    public int Id { get; set; }


    private ObservableCollection<Phone> _contactPhones = new ObservableCollection<Phone>();
    public ObservableCollection<Phone> ContactPhones
    {
        get { return _contactPhones; }
        set { _contactPhones = value; }
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}
// phone class  
public class Phone
{
    public string PhoneNumber { get; set; }
    public PhoneTypes PhoneType { get; set; }
}

I have a mock repository class that returns a collection of the contact class

class ContactRepositoryMock : IContactRepository
{
    private readonly ObservableCollection<Contact> _contactList;

    public ContactRepositoryMock()
    {
        _contactList = new ObservableCollection<Contact>();

        Contact contact = this.Create();
        contact.Id = 1;
        contact.FirstName = "Seth";
        contact.LastName = "Spearman";
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-1111",PhoneType = PhoneTypes.Mobile});
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-2222",PhoneType = PhoneTypes.Home});

        this.Save(contact);

    }
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactList;
    }

}

The Save and Create methods are not shown but Save adds to the _contactList collection and Create creates a new instance of contact (notice that the Contact ctor is using eager loading to init the phone _contactPhones collection)

Finally I created a WCF Service wrapper around the ContactRepositoryMock.GetContacts method as follows...

[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactsService
{
    private ContactRepositoryMock _contactRepository = new ContactRepositoryMock();

    [OperationContract]
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactRepository.GetContacts();
    }
}

The other project is a Silverlight project (which is really what I am s'posed to be studying today.)

In that project I added a Web Reference to my WCF Class and Visual Studio added the proxy class as usual.

I have added a MainPageViewModel in the project as follows:

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel()
    {
        if (!IsDesignTime)
        {
            //GetContacts();   //not shown               
        }
        else
        {
            var contactList = new ObservableCollection<Contact>();

            var contact = new Contact {Id = 1, FirstName = "Seth", LastName = "Spearman"};
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-1111", PhoneType = PhoneTypes.Mobile });
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-2222", PhoneType = PhoneTypes.Home }); 
            contactList.Add(contact);

            Contacts= contactList;
        }
    }

    private ObservableCollection<Contact> _contacts;
    public ObservableCollection<Contact> Contacts
    {
        get { return _contacts; }
        set
        {
            if (value!=_contacts)
            {
                _contacts = value;
                OnPropertyChanged("Contacts");
            }
        }
    }
}

AND the following XAML

<UserControl x:Class="MVVMDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:viewModels="clr-namespace:MVVMDemo.ViewModels"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <viewModels:MainPageViewModel x:Key="ViewModels" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" 
          DataContext="{Binding Source={StaticResource ViewModels}}"
          Background="White">
    </Grid>
</UserControl>

That is a lot of background to get to the error and ultimately what is probably a simple explanation.

The XAML line <viewModels:MainPageViewModel x:Key="ViewModels" /> has a squiggly under it and is returning the error "Cannot create and instance of MainPageViewModel".

I even know the CAUSE of that error. If I disable the contact.ContactPhones.Add ... lines that are in the MainPageViewModel ctor then the error goes away.

Finally, I even know why the error goes away. It is because the Reference file that creates the WCF Proxy class is not initializing the ContactPhones collection.

In other words, in the generated class Reference.cs in the proxy if I change the line that reads...

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField;

to

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField = new ObservableCollection<Phone>();

then I can re-enable the contact.ContactPhones.Add... lines and the error goes away. The project compiles and runs.

SO...all that to simply ask...how do I get Visual Studio to generate a proxy class that will init my collection. Or is there a flaw in the way I am doing this? What am I missing?

Sorry for all the detail but I was not sure where in the call chain there might be a failure. I am also going to use all of this detail to ask a few more questions after this one gets answered.

Seth

May 20 2011

How do you get a WCF proxy to ctor a child collection?

Hello,

I am studying today and I did my very first very simple WCF Service. I created a couple of very simple classes as follows (this is simplified a bit) ...

//contact class  
public class Contact
{
    public int Id { get; set; }


    private ObservableCollection<Phone> _contactPhones = new ObservableCollection<Phone>();
    public ObservableCollection<Phone> ContactPhones
    {
        get { return _contactPhones; }
        set { _contactPhones = value; }
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}
// phone class  
public class Phone
{
    public string PhoneNumber { get; set; }
    public PhoneTypes PhoneType { get; set; }
}

I have a mock repository class that returns a collection of the contact class

class ContactRepositoryMock : IContactRepository
{
    private readonly ObservableCollection<Contact> _contactList;

    public ContactRepositoryMock()
    {
        _contactList = new ObservableCollection<Contact>();

        Contact contact = this.Create();
        contact.Id = 1;
        contact.FirstName = "Seth";
        contact.LastName = "Spearman";
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-1111",PhoneType = PhoneTypes.Mobile});
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-2222",PhoneType = PhoneTypes.Home});

        this.Save(contact);

    }
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactList;
    }

}

The Save and Create methods are not shown but Save adds to the _contactList collection and Create creates a new instance of contact (notice that the Contact ctor is using eager loading to init the phone _contactPhones collection)

Finally I created a WCF Service wrapper around the ContactRepositoryMock.GetContacts method as follows...

[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactsService
{
    private ContactRepositoryMock _contactRepository = new ContactRepositoryMock();

    [OperationContract]
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactRepository.GetContacts();
    }
}

The other project is a Silverlight project (which is really what I am s'posed to be studying today.)

In that project I added a Web Reference to my WCF Class and Visual Studio added the proxy class as usual.

I have added a MainPageViewModel in the project as follows:

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel()
    {
        if (!IsDesignTime)
        {
            //GetContacts();   //not shown               
        }
        else
        {
            var contactList = new ObservableCollection<Contact>();

            var contact = new Contact {Id = 1, FirstName = "Seth", LastName = "Spearman"};
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-1111", PhoneType = PhoneTypes.Mobile });
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-2222", PhoneType = PhoneTypes.Home }); 
            contactList.Add(contact);

            Contacts= contactList;
        }
    }

    private ObservableCollection<Contact> _contacts;
    public ObservableCollection<Contact> Contacts
    {
        get { return _contacts; }
        set
        {
            if (value!=_contacts)
            {
                _contacts = value;
                OnPropertyChanged("Contacts");
            }
        }
    }
}

AND the following XAML

<UserControl x:Class="MVVMDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:viewModels="clr-namespace:MVVMDemo.ViewModels"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <viewModels:MainPageViewModel x:Key="ViewModels" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" 
          DataContext="{Binding Source={StaticResource ViewModels}}"
          Background="White">
    </Grid>
</UserControl>

That is a lot of background to get to the error and ultimately what is probably a simple explanation.

The XAML line <viewModels:MainPageViewModel x:Key="ViewModels" /> has a squiggly under it and is returning the error "Cannot create and instance of MainPageViewModel".

I even know the CAUSE of that error. If I disable the contact.ContactPhones.Add ... lines that are in the MainPageViewModel ctor then the error goes away.

Finally, I even know why the error goes away. It is because the Reference file that creates the WCF Proxy class is not initializing the ContactPhones collection.

In other words, in the generated class Reference.cs in the proxy if I change the line that reads...

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField;

to

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField = new ObservableCollection<Phone>();

then I can re-enable the contact.ContactPhones.Add... lines and the error goes away. The project compiles and runs.

SO...all that to simply ask...how do I get Visual Studio to generate a proxy class that will init my collection. Or is there a flaw in the way I am doing this? What am I missing?

Sorry for all the detail but I was not sure where in the call chain there might be a failure. I am also going to use all of this detail to ask a few more questions after this one gets answered.

Seth

May 20 2011

How do you get a WCF proxy to ctor a child collection?

Hello,

I am studying today and I did my very first very simple WCF Service. I created a couple of very simple classes as follows (this is simplified a bit) ...

//contact class  
public class Contact
{
    public int Id { get; set; }


    private ObservableCollection<Phone> _contactPhones = new ObservableCollection<Phone>();
    public ObservableCollection<Phone> ContactPhones
    {
        get { return _contactPhones; }
        set { _contactPhones = value; }
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}
// phone class  
public class Phone
{
    public string PhoneNumber { get; set; }
    public PhoneTypes PhoneType { get; set; }
}

I have a mock repository class that returns a collection of the contact class

class ContactRepositoryMock : IContactRepository
{
    private readonly ObservableCollection<Contact> _contactList;

    public ContactRepositoryMock()
    {
        _contactList = new ObservableCollection<Contact>();

        Contact contact = this.Create();
        contact.Id = 1;
        contact.FirstName = "Seth";
        contact.LastName = "Spearman";
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-1111",PhoneType = PhoneTypes.Mobile});
        contact.ContactPhones.Add(new Phone(){PhoneNumber = "864-555-2222",PhoneType = PhoneTypes.Home});

        this.Save(contact);

    }
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactList;
    }

}

The Save and Create methods are not shown but Save adds to the _contactList collection and Create creates a new instance of contact (notice that the Contact ctor is using eager loading to init the phone _contactPhones collection)

Finally I created a WCF Service wrapper around the ContactRepositoryMock.GetContacts method as follows...

[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactsService
{
    private ContactRepositoryMock _contactRepository = new ContactRepositoryMock();

    [OperationContract]
    public ObservableCollection<Contact> GetContacts()
    {
        return _contactRepository.GetContacts();
    }
}

The other project is a Silverlight project (which is really what I am s'posed to be studying today.)

In that project I added a Web Reference to my WCF Class and Visual Studio added the proxy class as usual.

I have added a MainPageViewModel in the project as follows:

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel()
    {
        if (!IsDesignTime)
        {
            //GetContacts();   //not shown               
        }
        else
        {
            var contactList = new ObservableCollection<Contact>();

            var contact = new Contact {Id = 1, FirstName = "Seth", LastName = "Spearman"};
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-1111", PhoneType = PhoneTypes.Mobile });
            contact.ContactPhones.Add(new Phone() { PhoneNumber = "864-555-2222", PhoneType = PhoneTypes.Home }); 
            contactList.Add(contact);

            Contacts= contactList;
        }
    }

    private ObservableCollection<Contact> _contacts;
    public ObservableCollection<Contact> Contacts
    {
        get { return _contacts; }
        set
        {
            if (value!=_contacts)
            {
                _contacts = value;
                OnPropertyChanged("Contacts");
            }
        }
    }
}

AND the following XAML

<UserControl x:Class="MVVMDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:viewModels="clr-namespace:MVVMDemo.ViewModels"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <viewModels:MainPageViewModel x:Key="ViewModels" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" 
          DataContext="{Binding Source={StaticResource ViewModels}}"
          Background="White">
    </Grid>
</UserControl>

That is a lot of background to get to the error and ultimately what is probably a simple explanation.

The XAML line <viewModels:MainPageViewModel x:Key="ViewModels" /> has a squiggly under it and is returning the error "Cannot create and instance of MainPageViewModel".

I even know the CAUSE of that error. If I disable the contact.ContactPhones.Add ... lines that are in the MainPageViewModel ctor then the error goes away.

Finally, I even know why the error goes away. It is because the Reference file that creates the WCF Proxy class is not initializing the ContactPhones collection.

In other words, in the generated class Reference.cs in the proxy if I change the line that reads...

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField;

to

private System.Collections.ObjectModel.ObservableCollection<MVVMDemo.WSProxy.Phone> ContactPhonesField = new ObservableCollection<Phone>();

then I can re-enable the contact.ContactPhones.Add... lines and the error goes away. The project compiles and runs.

SO...all that to simply ask...how do I get Visual Studio to generate a proxy class that will init my collection. Or is there a flaw in the way I am doing this? What am I missing?

Sorry for all the detail but I was not sure where in the call chain there might be a failure. I am also going to use all of this detail to ask a few more questions after this one gets answered.

Seth