简体   繁体   中英

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. So I added the Global.asax.cs file with the following code block, but the Application_BeginRequest() never fires. I also have set in my app.Config. Is there anything else I need to do and does this work for self hosted services or just services hosted via 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 will treat WCF service as a web service to parse its Global file. If it is self-hosted, Global file will not be parsed and run.

We can make WCF support JSONP to solve cross-domain:

<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.

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:

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?

UPDATE

The following pictures is my demo:

在此处输入图像描述

在此处输入图像描述

One of the two pictures above uses WebOperationContext one does not use.

In fact, WebOperationContext is similar to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM