WCF服务:在MVVM项目中找不到dafault端点

问题描述 投票:0回答:1

我有一个解决方案,其中的MVVM项目引用了WCF Web服务项目,而WPF项目是GUI的一部分。

当我在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;

    }
}

WMV窗口中如何包含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向导:wizard

请注意,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>

你知道出什么问题了吗?

c# wpf web-services mvvm
1个回答
0
投票

您应该将配置添加到正在运行的exectable的App.config文件中,即WPF应用程序项目中的配置。

未使用引用的类库项目中的App.config文件。

© www.soinside.com 2019 - 2024. All rights reserved.