自托管WCF服务的CORS

时间:2019-03-08 14:25:05

标签: wcf self hosted idispatchmessageinspector

我正在尝试在WCF服务中实现CORS支持。

我从

获得了一些代码

https://enable-cors.org/server_wcf.html

public class CustomHeaderMessageInspector : IDispatchMessageInspector
        {
            Dictionary<string, string> requiredHeaders;
            public CustomHeaderMessageInspector (Dictionary<string, string> headers)
            {
                requiredHeaders = headers ?? new Dictionary<string, string>();
            }

            public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
            {
                return null;
            }

            public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
                foreach (var item in requiredHeaders)
                {
                    httpHeader.Headers.Add(item.Key, item.Value);
                }           
            }
        }

但是我在这一行收到错误消息

public class CustomHeaderMessageInspector : IDispatchMessageInspector

错误:类只能从其他类继承

我如何继承 IDispatchMessageInspector

谢谢

1 个答案:

答案 0 :(得分:0)

您应该添加

Dim method = httpRequest.Method
If method.ToLower() = "options" Then httpResponse.StatusCode = System.Net.HttpStatusCode.NoContent

在BeforeSendReply末尾的示例中,否则至少POST请求将不起作用。