WCF 会话 ASP.NET 托管问题

Posted

技术标签:

【中文标题】WCF 会话 ASP.NET 托管问题【英文标题】:WCF Session ASP.NET Hosting Issue 【发布时间】:2012-03-30 03:21:36 【问题描述】:

我正在构建一个与现有应用程序对话的 WCF 服务,而此应用程序需要访问 ASP.NET 会话 - 查看会话的能力是我无法解决的要求。

我构建了 WCF 项目 - 并在 App.config 中有以下设置:

 <system.serviceModel>
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

我还在文件中包含了服务的接口和实现。在实现之前,我有:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
[AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Required)]
public class SearchService : ISearchServiceInterface

这是一个 REST 服务,所以我的界面开始如下:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ISearchServiceInterface


    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    string LoginToWebService_POST(Altec.Framework.Authorization auth);

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    string GetTopLevelFolderName_POST();

服务本身托管在另一个 Web 应用程序中 - 在 SearchService.svc 文件中,该文件包含以下内容:

<%@ServiceHost language=c# Debug="true" Service="Altec.UI.Web.SearchService.SearchService" %>

我将它添加到托管应用程序的 web.config 中:

  <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

我相信我做了所有正确的步骤 - 但是当我尝试运行 Web 应用程序时,我收到了这个错误:

"

System.InvalidOperationException:此服务需要 ASP.NET 兼容性,并且必须托管在 IIS 中。将服务托管在 在 web.config 中打开具有 ASP.NET 兼容性的 IIS 或设置 AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode 属性设置为必需以外的值。在 System.ServiceModel.Activation.AspNetEnvironment.ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode 兼容性模式)在 System.ServiceModel.Activation.AspNetCompatibilityRequirementsAttribute.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription 描述,ServiceHostBase serviceHostBase) 在 System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription 描述,ServiceHostBase 服务主机)在 System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription 描述,ServiceHostBase 服务主机)在 System.ServiceModel.ServiceHostBase.InitializeRuntime() 在 System.ServiceModel.ServiceHostBase.OnBeginOpen() 在 System.ServiceModel.ServiceHostBase.OnOpen(时间跨度超时)在 System.ServiceModel.Channels.CommunicationObject.Open(时间跨度 超时)在 System.ServiceModel.Channels.CommunicationObject.Open() 在 Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo 信息)”

    <?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <!-- database connection details -->

  </appSettings>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>

    <pages>
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
      </controls>
    </pages>
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="1048576"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  <location path="Scripts">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="Content">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="jQuery-UI-layout.css">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <!-- 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>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ISearchServiceInterface" 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://vmwarren27dev.altec-wa.com/Altec.UI.Web.Portal/SearchService.svc/WCPService" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISearchServiceInterface" contract="SearchService.ISearchServiceInterface" name="WSHttpBinding_ISearchServiceInterface">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
    </client>
    <services>
      <service name="Altec.UI.Web.SearchService.SearchService">
        <endpoint address="RESTService" binding="webHttpBinding" behaviorConfiguration="json" contract="Altec.UI.Web.SearchService.ISearchServiceInterface">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="WCPService" binding="wsHttpBinding" contract="Altec.UI.Web.SearchService.ISearchServiceInterface">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </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="True"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="AjaxControlToolkit" publicKeyToken="28f01b0e84b6d53e" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.1.51116.0" newVersion="4.1.51116.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

【问题讨论】:

【参考方案1】:

您提到了 App.config - 听起来您的服务是一个 WCF 服务库,您在 IIS 下托管。如果是这种情况,您需要将 WCF 配置部分从 App.config 复制到托管您的服务的 IIS 应用程序的 Web.config。

库不使用配置文件 - 它们使用调用应用程序的配置文件。

【讨论】:

是的,我这样做了 - 我将编辑以添加上面的完整 web.config。 你更新的 web.config 是应用程序的服务文件还是调用服务的应用程序? 调用服务的应用程序。 将设置放在具有服务文件的 IIS 应用程序的 Web.config 中 - 需要将 AspNetCompatabilityEnabled 设置为 True 的应用程序(在 IIS 下)。 我做到了。还是一样的问题。

以上是关于WCF 会话 ASP.NET 托管问题的主要内容,如果未能解决你的问题,请参考以下文章

WCF 服务 - ASP.net 托管 - 单点登录,如何传递凭据

ASP.NET 中的 REST WCF 服务和会话

具有相对路径的 Asp.net 自托管 WCF 服务 WSDL

尝试在 ASP.NET 网站中托管时出现 WCF 服务异常错误

从 ASP.NET 应用程序验证 WCF 服务

强大的自托管服务器的最佳选择:WCF 与 ASP.NET Web Api