我已经在VS2008/中添加了web服务的代理。NET 3.5解决方案。在构造客户端。net时会抛出以下错误:

在ServiceModel客户端配置部分中找不到引用合约“IMySOAPWebService”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中没有找到与此契约匹配的端点元素。

搜索此错误告诉我在契约中使用完整的名称空间。这是我的app.config完整的命名空间:

<client>
  <endpoint address="http://192.168.100.87:7001/soap/IMySOAPWebService"
            binding="basicHttpBinding" bindingConfiguration="IMySOAPWebServicebinding"
            contract="Fusion.DataExchange.Workflows.IMySOAPWebService" name="IMySOAPWebServicePort" />
</client>

我正在运行XP本地(我提到这是因为一些谷歌点击提到win2k3) 将app.config复制到app.exe。配置,所以这也不是问题。

有线索吗?


当前回答

我遇到了同样的问题,只有当主机应用程序和使用该端点的dll具有相同的服务引用名称时,才解决了这个问题。

其他回答

在我的案例中,我已经将app.config重命名为[appname].exe。手动配置。这最终在文件名中添加了一个额外的.config后缀。解决方案是将其重命名为[appname].exe,以消除额外的.config扩展名。

“如果在类库中调用服务,并从另一个项目调用类库,则可能出现此错误。” 在这种情况下,如果是winapp或web,则需要将WS配置设置包含到主项目app.config中。如果它是一个web应用程序,就配置它。这是即使使用PRISM和WPF/Silverlight的方式。”

是的,但是如果你不能改变主项目(例如Orchard CMS),你可以在你的项目中保留WCF服务配置。

你需要用客户端生成方法创建一个服务助手:

public static class ServiceClientHelper
{
    public static T GetClient<T>(string moduleName) where T : IClientChannel
    {
        var channelType = typeof(T);
        var contractType = channelType.GetInterfaces().First(i => i.Namespace == channelType.Namespace);
        var contractAttribute = contractType.GetCustomAttributes(typeof(ServiceContractAttribute), false).First() as ServiceContractAttribute;

        if (contractAttribute == null)
            throw new Exception("contractAttribute not configured");

        //path to your lib app.config (mark as "Copy Always" in properties)
        var configPath = HostingEnvironment.MapPath(String.Format("~/Modules/{0}/bin/{0}.dll.config", moduleName)); 

        var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = configPath }, ConfigurationUserLevel.None);
        var serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);

        if (serviceModelSectionGroup == null)
            throw new Exception("serviceModelSectionGroup not configured");

        var endpoint = serviceModelSectionGroup.Client.Endpoints.OfType<ChannelEndpointElement>().First(e => e.Contract == contractAttribute.ConfigurationName);
        var channelFactory = new ConfigurationChannelFactory<T>(endpoint.Name, configuration, null);
        var client = channelFactory.CreateChannel();
        return client;
    }
}

并使用它:

using (var client = ServiceClientHelper.GetClient<IDefaultNameServiceChannel>(yourLibName)) {
                ... get data from service ...
            }

详见本文。

如果你使用WPF应用程序使用PRISM框架,那么配置应该存在于你的启动项目中(即在你的bootstrapper驻留的项目中)。

我在ASP中得到这个错误。其中WCF服务已添加到正在添加到asp.net应用程序中的类库中。. NET应用程序作为bin文件夹中引用的.dll文件。为了解决这个错误,需要将类库中引用WCF服务的app.config文件中的配置设置复制到web中。配置设置的ASP。网网站/应用程序。

我遇到了同样的问题,但是更改契约名称空间对我不起作用。所以我尝试了。net 2风格的web引用,而不是。net 3.5的服务引用。这工作。

要在Visual Studio 2008中使用Web引用,请单击“添加服务引用”,然后在出现对话框时单击“高级”。您将发现一个选项,该选项允许您使用Web引用而不是服务引用。