繁体   English   中英

如何将本地主机 wcf 服务与 asp.net 4.0 连接

[英]how to connect localhost wcf service with asp.net 4.0

我刚刚创建了一个 wcf 服务来为我的本地 web 项目提供一些功能。 这是我的本地主机 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>

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

<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."}

这里出了什么问题请帮帮我这些家伙.....

更新

在此处输入图像描述

需要删除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文件中,服务的实现class保存在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:

在此处输入图像描述

使用浏览器访问服务:

在此处输入图像描述

使用Add Service Reference生成代理类来调用服务:

在此处输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM