将 MVVM Light 与供应商的 Web 服务结合使用
我正在使用 MVVM Light 模式创建 WPF 前端。该前端将调用 Web 服务从应用程序服务器检索数据。应用程序服务器是专有的供应商应用程序服务器,通过 .dll 公开 Web 方法。
我需要从服务器获取客户端会话才能从服务器获取结果。问题是,当我调用连接到服务器的模型时,出现以下错误:
'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);
}
{
}
}
}
}
我想看看服务代理是否是更好的方法。如果有人有想法,我将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 XAML 解析器(通过 Resource 部分)创建 ViewModel 时,会发生此异常,并且在创建过程中发生异常。您在这里看到的只是较低异常的结果,即 ViewModel 创建失败。
要了解发生了什么,请在 ViewModel 构造函数中放置一个断点,然后在调试模式下运行您的应用程序。你会发现一些内部代码崩溃了。
一般来说,我不建议在虚拟机构建过程中连接到Web服务。这会减慢应用程序的启动速度,并延迟第一个窗口的出现。我宁愿创建虚拟机,显示带有“请等待”消息的窗口,然后仅发生与 Web 服务的连接,例如由窗口的“已加载”事件触发。
干杯,
洛朗
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
好吧,在我的具体情况下,我注册的类的构造函数是私有的。我有点希望异常比所述的更具体,或者至少有正确的内部异常。我猜是 C#/CLR 问题。
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.