Reputation: 569
I am using the MVVM Light pattern to create a WPF front end. This front end will call a webservice to retrieve data from an application server. The application server is a proprietary Vendor app server that exposes web methods via .dll.
I need to get a client session from the server in order to get results from the server. The problem is that when I invoke my model, which has the connection to the server, I get the following error:
'The invocation of the constructor on type [APP].ViewModel.ViewModelLocator' that matches the specified binding constraints threw an exception.' Line number '12' and line position '10'.
When I take out the line
cashaccount.getConnection();
The system continues and produces a WPF window with the Data Grid from WPF Toolkit.
MainWindow.xaml
<Window x:Class="CreditSuisse.MainWindow"
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"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
Height="301"
Width="520"
Title="MVVM Light Application"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<dg:DataGrid ItemsSource="{Binding Path=CashAccount}"
Margin="5" AutoGenerateColumns="False">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding AcctCd}" Header="Account Code" />
<dg:DataGridTextColumn Binding="{Binding AcctCd}" Header="Security Name" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Quantity Start of Day" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Cash Delta (Price Delta)" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Action" />
</dg:DataGrid.Columns>
</dg:DataGrid>
</Window>
ViewModel.cs
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
logger.Info("----- Start -----");
cashaccount.getConnection();
}
}
I've abbreviated for the sake of brevity.
Here is the model
CashAccount.cs
public class CashAccount : xx.xx.Position
{
private bool cashChanged=false;
private string secName = null;
private xxx.Wsi.ClientSession privateSession;
private static Logger logger = LogManager.GetCurrentClassLogger();
public bool didCashChange
{
get
{
return cashChanged;
}
set
{
this.cashChanged = value;
}
}
public void getConnection()
{
try
{
app.Helper.Session session = new Session();
privateSession = session.getSession();
}
catch (TransportException e)
{
Console.WriteLine("Error communicating with server: " + e.Message);
logger.Info("Couldn't log into xxx...");
logger.Error(e.Message);
}
{
}
}
}
}
I am looking to see if a Service Agent would be a better approach. If anyone has ideas I would appreciate it.
Upvotes: 0
Views: 1008
Reputation: 1945
Well, in my specific case the constructor of the class I was registering was private. I kinda wish the exception was more specific than stated or at least had the correct inner exception. C#/CLR issue I guess.
Upvotes: 0
Reputation: 6662
This exception occurs when the ViewModel is created by the XAML parser (through a Resource section), and an exception occurs during the creation. What you see here is only the result of that lower exception, i.e. the ViewModel creation failed.
To find out what happens, place a breakpoint in the ViewModel constructor, and run your app in debug mode. You will find out that some inner code is crashing.
Generally speaking, I wouldn't recommend connecting to web services during the construction of the VM. This will slow down the start of your application, and delay the appearance of the first window. I would rather prefer the VM to be created, the window to be shown with a "please wait" message, and then only a connection to web services to occur, for example triggered by the "Loaded" event of the Window.
Cheers, Laurent
Upvotes: 1