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

有线索吗?


当前回答

我也有同样的问题。事实证明,对于web REFERENCE,你必须将URL作为构造函数的第一个参数:

new WebService.WebServiceSoapClient("http://myservice.com/moo.aspx");

对于一个新样式的web SERVICE REFERENCE,你必须在配置中提供一个引用端点条目的名称:

new WebService.WebServiceSoapClient("WebServiceEndpoint");

在Web中使用相应的条目。config或App.config:

<client>
      <endpoint address="http://myservice.com/moo.aspx"
        binding="basicHttpBinding" 
        bindingConfiguration="WebService"
        contract="WebService.WebServiceSoap"
        name="WebServiceEndpoint" />
    </client>
  </system.serviceModel>

很难消除“它在旧的程序中工作”的狭隘观点……

其他回答

这让我抓狂。

我使用的是带有WCF的Silverlight 3 Prism (CAB)

当我在Prism模块中调用WCF服务时,我得到了相同的错误:

找不到引用合同的默认端点元素 服务模型客户端配置部分中的'IMyService'。这 可能是因为没有找到应用程序的配置文件 或者因为找不到与此契约匹配的端点元素 在client元素中

原来它在Shell的.xap文件中寻找ServiceReferences。ClientConfig文件,而不是模块的ServiceReferences中。ClientConfig文件。我将端点和绑定添加到现有的servicereference中。我的Silverlight Shell应用程序中的ClientConfig文件(它调用它自己的WCF服务)。

然后,我必须重新构建Shell应用程序,为我的Web项目的ClientBin文件夹生成新的.xap文件。

现在这行代码终于可以工作了:

MyServiceClient myService = new MyServiceClient();

“如果在类库中调用服务,并从另一个项目调用类库,则可能出现此错误。” 在这种情况下,如果是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 ...
            }

详见本文。

如果在类库中调用服务,并从另一个项目调用类库,则可能出现此错误。

还有另一种方法我找到正确的endpointConfigurationName。

如下所示添加endpointConfigurationName

EservicesNew.ServiceClient eservicenew = new EservicesNew.ServiceClient("BasicHttpsBinding_IService");

如下所示找到endpointConfigurationName

添加Web Reference之后,打开配置。Svcinfo文件中生成的参考文件。

找到了两个端点,我使用了正确的端点bindingConfiguration值,即basichttpbinding_iservice

当我在配置文件元素中引用没有全局作用域操作符的契约时,出现了这个错误。

i.e.

<endpoint contract="global::MyNamepsace.IMyContract" .../>

有效,但

<endpoint contract="MyNamepsace.IMyContract" .../>

给出“无法找到引用合同的默认端点元素”错误。

包含MyNamepsace的程序集。IMyContract位于与主应用程序不同的程序集中,因此这可能解释了使用全局作用域解析的必要性。