我在纯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 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
在使用NUnit测试方法时,如何绕过WCF服务方法中的WebOperationContext为空
我有一个单元测试项目使用NUnit来测试WCF方法返回的数据:
public class SampleService
{
public XmlDocument Init ()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
return _defaultInitializationXMLfile;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个测试方法如下
[TextFixture]
public class SampleServiceUnitTest
{
[Test]
public void DefaultInitializationUnitTest
{
SampleService sampleService = new SampleService();
XMLDocument xmlDoc = sampleService.Init();
XMLNode xmlNode = xmlDoc.SelectSingleNode("defaultNode");
Assert.IsNotNull(xmlNode, "the default XML element does not exist.");
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在测试期间收到错误
SampleServiceUnitTest.DefaultInitializationUnitTest:
System.NullReferenceException : Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
关于SampleService方法中的WebOperationContext.
我正在尝试GetAwesomeResultsAsXml()为以下WCF Rest服务编写单元测试(更多的集成测试).
我如何处理WebOperationContext嘲弄方面?
什么是最好的方法?
public class AwesomeRestService : AwesomeRestServiceBase, IAwesomeRestService
{
public AwesomeSearchResults<AwesomeProductBase> GetAwesomeResultsAsXml()
{
return GetResults();
}
private static AwesomeSearchResults<AwesomeProductBase> GetResults()
{
var searchContext = AwesomeSearchContext
.Parse(WebOperationContext.Current);
..............
..............
..............
}
}
[ServiceContract]
public interface IAwesomeRestService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/search/xml")]
AwesomeQueryResults<AwesomeProductBase> GetAwesomeResultsAsXml();
}
public class AwesomeSearchContext
{
................
................
................
public static AwesomeSearchContext Parse
(WebOperationContext operationContext)
{
return WebOperationContext.Current != null ? new
AwesomeSearchContext(operationContext.IncomingRequest.UriTemplateMatch.QueryParameters) : …Run Code Online (Sandbox Code Playgroud)