【问题标题】:Unable to find element in WCF ServiceModel client configuration section在 WCF ServiceModel 客户端配置部分中找不到元素
【发布时间】:2015-05-19 08:29:15
【问题描述】:
[ServiceContract]
public interface IService1
{
   [OperationContract]
   DataTable GetADUserList(string strUserName, string strFirstName, string strLastName, string strEmail, string domain);
}

我有一个托管在 IIS 中的 WCF 服务,上面的示例服务合同。服务web.config文件设置如下。

完整的 WCF Web.config 文件

 <?xml version="1.0"?>
<configuration>

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <bindings>
            <customBinding>
                <binding name="notSecureBinding">
                    <binaryMessageEncoding />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
                <binding name="SecureBinding">
                     <binaryMessageEncoding />
                    <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>

        <client>
            <endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
                      binding="customBinding"
                      bindingConfiguration="notSecureBinding"
                      contract="ADSearcher.IService1"
                      name="notSecureBinding" />

            <endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
                      binding="customBinding"
                      bindingConfiguration="SecureBinding"
                      contract="ADSearcher.IService1"
                      name="SecureBinding" />
        </client>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

      </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

我正在尝试以编程方式访问服务,如下所示。

  EndpointAddress endpointAddress = new EndpointAddress("http://ServerName.myDomain.org/ADSearcher/Service1.svc");
  IService1 ADUser = new ChannelFactory<IService1>("notSecureBinding", endpointAddress).CreateChannel();

上面的代码抛出了下面的错误

在 ServiceModel 客户端配置部分中找不到名为“notSecureBinding”和合同“ADSearcher.IService1”的端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素

我似乎无法弄清楚我在这里做错了什么,或者是否有更好的选择以编程方式访问此服务?

【问题讨论】:

    标签: c# wcf wcf-binding wcf-endpoint


    【解决方案1】:

    在您的端点中,您指定了Data.GetData 类型的合同。合约类型为IService1,默认情况下,合约名称应为服务接口的类型名称。

    如果您确实希望您的IService 被称为Data.GetData,您可以通过ServiceContractAttribute 指定标识名称:

    [ServiceContract(ConfigurationName = "Data.GetData")]
    public interface IService1
    {
       [OperationContract]
       DataTable GetADUserList(string strUserName, string strFirstName, string strLastName, string strEmail, string domain);
    }
    

    【讨论】:

    • 我已经关注了你和 Tom Redfern 的回答,但我仍然无法让它工作(我已经更新了我原来的问题的 部分以匹配我目前的有,请检查)。我可以指向错误的 web.config 文件吗? WCF dll 放在 WCF 服务网站的 bin 文件夹中。然后我向我的 MVC 应用程序添加了对同一个 WCF dll 的引用,这样我就可以访问 IService1 类型的合同,这是正确的做法吗?我可以从浏览器成功访问服务。
    【解决方案2】:

    找不到名为“customBinding”的端点元素....在 ServiceModel 客户端配置部分

    这是因为您在配置文件中定义了两个客户端端点。它们被命名为:

    • notSecureBinding 和
    • 安全绑定

    所以错误信息是正确的,没有名为“customBinding”的端点元素。

    我认为你需要这样做:

    IService1 ADUser = new ChannelFactory<IService1>("<either of the two bindings you defined>").CreateChannel();
    

    我是否需要在 ASP.NET MVC 中配置与 WCF 相关的任何内容 应用程序的 web.config 文件?

    好的,现在我明白了。您在 ASP.NET 网站中托管 WCF 服务。而你试图从我假设的其他地方调用它?

    您肯定需要在您的 web.config 中定义一个&lt;system.serviceModel /&gt; 部分,它将告诉 IIS 如何托管您的服务。

    目前,您在上面发布的配置定义为 client 端点。这些不引用服务端点,而是旨在允许您调用服务的客户端配置。提示在节节点的名称中:&lt;client /&gt;。要定义您希望公开的服务端点,您需要将端点配置放在 &lt;service /&gt; 部分中。在您的情况下,可能只需将“客户端”更改为“服务”即可使其全部正常工作。

    调用服务的代码可能需要也可能不需要 app.config 文件中的客户端配置。您说您正在以编程方式调用它(尽管没有看到您的完整客户端代码,但我不知道您是否做得足以满足 WCF 客户端调用堆栈),在这种情况下您不需要客户端配置。我希望这能让你更清楚。

    【讨论】:

    • 将“customeBinding”更改为“notSecureBinding”或“SecureBinding”仍然给我同样的错误。
    • @SQL.NETWarrior - 请参阅 Andrew Shepherd 的回答。你实际上有两个问题——我已经解决了一个问题,而安德鲁则解决了另一个问题。您基本上需要将客户端端点配置中的合同值更改为等于定义为服务定义的类型的完全限定名称(在您的情况下为 myNamespace.IService1)
    • 我已经关注了你的回答和 Andrew Shepherd 的回答,但我仍然无法让它工作(我已经更新了我原来的问题的 部分以匹配我的目前有,请检查)。我可以指向错误的 web.config 文件吗? WCF dll 放在 WCF 服务网站的 bin 文件夹中。然后我向我的 MVC 应用程序添加了对同一个 WCF dll 的引用,这样我就可以访问 IService1 类型的合同,这是正确的做法吗?我可以从浏览器成功访问服务。
    • IService1 需要完全符合命名空间。
    • 即使在使用命名空间完全限定 IService1 之后,我仍然会收到相同的错误。
    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2014-10-11
    • 2012-03-11
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多