我在纯WCF上下文中运行了一些RESTful服务(即没有启用ASP.NET兼容性,因此没有HttpContext.Current
可用的对象).
服务的URL在请求开始时使用IHttpModule
(在此时确实具有HttpContext
并使用其重写HttpContext.Current.RewritePath
)来重写,以.svc
从URL中删除诸如扩展之类的内容.
但是,我需要访问WCF基础结构中请求的原始URL.是否有一个相当于HttpContext.Current.Request.RawUrl
上OperationContext
或WebOperationContext
类哪儿了吗?使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri
返回重写的URL而不是原始的URL.
wcf url-rewriting httpcontext operationcontext weboperationcontext
我有几种方法的WCF服务.我想记录来自客户端的原始请求,无论它是如何发送的.一种方法接受数据作为查询字符串(严格用于遗留支持),我可以使用它来记录:
OperationContext.Current.IncomingMessageHeaders.To.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)
这种情况就足够了,但其他方法允许客户端使用svcutil.exe生成的代理类将数据作为XML发送.在这种情况下,我在s:Body of中找到了我想要的数据:
OperationContext.Current.RequestContext.RequestMessage
Run Code Online (Sandbox Code Playgroud)
不幸的是,无论我尝试什么,我都无法在读取之前创建消息的缓冲副本.这是一个例子:
public CascadeResponse SendCustomer(Customer c)
{
Message msg = OperationContext.Current.RequestContext.RequestMessage.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
LogMessage(msg);
// Now that the request is logged, get on with the rest
}
Run Code Online (Sandbox Code Playgroud)
但是,在SendCustomer的第一行,我收到以下错误:
此消息不支持该操作,因为它已被读取.
这是我创建缓冲副本的关键吗?我猜我在这里犯了一些错误的东西.
编辑:
好的,所以方法现在是这样的:
public CascadeResponse SendCustomer(Message requestMessage)
{
Message msg = OperationContext.Current.RequestContext.RequestMessage.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
LogMessage(msg);
// Now that the request is logged, get on with the rest
Customer c = msg.GetBody<Customer>();
string clientKey = "1111"; // This should be the clientKey string passed to the function along with the customer
return …
Run Code Online (Sandbox Code Playgroud) 我目前正在将我的WCF RESTful服务从.NET 3.5(入门套件)迁移到.NET 4.我使用Visual Studio 2010中的WCF Rest服务模板启动了我的项目.我必须弄清楚如何保留我的授权方案(formely)使用ServiceAuthorizationManager完成RequestInterceptor.经过一些工作和研究,我完成了它.但现在我有了抵押问题.我的服务用于使用HTTP状态代码和简要说明向我的客户反馈任何处理错误.我在服务方法的许多方面使用WebOperationContext来向客户描述出错的地方,如下所示:
protected void returnCode(HttpStatusCode code, string description)
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusDescription = description;
ctx.OutgoingResponse.StatusCode = code;
}
Run Code Online (Sandbox Code Playgroud)
但在WCF 4中,只有StatusCode工作 - StatusDescription静默失败.我无法弄清楚为什么.我唯一的猜测是WebOperationContext在这个新的WCF 4场景中不起作用,我应该使用OperationContext,但这也行不通.在我的自定义类中使用以下方法扩展ServiceAuthorizationManager,通知客户端请求无法访问,因为auth摘要格式错误:
private void GenerateBadDigestMessage(ref OperationContext operationContext)
{
Message reply = Message.CreateMessage(MessageVersion.None, null, null, new DataContractJsonSerializer(typeof(object)));
HttpResponseMessageProperty hrp = new HttpResponseMessageProperty();
hrp.StatusCode = HttpStatusCode.Forbidden;
hrp.StatusDescription = "bad digest";
reply.Properties[HttpResponseMessageProperty.Name] = hrp;
operationContext.RequestContext.Reply(reply);
operationContext.RequestContext = null;
}
Run Code Online (Sandbox Code Playgroud)
即使在这里使用OperationContext direclty(由WebOperationContext提供),StatusDescription也不起作用.
我在这里缺少什么?为什么这么小的东西可以从.NET 3.5打破到4?
rest wcf http-status-codes operationcontext weboperationcontext
我目前正在查看OperationContect.Current属性.是否有(嵌套)属性将始终返回客户端的机器名称?我目前正在使用net.tcp绑定,但希望将来支持其他绑定.
使用.NET 3.5 SP1
在WCF安全性中,给定当前的OperationContext,确定请求是SOAP请求还是REST请求的最佳方法是什么?
我正在尝试使用WCF设置发布/订阅系统,并且WCF服务器位于Windows服务中.绑定是net.TCP.该服务向客户端提供"订阅"方法,以便客户端可以将回调处理程序注册到将从链接到服务器的DLL引发的事件.在Subscribe方法中,我尝试使用OperationContext.Current.GetCallbackChannel方法获取回调通道.当我尝试这个时,OperationContext.Current属性返回NULL.
谁能告诉我在什么情况下这个属性会返回null?我错过了设置吗?我将在下面包含服务代码和接口代码.我在Visual Studio 2012和目标框架4.5中使用c#.
服务:
namespace WService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class WcfPublisherService : IWcfPublisherContract
{
IOALogic logic = new OAControlExample();
IWcfSubscriberContract _callback = null;
public void Subscribe()
{
_callback = OperationContext.Current.GetCallbackChannel<IWcfSubscriberContract>();
logic.BarriersChanged += logic_BarriersChanged;
}
public void UnSubscribe()
{
logic.BarriersChanged -= logic_BarriersChanged;
}
void logic_BarriersChanged(object sender, BarriersChangedEventArgs e)
{
_callback.BarriersChanged(e.BarrierLines);
}
}
}
Run Code Online (Sandbox Code Playgroud)
接口:
namespace WService
{
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IWcfSubscriberContract))]
public interface IWcfPublisherContract
{
[OperationContract(IsOneWay=false, IsInitiating=true)]
void Subscribe();
[OperationContract(IsOneWay = false, IsTerminating=true)]
void UnSubscribe();
} …
Run Code Online (Sandbox Code Playgroud) With C#5 Async-Await in WCF, after an await if rest of the code continues on a different thread, we loose the Current Operation Context. (OperationContext.Current is null).
I am working on a WCF Service which calls another external service. And there are a few Custom Binding Extensions used in the external service call which access the Operation Context. So I need the Context to be propagated during this call and it cant just work with copying the operation context into …
我OperationContext
在调用异步操作(和我的threadid更改)后获取null 时遇到问题.
我知道这是一个已知问题,我已经提出了一些关于这个问题的StackOverflow问题.
在.net 4.6.2
没有为问题的解决办法,你可以看这里.
OperationContext.Current Async改进
WCF现在能够在ExecutionContext中包含OperationContext.Current,以便OperationContext流经异步延续.通过这种改进,WCF允许CurrentContext从一个线程传播到另一个线程.这意味着即使在调用OperationContext.Current之间有上下文切换,它的值也会在整个方法执行过程中正确流动.
为了得到我的支持,我需要做些什么特别的事吗?我正在使用VS 2013,更新了框架4.6.2
并安装了dev-pack.我已经改变了我要使用的项目,在异步调用后Framework 4.6.2
我仍然得到一个null OperationContext
.
在普通的WCF请求/回复合同中,您可以使用以下内容读取邮件标头:
OperationContract.Current.IncomingMessageHeaders
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何在双工合同的回调方面做到这一点.回调实现内部OperationContext.Current
是null
.
编辑2013年4月5日:我正在使用基于net.tcp的自定义绑定,但有很多自定义.例如,使用协议缓冲区消息编码而不是Xml.还有一些自定义安全性.