WCF 中HttpContext.Current为null的解决办法

解决WCF hosting IIS的环境中使HttpContext.Current不为NULL, 来我们传递每次请求中的Cookie

经过测试发现只要注意下面亮点就可以了:

1. 在hosting WCF的web.config中加入:

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

 
2. 在每个Service的定义(注意不是Contract, 不过就算加在Contract上编译是也会报错)上加上下面Attribute:

[AspNetCompatibilityrequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

 
再测试看看, 发现WCF hosting IIS的程序中也可以使用HttpContext.Current了.

转载于:https://www.cnblogs.com/federer320/p/4597869.html

WCF 接收 `uni.uploadFile` 文件,你可以按照以下步骤进行操作: 1. 首先,在 WCF 服务的契约定义一个操作合同,用于接收文件。例如: ```csharp [ServiceContract] public interface IFileService { [OperationContract] void UploadFile(Stream fileData); } ``` 2. 在服务实现类,实现该操作合同。在方法,你可以使用 `Stream` 类型的参数接收文件数据。例如: ```csharp public class FileService : IFileService { public void UploadFile(Stream fileData) { // 处理文件数据 // 可以使用流操作、保存到磁盘、或者进行其他自定义操作 } } ``` 3. 在服务的配置文件,配置绑定和终结点。你可以使用基于 HTTP 的绑定,如 `basicHttpBinding` 或 `webHttpBinding`。例如: ```xml <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IFileService" /> </basicHttpBinding> </bindings> <services> <service name="YourNamespace.FileService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileService" contract="YourNamespace.IFileService" /> </service> </services> </system.serviceModel> ``` 4. 在客户端调用代码,引用服务契约并创建代理对象。然后,可以使用代理对象调用 `UploadFile` 方法并传递文件数据。例如: ```csharp using (var client = new FileServiceClient()) { using (var fileStream = File.OpenRead("path/to/file")) { client.UploadFile(fileStream); } } ``` 这样,你就可以在 WCF 成功接收 `uni.uploadFile` 的文件数据。记得根据实际需求进行适当的异常处理和文件操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值