Tag: wcf rest

WebOperationContext.current和HttpContext.Current之间的区别

我为客户开发Web和移动应用程序。 在我目前的架构中,Web访问和移动访问之间共享许多资源。 可以在Web上显示aspx页面,并将其调用到移动应用程序中的Web视图。 我的问题是: WebOperationContext.Current和HttpContext.Current对象有什么区别? 根据我的理解,它是同一个对象,但我注意到WebOperationContext.Current在某些情况下为空,我不明白为什么。

WCF奇怪的行为

我在使用Web服务时得到了这个: 合同’IServices’的操作’Login’指定要序列化的多个请求体参数,而不包含任何包装元素。 最多可以在没有包装元素的情况下序列化一个body参数。 删除额外的body参数或将WebGetAttribute / WebInvokeAttribute上的BodyStyle属性设置为Wrapped。 我使用界面看起来像: namespace DreamServices { // 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 IServices { [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “LogIn/{username}/{password}”)] string Login(string […]

WCF REST服务模板40(CS)跨域错误

嗨,我有一个WCFrest服务使用(WCF REST服务模板40(CS))。 我已设法让它返回Json响应。 项目运行时工作正常。 但是当我尝试从浏览器向服务进行Ajaxcall时,我收到错误: 跨源请求已阻止:同源策略不允许在http://192.168.0.70:8001/Service/test读取远程资源。 这可以通过将资源移动到同一域或启用CORS来解决。 和Ajax电话: $.ajax({ type: “POST”, url: “http://192.168.0.70:8001/Service/test”, contentType: “application/json; charset=utf-8”, dataType: “json”, async: true, cache: false, success: function (msg) { var Tabels = msg.d; alert(‘success’); } }); 这是web.config: <!– Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the […]

WCF REST服务中的CORS支持

我在Windows服务中托管了WCF REST服务,并且我希望每次响应都发送Access-Control-Allow-Origin HTTP标头(定义为CORS的一部分)。 我尝试的解决方案是在IDispatchMessageInspector实现中使用以下内容: public void BeforeSendReply(ref Message reply, object correlationState) { var httpResponse = reply.Properties[“httpResponse”] as HttpResponseMessageProperty; if (httpResponse != null) { // test of CORS httpResponse.Headers[“Access-Control-Allow-Origin”] = “*”; } } 通常这会起作用,但不幸的是我的服务也使用HTTP基本授权 ,这意味着当请求进入而没有Authorization标头时,WCF会自动发送401响应,要求提供凭据。 遗憾的是,在此初始交换期间,WCF不会调用我的IDispatchMessageInspector,因此Access-Control-Allow-Origin标头不会添加到初始交换中。 当我尝试从浏览器调用服务时,会出现此问题。 CORS指定仅当源域与Access-Control-Allow-Origin响应头中列出的域匹配时才允许跨源请求(*匹配所有域)。 不幸的是,当浏览器看到没有Access-Control-Allow-Origin标头的初始401响应时,它会阻止访问(根据相同的源策略 )。 有没有办法在WCF自动发送的初始401响应中添加标头?