我已经在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。配置,所以这也不是问题。

有线索吗?


当前回答

对使用服务的非库应用程序进行单元测试可能会导致此问题。

The information that others have entered addresses the root cause of this. If you are trying to write automated test cases and the unit you are testing will actually invoke the service interface, you need to add the service reference to the test project. This is a flavor of the application using library type of error. I did not immediately realize this though because my code that consumes the interface is not in a library. However, when the test actually runs it will be running from the test assembly, not the assembly under test.

向单元测试项目添加服务引用解决了我的问题。

其他回答

我遇到过这样的情况,我

WCF服务托管在某处 主要项目 类型为“类库”的消费者项目,该项目具有对WCF服务的服务引用 主项目从使用者项目调用方法

现在Consumer项目已经在<system中设置了所有相关的配置。我的app.config的serviceModel>标签,它仍然抛出与上面相同的错误。

我所做的只是添加了相同的标签<system。serviceModel>到我的主项目的app.config文件,最后我们就可以开始了。

就我的情况而言,真正的问题是读取了错误的配置文件。而不是消费者的app.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 ...
            }

详见本文。

对使用服务的非库应用程序进行单元测试可能会导致此问题。

The information that others have entered addresses the root cause of this. If you are trying to write automated test cases and the unit you are testing will actually invoke the service interface, you need to add the service reference to the test project. This is a flavor of the application using library type of error. I did not immediately realize this though because my code that consumes the interface is not in a library. However, when the test actually runs it will be running from the test assembly, not the assembly under test.

向单元测试项目添加服务引用解决了我的问题。

配置中的名称空间应该反映客户端默认名称空间(如在项目属性中配置的那样)之后的名称空间路径的其余部分。根据你发布的答案,我猜你的客户端被配置为“Fusion.DataExchange”。工作流”名称空间。如果将客户端代码移动到另一个名称空间,则需要更新配置以匹配剩余的名称空间路径。

当你在类文件中引用服务时,这里有几个响应可以找到正确的解决方案:将服务配置信息复制到app.config web中。这些答案似乎都没有告诉你应该复制什么。让我们试着纠正一下。

下面是我从我的类库的配置文件复制到我的控制台应用程序的配置文件中,为了解决这个疯狂的错误,我写了一个名为“TranslationServiceOutbound”的服务。

你基本上想要系统内的一切。serviceModel部分:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ITranslationServiceOutbound" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://MyHostName/TranslationServiceOutbound/TranslationServiceOutbound.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITranslationServiceOutbound"
    contract="TranslationService.ITranslationServiceOutbound" name="BasicHttpBinding_ITranslationServiceOutbound" />
</client>