1

我创建了一个 WCF 服务,它编译好并发布好;我创建了一个 Web 应用程序来测试 WCF 服务,最初在使用svcutil.exe它时创建了源文件而不是配置文件。因此,我将该服务添加为服务参考,并且在我尝试运行客户端应用程序之前看起来还不错。出现以下错误:

Could not find default endpoint element that 
references contract 'ServiceReference1.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 contract could be found in the client element.

我发现 web.config 文件有问题,搜索帖子时发现我需要将服务模型部分从服务配置文件复制到客户端测试 web 配置文件。这没有帮助。

WCF 服务配置文件

<configuration>

<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings>
  <webHttpBinding>
    <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true" />
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="webHttpBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
 </behaviors>
  <services>
  <service behaviorConfiguration="webHttpBehavior" name="WcfInstanceRules2.Service1">
    <endpoint address="mex" 
     binding="webHttpBinding" bindingConfiguration="webHttpBinding"      

    contract="WcfInstanceRules2.IService1" behaviorConfiguration="web"/>
  </service>
 </services>
 </system.serviceModel>
 <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

WebApp 测试配置文件

    <configuration>
    <connectionStrings>
    <add name="ApplicationServices"
    connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
    </connectionStrings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>
     <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    </system.web>
    <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    <system.serviceModel>
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
     <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="webHttpBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      </behaviors>
       <services>
       <service behaviorConfiguration="webHttpBehavior" name="WcfInstanceRules2.Service1">
        <endpoint address="mex" binding="webHttpBinding"   
     bindingConfiguration="webHttpBinding" 
     contract="WcfInstanceRules2.IService1" behaviorConfiguration="webHttpBehavior"/>
      </service>
     </services>
     </system.serviceModel>
    </configuration>
4

2 回答 2

1

您需要使用在客户端定义的客户端端点。您当前正在 Web 应用程序上定义一个新的服务主机。在客户端,您应该有类似...

<system.serviceModel>
  <client>
     <endpoint address="http://.../mex" binding="webHttpBinding"   
 bindingConfiguration="webHttpBinding" 
 contract="ServiceReference1.IService1" behaviorConfiguration="webHttpBehavior"/>
  </client>
</system.serviceModel>

为了让这更容易,当你使用Add Service Reference时,VS.NET 会为你添加这个客户端端点。

于 2012-08-10T19:08:57.630 回答
0

在您的端点标签更改 contract="ServiceReference1.IService1"

其中 ServiceReference1 是您在项目中添加的服务引用。

于 2014-05-14T11:38:13.813 回答