尝试从app.config文件创建WCF绑定的异常

时间:2014-10-02 10:35:02

标签: web-services wcf app-config wcf-binding wcf-client

您好我有一个c#客户端应用程序试图连接到WCF Web服务。绑定的配置位于客户端的app.config文件中。

当我调用instatiate Web服务(这是一个https连接)时,我收到以下错误。

异常详细信息:

System.InvalidOperationException: Could not find default endpoint element that references contract 'TcWcfServices.ITcWcfService' 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 contract could be found in the client element.
   at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
   at System.ServiceModel.ConfigurationEndpointTrait`1.CreateSimplexFactory()
   at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)
   at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
   at xx.Int.Biz.Services.TMOScheduling.TcWcfServices.TcWcfServiceClient..ctor()

我的代码简单调用

   if (_wcfClient == null)
        _wcfClient = new TcWcfServiceClient();

在我的TMOSAcheduling.exe.config文件中,我有这个片段试图绑定到WCF服务。

   <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpsBinding_ITcWcfService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="33554432" maxBufferPoolSize="524288" maxReceivedMessageSize="33554432"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
          <readerQuotas maxDepth="999999999" maxStringContentLength="999999999"
            maxArrayLength="999999999" maxBytesPerRead="999999999" maxNameTableCharCount="999999999" />
          <security mode="Transport">
            <transport clientCredentialType="None"
                       proxyCredentialType="None"
                       realm=""/>
            <message clientCredentialType="UserName"
                     algorithmSuite="Default"/>
          </security>
        </binding>

      </basicHttpBinding>
    </bindings>

    <!-- If Environment is HTTPS then use below client and comment out HTTP Client-->
    <!-- HTTPS Client -->

    <client>
      <endpoint address="https://xyz.externalhttpsAddredd.net/TcWcfServices/TcWcfServices.svc"
                behaviorConfiguration="TcWcfServicesBehavior"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpsBinding_ITcWcfService"
                contract="TcWfcServices.ITcWcfService"
                name="BasicHttpsBinding_ITcWcfService" />
    </client>

    <behaviors>
      <endpointBehaviors>
        <behavior name="TcWcfServicesBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

我想知道是否有人可以给我一些关于问题是什么或者我如何添加HTTPS绑定的建议,以便我可以实例化并调用该服务。

此配置文件必须是手动生成的&#34;因为我正在开发一个名为非HTTPS的服务,并且还在我的localhost开发Web服务站点上。

此外,此客户端应用无法在我的开发人员上运行。因为我无法从dev访问服务器URL。机器

谢谢!我正处于关键时刻实现这一目标并尝试了所有我能想到的类似结果。

编辑这里是第二个配置文件

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpsBinding_ITcWcfService"
       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
       allowCookies="false" bypassProxyOnLocal="false"      hostNameComparisonMode="StrongWildcard"
  maxBufferSize="33554432" maxBufferPoolSize="524288" maxReceivedMessageSize="33554432"
  messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
  useDefaultWebProxy="true">
      <readerQuotas maxDepth="999999999" maxStringContentLength="999999999"
        maxArrayLength="999999999" maxBytesPerRead="999999999" maxNameTableCharCount="999999999" />
      <security mode="Transport">
        <transport clientCredentialType="None"
                   proxyCredentialType="None"
                   realm=""/>
        <message clientCredentialType="UserName"
                 algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<client>
  <endpoint address="https://xxxxxx.testexternal.net/TcWcfServices/TcWcfServices.svc"
            behaviorConfiguration="TcWcfServicesBehavior"
            binding="basicHttpBinding"             
            contract="TcWfcServices.ITcWcfService" />
</client>
<behaviors>
  <endpointBehaviors>
    <behavior name="TcWcfServicesBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

  Second edit.  This worked but had certificate errors due to the https

  <system.serviceModel>
     <bindings>
       <basicHttpBinding>
          <binding name="BasicHttpBinding_ITcWcfService"
            closeTimeout="00:01:00"
            openTimeout="00:01:00"
            receiveTimeout="00:10:00"
            sendTimeout="00:01:00"
            allowCookies="false"
           bypassProxyOnLocal="false"
            hostNameComparisonMode="StrongWildcard"
           maxBufferSize="65536"
            maxBufferPoolSize="524288"
           maxReceivedMessageSize="65536"
           messageEncoding="Text"
           textEncoding="utf-8"
           transferMode="Buffered"
        useDefaultWebProxy="true">
      <readerQuotas maxDepth="32"
        maxStringContentLength="8192"
        maxArrayLength="16384"
          maxBytesPerRead="4096"
        maxNameTableCharCount="16384" />
       <security mode="Transport">
           <transport clientCredentialType="None"
            proxyCredentialType="None"
            realm="" />
            <message clientCredentialType="UserName"
             algorithmSuite="Default" />
           </security>
          </binding>
       </basicHttpBinding>
     </bindings>
     <client>
       <endpoint address="https://txxxx.external.net/TcWcfServices/TcWcfServices.svc"
           binding="basicHttpBinding"
           bindingConfiguration="BasicHttpBinding_ITcWcfService"
           contract="TcWcfServices.ITcWcfService"
           name="BasicHttpBinding_ITcWcfService" />
     </client>
   </system.serviceModel>

2 个答案:

答案 0 :(得分:1)

问题在于你的&lt; endpoing&gt; element已定义name属性,因此您需要将其删除以使其成为该合同的“默认端点”,或者在客户端代理对象的构造函数中手动指定端点名称。

来自http://msdn.microsoft.com/en-us/library/ms731762(v=vs.110).aspx

的文档
  

可选的字符串属性。该属性唯一标识一个   给定合同的终点。您可以为a定义多个客户端   给定合同类型。每个定义必须用a来区分   唯一的配置名称。 如果省略此属性,则   对应的端点用作与关联的默认端点   指定的合同类型。默认值为空字符串。

答案 1 :(得分:0)

答案最终成为我做的最后一次编辑,但我最终还是有了一个证书问题(另外一件事)。所以我们最终还是去了HTTP。

Tomasr - 你的建议至少有助于指出一些我不了解的关于端点等的事情。而且我能够验证我实际上正在使用正确的配置 - 这是我第二次猜测自己。