- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 WCF Restful 服务,我将其作为 Windows 服务托管。我想为我的服务添加跨域支持。但是,当我使用 global.asax 文件时,我可以轻松地做到这一点。但我想将我的服务托管为 Windows 服务。
我创建了一个项目,将我的服务作为 Windows 服务托管。现在我面临的问题是,我现在无法添加跨域支持。我尝试了通过 app.config 文件找到的所有可能的解决方案,但没有一个有效。我已经尝试过这些链接上的解决方案:
我尝试通过在每个服务契约(Contract)方法中调用以下函数来设置代码中的 header 。
private static void SetResponseHeader()
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache, no-store");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Request-Methods", "GET, POST, PUT, DELETE, OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
}
界面:
namespace ReaderService
{
[ServiceContract]
public interface INFCReader
{
[OperationContract]
[WebInvoke(UriTemplate = "GetLogin", Method = "POST")]
GetLoginResults GetLogin(DisplayRequest dispRequest);
}
这里 DisplayRequest 是一个类。
请大家帮忙。如果有人想查看任何其他代码,请告诉我。
非常感谢。
编辑:::::::
非常感谢托马斯的回复。我创建了一个实现 IDispatchMessageInspector 的 MessageInspector 类。我在 MessageInspector 类中有以下代码。
public class MessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
return null;
}
}
我现在收到的错误是——“对象引用未设置为对象的实例。”错误就在上面代码的这一行
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
我想做的就是向我的网络服务添加 CORS 支持。如果我做得正确,请告诉我。或者有没有其他方法可以做到同样的事情。
最佳答案
终于找到了解决我的疑问的方法。
一切都在这里。 Supporting Cross Origin Resource
很好的逐步解释。我想我自己永远无法解决这个问题。
代码:
创建 2 个类,如下所示:
MessageInspector
实现 IDispatchMessageInspector
。BehaviorAttribute
实现 Attribute
、IEndpointBehavior
和 IOperationBehavior
。包含以下详细信息:
//MessageInspector Class
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
namespace myCorsService
{
public class MessageInspector : IDispatchMessageInspector
{
private ServiceEndpoint _serviceEndpoint;
public MessageInspector(ServiceEndpoint serviceEndpoint)
{
_serviceEndpoint = serviceEndpoint;
}
/// <summary>
/// Called when an inbound message been received
/// </summary>
/// <param name="request">The request message.</param>
/// <param name="channel">The incoming channel.</param>
/// <param name="instanceContext">The current service instance.</param>
/// <returns>
/// The object used to correlate stateMsg.
/// This object is passed back in the method.
/// </returns>
public object AfterReceiveRequest(ref Message request,
IClientChannel channel,
InstanceContext instanceContext)
{
StateMessage stateMsg = null;
HttpRequestMessageProperty requestProperty = null;
if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
{
requestProperty = request.Properties[HttpRequestMessageProperty.Name]
as HttpRequestMessageProperty;
}
if (requestProperty != null)
{
var origin = requestProperty.Headers["Origin"];
if (!string.IsNullOrEmpty(origin))
{
stateMsg = new StateMessage();
// if a cors options request (preflight) is detected,
// we create our own reply message and don't invoke any
// operation at all.
if (requestProperty.Method == "OPTIONS")
{
stateMsg.Message = Message.CreateMessage(request.Version, null);
}
request.Properties.Add("CrossOriginResourceSharingState", stateMsg);
}
}
return stateMsg;
}
/// <summary>
/// Called after the operation has returned but before the reply message
/// is sent.
/// </summary>
/// <param name="reply">The reply message. This value is null if the
/// operation is one way.</param>
/// <param name="correlationState">The correlation object returned from
/// the method.</param>
public void BeforeSendReply(ref Message reply, object correlationState)
{
var stateMsg = correlationState as StateMessage;
if (stateMsg != null)
{
if (stateMsg.Message != null)
{
reply = stateMsg.Message;
}
HttpResponseMessageProperty responseProperty = null;
if (reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
{
responseProperty = reply.Properties[HttpResponseMessageProperty.Name]
as HttpResponseMessageProperty;
}
if (responseProperty == null)
{
responseProperty = new HttpResponseMessageProperty();
reply.Properties.Add(HttpResponseMessageProperty.Name,
responseProperty);
}
// Access-Control-Allow-Origin should be added for all cors responses
responseProperty.Headers.Set("Access-Control-Allow-Origin", "*");
if (stateMsg.Message != null)
{
// the following headers should only be added for OPTIONS requests
responseProperty.Headers.Set("Access-Control-Allow-Methods",
"POST, OPTIONS, GET");
responseProperty.Headers.Set("Access-Control-Allow-Headers",
"Content-Type, Accept, Authorization, x-requested-with");
}
}
}
}
class StateMessage
{
public Message Message;
}
}
//BehaviorAttribute Class
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace OpenBetRetail.NFCReaderService
{
public class BehaviorAttribute : Attribute, IEndpointBehavior,
IOperationBehavior
{
public void Validate(ServiceEndpoint endpoint) { }
public void AddBindingParameters(ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters) { }
/// <summary>
/// This service modify or extend the service across an endpoint.
/// </summary>
/// <param name="endpoint">The endpoint that exposes the contract.</param>
/// <param name="endpointDispatcher">The endpoint dispatcher to be
/// modified or extended.</param>
public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
EndpointDispatcher endpointDispatcher)
{
// add inspector which detects cross origin requests
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new MessageInspector(endpoint));
}
public void ApplyClientBehavior(ServiceEndpoint endpoint,
ClientRuntime clientRuntime) { }
public void Validate(OperationDescription operationDescription) { }
public void ApplyDispatchBehavior(OperationDescription operationDescription,
DispatchOperation dispatchOperation) { }
public void ApplyClientBehavior(OperationDescription operationDescription,
ClientOperation clientOperation) { }
public void AddBindingParameters(OperationDescription operationDescription,
BindingParameterCollection bindingParameters) { }
}
}
在此之后,您需要做的就是将此消息检查器添加到服务端点行为中。
ServiceHost host = new ServiceHost(typeof(myService), _baseAddress);
foreach (ServiceEndpoint EP in host.Description.Endpoints)
EP.Behaviors.Add(new BehaviorAttribute());
谢谢大家的帮助。
关于asp.net - 作为 Windows 服务托管的 c# WCF Restful Web 服务的跨源资源共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16024347/
我会尝试尽可能详细地描述我的问题,但如果需要更详细的解释,请告诉我。 为了简化,假设我有 3 个 DLL(实际上我有更多,但我猜这不是很重要): managed-1.dll - 托管 DLL(用 C#
我有一个 Tomcat 6 JSF Web 应用程序,我想使用 CDI bean 来设置它。不过,我必须逐渐将项目转换为 CDI。 我的问题是:CDI bean 和传统的 JSF 托管 bean 可以
我计划将 Web 应用程序从使用 JSF 托管 Bean 转换为使用 CDI 托管 Bean。我知道我需要执行以下操作: 在 WEB-INF 中添加一个空 beans.xml 文件。 将所有 JSF
我在裸域上运行我的解析应用程序。 Parse 并没有让我的生活变得轻松。 起初我很难设置它,因为大多数 DNS 托管服务不允许根域上的 CNAME,而 Parse 需要 CNAME。 决定尝试使用 C
如果您使用 Firebase 托管 将请求定向到 云函数 通过重写,“通过”托管的请求流量是否会记入 Firebase 托管 国标转出 ? (忽略 Cloud Function 的计费) 换句话说,
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我有一个特定的用例,我正尝试使用 Firebase 托管来解决,它如下: 我正在使用 Gridsome 生成静态网站 在构建之前,Gridsome 使用 Graphql 并从 Headless CMS
您好,我有一个blazor页面,我想在其中显示一个变量。 这个变量从另一个线程(通过Websocket接收数据的另一个线程)获取更新,我想以线程安全的方式显示它: Blazor页面 @page "/n
我想使用 Firebase 托管来托管一个 angular 应用程序,我需要创建一个重定向到另一个 URL 中的一些旧文件。 根据 Firebase 文档,您可以进行基本重定向 "redirects"
我正在尝试找出满足电子学习平台以下要求的最佳方法。 我在其中编辑视频的后端,以便可以将它们彼此链接(例如youtube-comment函数) 用户只能在访问平台时观看视频(不允许下载!) 视频只能通过
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
我想这是一个相当深入的主题,因此任何带有洞察信息的网址也很乐意接受。我一直在使用原生 DirectX,但从未管理过。另一方面,大多数情况下,在开发不需要高级 GPU 渲染的其他类型的应用程序时,我通常
我刚刚将一个网站部署到 Firebase 托管,效果非常好 - 设置非常简单。 但是,我的问题是,有什么方法可以使访问该网站受到身份验证的限制吗?这是一个管理面板,只有我的团队才能访问。有什么方法可以
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我刚刚将一个网站部署到 Firebase 托管,它运行良好 - 设置非常简单。 但是,我的问题是,有什么方法可以通过身份验证限制对网站的访问?这是一个只有我的团队才能访问的管理面板。有什么方法可以用密
如果我想托管一个对公众用处不大的应用程序(例如,一个将点打印到控制台的程序,它们的数量取决于现在的时间),我喜欢在启动板中的方式不必乱扔全局托管站点的命名空间并用诸如 HourDot 之类的名称填充它
我是一名优秀的程序员,十分优秀!