2011-11-26 50 views
0

我正在使用MVVM Light模式創建WPF前端。該前端將調用Web服務從應用程序服務器檢索數據。應用程序服務器是通過.dll公開Web方法的專有Vendor應用程序服務器。將MVVM Light與供應商提供的Webservice一起使用

我需要從服務器獲取客戶端會話才能從服務器獲取結果。問題是,當我調用我的模型,它具有對服務器的連接,我收到以下錯誤:

'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'. 

當我拿出

cashaccount.getConnection(); 

系統繼續併產生行帶有來自WPF Toolkit的數據網格的WPF窗口。

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(); 
     } 
    } 

我略爲簡潔起見。

這裏是模型

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); 
     }   
     { 
     } 
    } 
    } 
} 

我想看看是否服務代理將是一個更好的辦法。如果有人有想法,我將不勝感激。

回答

1

當XAML解析器(通過Resource部分)創建ViewModel並且在創建過程中發生異常時,會發生此異常。您在這裏看到的只是較低異常的結果,即ViewModel創建失敗。

要了解發生了什麼,請在ViewModel構造函數中放置斷點,然後在調試模式下運行您的應用程序。你會發現一些內部代碼崩潰了。

一般來說,我不建議在構建虛擬機時連接到Web服務。這會減慢應用程序的啓動速度,並延遲第一個窗口的顯示。我寧願選擇要創建的虛擬機,將窗口顯示爲「請稍候」消息,然後僅發生Web服務連接,例如由窗口的「加載」事件觸發。

乾杯, 洛朗

0

那麼,在我的具體情況我是註冊類的構造函數是私有的。我有點希望這個例外比陳述更具體,或者至少有正確的內部例外。 C#/ CLR問題我猜。