【问题标题】:how to connect localhost wcf service with asp.net 4.0如何将 localhost wcf 服务与 asp.net 4.0 连接
【发布时间】:2021-01-25 19:18:23
【问题描述】:

我刚刚创建了一个 wcf 服务来为我的本地 Web 项目提供一些功能。这是我的 localhost wcf 服务 web 配置。

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/staff_care_wcf_ws/RestServiceImpl.svc"
        behaviorConfiguration="WebHttp" binding="webHttpBinding" bindingConfiguration="LargeWeb"
        contract="SC_WCF.IRestServiceImpl" />
    </client>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

最后这是我处理本地托管 wcf 服务的本地网站 web 配置。

<system.serviceModel>
     <bindings>
      <basicHttpBinding>
        <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/staff_care_wcf_ws/RestServiceImpl.svc"
        behaviorConfiguration="WebHttp" binding="basicHttpBinding" bindingConfiguration="LargeWeb"
        contract="SC_WCF.IRestServiceImpl">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
     <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
      </webScriptEndpoint>
    </standardEndpoints>
  </system.serviceModel>

这怎么行得通。出现以下错误:

The endpoint at 'http://localhost/staff_care_wcf_ws/RestServiceImpl.svc' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings."}

这里出了什么问题,请帮我解决这些问题.....

更新

【问题讨论】:

  • 出于好奇,您为什么今天使用 WCF(和 .NET Framework 4.0)?两者都有几十年的历史并且实际上已经过时(更不用说 WCF 配置是一个巨大的 PITA - 正如您所看到的)
  • @dai 帮帮我。这个网络项目对我很重要..

标签: c# wcf asp.net-4.0


【解决方案1】:

需要删除endpointBehaviors节点中的webHttp,因为IIS中用于服务部署的基地址不是“http://localhost/staff_care_wcf_ws/RestServiceImpl.svc”。您的 WCF 服务只有一个 basicHttpsBinding 端点。您的客户端配置文件应如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8000/Service1.svc"
          behaviorConfiguration="WebHttp" binding="basicHttpBinding" name="webHttpBinding_IService1"
          contract="ConsoleApp1.IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WebHttp">
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
        <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
        </webScriptEndpoint>
    </standardEndpoints>
</system.serviceModel>
</configuration>

如果WCF服务部署到IIS,其基地址应该如下:

所以你的WCF配置文件和下面的配置文件效果一样:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

ProtocolMapping 可以简化配置,它会帮助你生成一个 basicHttpsBinding 端点。

如果问题仍然存在,请随时告诉我。

更新

您的配置文件有点混乱,我建议按照以下步骤重新创建部署在 IIS 中的 WCF 服务:

创建一个 WCF 服务应用程序项目:

这是项目目录:

服务的接口存放在IService文件中,服务的实现类存放在Service1文件中。

将web.config中的内容替换为如下配置信息:

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

    <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.7.2" />
        <httpRuntime targetFramework="4.7.2"/>
    </system.web>

    <system.serviceModel>
        <services>
            <service name="WcfService2.Service1"
                     behaviorConfiguration="CalculatorServiceBehavior">
                <endpoint address=""
                          binding="basicHttpBinding"
                          contract="WcfService2.IService1"
                          bindingConfiguration="LargeWeb"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="CalculatorServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        
            <bindings>
                <basicHttpBinding>
                    <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                        <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    </binding>
                </basicHttpBinding>
            </bindings>
    

    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
        <directoryBrowse enabled="true"/>
    </system.webServer>

</configuration>

以上配置信息会生成一个basicHttpBinding端点。

将其发布到 IIS:

使用浏览器访问服务:

使用添加服务引用生成代理类来调用服务:

这会自动生成代理类和配置文件,可以直接调用。

【讨论】:

  • 如果您觉得我的回复对您有帮助,可以标记为回答。
  • 您是如何将应用程序池设置为 WCF 的。好吧,我有我的默认应用程序池的列表。查看更新...
  • 我们不需要设置应用程序池,可以尝试发布部署在IIS中的模板项目。
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
相关资源
最近更新 更多