我已经抓了7个小时试图解决这个问题。我在网上搜索过但没有运气。我有一个Angular应用程序正在向WCF命令行托管服务应用程序发出请求。我通过使用这两个类来设法获得CORS:
public class EnableCorsBehavior : BehaviorExtensionElement, IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{ }
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{ }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
var requiredHeaders = new Dictionary<string, string>();
requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CustomHeaderMessageInspector(requiredHeaders));
}
public void Validate(ServiceEndpoint endpoint) { }
public override Type BehaviorType
{
get { return typeof(EnableCorsBehavior); }
}
protected override object CreateBehavior()
{
return new EnableCorsBehavior();
}
}
和
Request Method:OPTIONS
Status Code:405 Method Not Allowed
将此自定义扩展添加到app.config文件解决了我的CORS问题。我当前的问题是每当我发出POST请求时,都会收到错误:
{{1}}
我对C#很陌生,我似乎无法找到允许我通过此代码的代码放置位置。我有一个想法,它应该放在BeforeSendReply()方法的某个地方。请帮我!我真的很感激它!
问候!
答案 0 :(得分:2)
我找到了解决方案,我希望这可以帮助每个遇到同样问题的人。在我在问题中发布的sameCnts
课程中,我在CustomHeaderMessageInspector
方法中编辑了以下代码,如下所示:
AfterReceiveRequest
我希望代码所做的是使用OPTIONS方法和&#34;标记&#34;来监控任何请求。它具有预检状态。然后我修改了// return null;
var httpRequest = (HttpRequestMessageProperty)request
.Properties[HttpRequestMessageProperty.Name];
return new
{
origin = httpRequest.Headers["Origin"],
handlePreflight = httpRequest.Method.Equals("OPTIONS",
StringComparison.InvariantCultureIgnoreCase)
};
中的代码,如下所示:
BeforeSendReply
我希望得到的任何请求都是用OPTIONS标记的,并通过返回200状态代码来处理它。这让它终于工作了,我希望它可以帮助别人!
答案 1 :(得分:0)
除了 realnsleo 回答:
我有使用(动态)correlationState的问题,因为我的项目必须是 在Framework 3.5中
我也试图简化一些行:
private class CORSHeaderInjectingMessageInspector : IDispatchMessageInspector
{
private static IDictionary<string, string> _headersToInject = new Dictionary<string, string>
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS" },
{ "Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Origin,Accept" },
{ "Access-Control-Request-Headers", "POST" }
};
public object AfterReceiveRequest( ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
var httpRequest = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
return httpRequest.Method.Equals("OPTIONS", StringComparison.InvariantCulture);
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
if ((bool) correlationState)
{
var httpResponse = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
httpResponse.SuppressEntityBody = true;
httpResponse.StatusCode = HttpStatusCode.OK;
}
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in _headersToInject)
{
httpHeader.Headers.Add(item.Key, item.Value);
}
}