WCF中使用HttpContext.Current的办法

正常方法创建的WCF服务,就是在asp.netk中的.SVC后缀的

是无法使用httpcontext.current等信息的

解决办法就是在服务前加上个标识:[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]   

此标识是加在服务的class前的..不是接口中

别忘了引用 System.ServiceModel.Activation.dll

和在类上面using System.ServiceModel.Activation;

还需要在web.config中加入aspNetCompatibilityEnabled="true" 
例如:
<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />          
        </behavior>        
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

这样WCF服务就可以用WEB特性了

例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace Kingsoft.Attachment.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Attachment”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]     
    public class WCFAttachment : IServer.IAttachment
    {

Powered By D&J (URL:http://www.cnblogs.com/Areas/)
原文地址:https://www.cnblogs.com/Areas/p/2186667.html