微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将本地wcf服务与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配置,用于处理本地托管的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."}

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

更新

enter image description here

解决方法

由于在IIS中用于服务部署的基址不是“ http://localhost/staff_care_wcf_ws/RestServiceImpl.svc”,因此您需要在endpointBehaviors节点中删除 webHttp 。您的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,则其基本地址应如下:

enter image description here

enter image description here

因此,您的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可以简化配置,它将帮助您生成基本的HttpsBinding端点。

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

更新

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

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

enter image description here

这是项目目录:

enter image description here

服务的接口存储在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:

enter image description here

使用浏览器访问该服务:

enter image description here

使用“添加服务参考”来生成代理类以调用服务:

enter image description here

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。