WPF用户控制库中的WCF服务参考

本文关键字:WCF 服务 参考 用户 控制 WPF | 更新日期: 2023-09-27 18:14:55

我有一个简单的WCF web服务,如果我创建一个新的简单的c# WPF应用程序,添加WCF服务引用,它会工作得很好。然后我可以从WCF服务访问方法/操作契约。

,

Service1Client svc = new Service1Client();
        svc.GetData()

如果我创建一个WPF UserControl,在其中添加WCF服务引用并实例化:Service1Client svc = new Service1Client();-一切都编译好了(不能调试,因为它只是一个UserControl)。

但是如果我从UserControl导入。dll到一个新的c# WPF应用程序中,并在主窗口中使用UserControl。Xaml失败:

类型为'System '的异常。在System.ServiceModel.dll中发生了InvalidOperationException,但没有在用户代码中处理附加信息:找不到引用契约"ServiceReference1"的默认端点元素。在ServiceModel客户端配置部分中的IService1'。这可能是因为没有找到您的应用程序的配置文件,或者因为在客户端元素中没有找到匹配此契约的端点元素。

这是怎么回事?为什么我不能在另一个WPF主应用程序中使用的WPF用户控件中使用WCF服务引用?

我已经搜索了又搜索,但没有任何有用的发现。

问好

编辑# 1

我添加了系统。ServiceModel到我的MainApplication(谢谢你指出这一点)我得到同样的错误

类型为'System '的异常。在System.ServiceModel.dll中发生了InvalidOperationException,但没有在用户代码中处理附加信息:找不到引用契约"ServiceReference1"的默认端点元素。在ServiceModel客户端配置部分中的IService1'。这可能是因为没有找到您的应用程序的配置文件,或者因为在客户端元素中没有找到匹配此契约的端点元素。

我猜问题是MainApplication不知道端点,从app.config从UserControl库。

我必须将这部分从app.config (UserControl库)复制到MainApplication app.config它会编译和启动,它工作。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="DELETED" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

但我仍然得到错误在Visual Studio I live design view在MainWindow.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfControlLibrary1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1" x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <WpfControlLibrary1:UserControl1/>
</Grid>

我得到错误,不能在实时视图中看到UserControl(不能创建"UserControl1"的实例)

从错误列表中:

Error   1   Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.    DELETED'WpfApplication1'WpfApplication1'MainWindow.xaml 7   9   WpfApplication1

WPF用户控制库中的WCF服务参考

根据您得到的错误,您需要引用System。

从主WPF应用程序中的ServiceModel命名空间。

:

对于第二个错误,这可能是由于VS设计器。

有趣,svcutil.exe的输出是

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://REMOED/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

来自UserControl的App.config是:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://REMOVED/Service1.svc" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
      name="BasicHttpBinding_IService1" />
</client>

注意"contract"

但如果我复制/粘贴配置从svcutil我得到错误VS.相同的我的第一次。因此,正确的契约应该是contract="ServiceReference1 "。"IService1"-但有趣的是,svcutil输出不同的东西?

明白了!!

用svcutil的输出更新了USERCONTROL中的app.config,并重新编译。现在设计视图工作了:)