简体   繁体   English

为什么我在 Global.asax.cs 中的 Application_BeginRequest 没有被自托管的 WCF 服务调用

[英]Why is my Application_BeginRequest in Global.asax.cs not getting called with a self hosted WCF service

I'm self hosting a WCF service that needs to support inbound CORS REST traffic.我自己托管了一个需要支持入站 CORS REST 流量的 WCF 服务。 So I added the Global.asax.cs file with the following code block, but the Application_BeginRequest() never fires.所以我添加了带有以下代码块的 Global.asax.cs 文件,但 Application_BeginRequest() 永远不会触发。 I also have set in my app.Config.我也在我的 app.Config 中设置了。 Is there anything else I need to do and does this work for self hosted services or just services hosted via IIS?还有什么我需要做的吗?这是否适用于自托管服务或仅通过 IIS 托管的服务?

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string origin = HttpContext.Current.Request.Headers["origin"];
        if (!String.IsNullOrEmpty(origin)) // CORS origin?
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS") // CORS origin w/ options?
        {
            var requestedHeaders = HttpContext.Current.Request.Headers["Access-Control-Request-Headers"];
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", requestedHeaders);
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.StatusCode = 200;
            HttpContext.Current.Response.End();
        }
    }

Only Global files hosted on IIS will take effect.只有在 IIS 上托管的全局文件才会生效。 IIS will treat WCF service as a web service to parse its Global file. IIS 会将 WCF 服务视为 web 服务来解析其全局文件。 If it is self-hosted, Global file will not be parsed and run.如果它是自托管的,则不会解析和运行全局文件。

We can make WCF support JSONP to solve cross-domain:我们可以让WCF支持JSONP来解决跨域:

<binding name="bind1" crossDomainScriptAccessEnabled="true">
</binding>

You can also implement IDispatchMessageInspector to add response headers before the service responds.This solution is suitable for self-hosting.您还可以实现 IDispatchMessageInspector 以在服务响应之前添加响应标头。此解决方案适用于自托管。

public class ServerMessageLogger : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
           return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            WebOperationContext ctx = WebOperationContext.Current;
            ctx.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
        }
    }

For more information about IDispatchMessageInspector,Please refer to the following link:有关 IDispatchMessageInspector 的更多信息,请参考以下链接:

https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.idispatchmessageinspector?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.idispatchmessageinspector?view=netframework-4.8

If you are still unclear, you can refer to the link below, which contains the complete code:如果还是不清楚,可以参考下面的链接,里面有完整的代码:

How to enable Cross-Origin Resource Sharing in.Net Console Application WCF Service? 如何在.Net 控制台应用程序 WCF 服务中启用跨域资源共享?

UPDATE更新

The following pictures is my demo:以下图片是我的演示:

在此处输入图像描述

在此处输入图像描述

One of the two pictures above uses WebOperationContext one does not use.上面两张图,一张使用了WebOperationContext,一张没有使用。

In fact, WebOperationContext is similar to HttpContext.实际上,WebOperationContext 与 HttpContext 类似。 WebOperationContext is usually used in WCF REST methods, and HttpContext is usually used in ASP.NET WebForms pages or ASMX Web Service Web methods. WebOperationContext is usually used in WCF REST methods, and HttpContext is usually used in ASP.NET WebForms pages or ASMX Web Service Web methods.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Global.asax.cs Application_BeginRequest - 抛出 WebFaultException - Global.asax.cs Application_BeginRequest - Throw WebFaultException 为什么CSS文件受全局asax中的Application_BeginRequest影响 - Why CSS files are affected by Application_BeginRequest in global asax 在global.asax Application_BeginRequest中添加动态参数 - Add dynamic parameter in global.asax Application_BeginRequest 在Application_BeginRequest(Global.asax)中查询数据库 - Query database in Application_BeginRequest (Global.asax) global.asax中的NHibernate会话Application_BeginRequest - NHibernate Session in global.asax Application_BeginRequest asp.net global.asax Application_BeginRequest - asp.net global.asax Application_BeginRequest 为什么我的 ASP.net 解决方案中的一个“Global.asax.cs”文件没有被调用,其中有两个启动项目? - Why is one of my `Global.asax.cs` file in my ASP.net solution with two startup projects not called? 如何在IIS 6.0上的asmx Web服务中获取请求之前调用Application_Start(在Global.asax.cs中) - how to get Application_Start (in Global.asax.cs) to be called before requests in asmx web service on IIS 6.0 MissingMethodException Global.asax.cs - MissingMethodException Global.asax.cs 在Global.asax.cs中获取客户端IP地址-可以吗? - Getting client ip address in Global.asax.cs - is it possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM