WCF:找不到引用契约的默认端点元素

时间:2011-11-30 02:07:14

标签: wpf wcf

  

可能重复:
  WCF Error - Could not find default endpoint element that references contract 'UserService.UserService'

我的客户端代码如下:

public virtual bool CanExecute(Move movement)
        {
            using (MediatorServiceContractClient mediatorProxy = new MediatorServiceContractClient())
            {
                return mediatorProxy.CanMove(movement);
            }
        }

错误发生在MediatorServiceContractClient的构造函数中。

合同如下:

namespace GameServices
{
    [ServiceContract]
    public interface IMediatorServiceContract
    {
        [OperationContract]
        bool CanMove(Move move);
    }
}

我有一个GameServices库,它包含以下配置数据:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IMediatorServiceContract" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8732/GameServices/Mediator/"
          binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMediatorServiceContract"
          contract="GameViewModel.MediatorServiceReference.IMediatorServiceContract"
          name="WSHttpBinding_IMediatorServiceContract">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
    <services>
      <service name="GameServices.MediatorService">
        <endpoint address="" binding="wsHttpBinding" contract="GameServices.IMediatorServiceContract">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/GameServices/Mediator/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

然后我有一个ViewModel库,它包含以下配置数据:

enter code here`<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IMediatorServiceContract" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8732/GameServices/Mediator/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMediatorServiceContract2"
                contract="GameViewModel.MediatorServiceReference.IMediatorServiceContract"
                name="WSHttpBinding_IMediatorServiceContract">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

我还在运行时收到以下错误消息: 无法在ServiceModel客户端配置部分中找到引用合同“MediatorServiceReference.IMediatorServiceContract”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

我已查看有关此常见问题的所有评论和反馈。然而对我来说问题仍然存在,我被困住了。我错过了什么?为什么这么复杂?

3 个答案:

答案 0 :(得分:1)

看起来客户端配置端点中的合同应该是GameServices.IMediatorServiceContract而不是GameViewModel.MediatorServiceReference.IMediatorServiceContract,假设您粘贴的接口代码是客户端实际使用的。

答案 1 :(得分:0)

一些事情(我没有使用过WPF,但我已经用WCF做了很多,所以如果它是WPF特定的问题,这可能不适用。)

您看到的错误消息显示无法找到MediatorServiceReference.IMediatorServiceContract的端点。如果查看服务配置,您的端点合同为GameServices.IMediatorServiceContract;这很可能是导致错误的原因。修复引用的合同,你应该好好去。

其次,将using块与代理一起使用被认为是不好的做法。最好只是打开代理,拨打电话,然后关闭(或根据需要中止)频道。有关详细说明,请参阅Avoiding Problems with the Using Statement

答案 2 :(得分:0)

Mike Gledhill适合WPF应用。我的源应用程序配置文件不在客户端可执行项目中,而是在我的viewmodel项目中(仍在客户端)。

因此,我只是从我的可执行项目中添加了一个指向我的viewmodel项目中的app配置的链接。

在经历了12个小时的挫折之后,它终于奏效了。

所以请注意:可执行项目必须有一个APP配置文件参考,即使它不包含执行呼叫的cs文件。