c# - 无法在 WCF ServiceModel 客户端配置部分中找到元素

标签 c# wcf wcf-binding wcf-endpoint

[ServiceContract]
public interface IService1
{
   [OperationContract]
   DataTable GetADUserList(string strUserName, string strFirstName, string strLastName, string strEmail, string domain);
}

我有一个 WCF 服务托管在 IIS 中,上面有示例服务契约(Contract)。服务 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();

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

Could not find endpoint element with name 'notSecureBinding' and contract 'ADSearcher.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 name could be found in the client element

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

最佳答案

在您的端点中,您指定了 Data.GetData 类型的契约(Contract)。协定类型为IService1,默认情况下,协定名称应为服务接口(interface)的类型名称。

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

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

关于c# - 无法在 WCF ServiceModel 客户端配置部分中找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30320441/

相关文章:

c# - 从浏览器运行 WCF 方法

c# - WCF Web 服务响应 XML 文件

wcf - 已超过最大名称表字符数配额 (16384)

wcf - 在 WCF-CustomIsolated Receive Location 上创建失败消息

c# - 无法在 ASP C# 中切换到 DropDownList 中的另一个值(它切换回以前的值)

c# - 复选框(取消选中所有其他复选框

.NET WCF 服务引用使用服务器名称而不是 IP 地址,导致使用时出现问题

c# - 可靠地重新连接到服务器 TCPClient C#

c# - 在模拟另一个域帐户时进行调试

wcf - 如何在 WSDL 中使用 HTTPS 为卸载 SSL 后的 WCF 服务配置服务端点