我有一个解决方案,其中包含引用WCF Web服务项目的MVVM项目和作为GUI部分的WPF项目。
当我在WPF窗口XAML中添加对MVVM主类的引用时,出现一个错误:在客户端ServiceModel的配置部分中找不到引用合同“ MarketService.IService1”的dafault终结点。由于找不到配置文件,或者在客户端元素中找不到与该端点相对应的端点。
调用Web服务时,MVVM项目中出现错误:
public class MainWindowMVVM
{
private IList<Magasin> _Magasins = new ObservableCollection<Magasin>();
public IList<Magasin> Magasins {
set
{
_Magasins = value;
}
get
{
return _Magasins;
}
}
public MainWindowMVVM()
{
//au démarrage, magasins est remplie grâce à un appel au service
using (var market = new MarketService.Service1Client()) <---- here is the error
{
Supermarche retour = market.GetSupermarche();
//copier retour.magasins dans magasins
foreach (Magasin mg in retour.Magasins)
{
Magasins.Add(mg);
}
}
//trvMarket.ItemsSource = Magasins;
}
}
以下是WPF窗口中如何包含MVVM类的信息:
<Window x:Class="UserInterface.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:local="clr-namespace:UserInterface"
mc:Ignorable="d"
Title="Market Client" Height="450" Width="800"
xmlns:self="clr-namespace:Model;assembly=Model"
xmlns:mvvmPart="clr-namespace:MVVM;assembly=MVVM" <-- here I include the project
>
<Window.Resources>
<local:NotConverter x:Key="NotConverter" />
<Style x:Key="TreeViewItemStyle_ExpandAll" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</Window.Resources>
<Window.DataContext>
<mvvmPart:MainWindowMVVM/> <--here I want to make a default access to the MVVM class
...
在最后一个箭头中,Visual Studio显示与上述相同的错误(关于默认端点)。
要将Web服务添加到MVVM项目中,我使用Visual Studio向导:
请注意,WPF项目不包含对Web服务的任何引用:我认为没有必要。
这是MVVM项目中的配置文件app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/market11/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="MarketService.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
您知道出了什么问题吗?
答案 0 :(得分:0)
您应该将配置添加到正在运行的exectable的App.config
文件中,即WPF应用程序项目中的配置。
未使用引用的类库项目中的App.config
文件。
您还可以通过编程方式创建绑定:
var binding = new System.ServiceModel.BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost/market11/Service1.svc");
var factory = new ChannelFactory<IService1>(binding, endpoint);
var channel = factory.CreateChannel();
var retour = channel.GetSupermarche();