Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用跨域WCF服务时拒绝选项方法_C#_Wcf_Rest_Jquery_Cross Domain - Fatal编程技术网

C# 使用跨域WCF服务时拒绝选项方法

C# 使用跨域WCF服务时拒绝选项方法,c#,wcf,rest,jquery,cross-domain,C#,Wcf,Rest,Jquery,Cross Domain,我正在尝试向WCF服务执行POST。 使用Firefox8时,浏览器首先发送一个选项HTTP请求 当我使用WebMessageBodyStyle.Bare作为BodyStyle时,这很好,但我想要的是使用包装的body样式。 当我切换到Wrapped时,选项请求被拒绝,状态为400。 我怀疑这是由于选项请求没有正文,因此BodyStyle解析器失败 以下是我的web方法的布局: [OperationContract(ProtectionLevel = ProtectionLevel.None)]

我正在尝试向WCF服务执行POST。 使用Firefox8时,浏览器首先发送一个选项HTTP请求

当我使用WebMessageBodyStyle.Bare作为BodyStyle时,这很好,但我想要的是使用包装的body样式。 当我切换到Wrapped时,选项请求被拒绝,状态为400。 我怀疑这是由于选项请求没有正文,因此BodyStyle解析器失败

以下是我的web方法的布局:

[OperationContract(ProtectionLevel = ProtectionLevel.None)]
[WebInvoke(Method = "*",
           BodyStyle = WebMessageBodyStyle.Wrapped,
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json)]
bool Ping(String msg);
我使用以下jquery进行调用:

$.ajax({
            url: "http://localhost/Server/Service.svc/Ping",
            data: JSON.stringify({msg: msg}),
            type: "POST",
            processData: false,
            contentType: "application/json",
            timeout: 10000,
            dataType: "text"
});
我非常感谢在这个问题上的任何帮助。。。
谢谢

我通过向Enpoint行为添加一个自定义IErrorHandler解决了这个问题。 有关如何执行此操作的一般说明如下

public bool HandleError(Exception error)
{
  if (error.Message.Contains("IsEmpty = true")) return true;

  return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error.Message.Contains("IsEmpty = true"))
            {
                fault = Message.CreateMessage(version, "");
                var jsonFormatting = new WebBodyFormatMessageProperty(WebContentFormat.Json);
                fault.Properties.Add(WebBodyFormatMessageProperty.Name, jsonFormatting);
                var httpResponse = new HttpResponseMessageProperty
                                       {
                    StatusCode = System.Net.HttpStatusCode.OK,
                    StatusDescription = "OK"
                };
                fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponse);
            }
            else
            {
                fault = GetJsonFaultMessage(version, error);

                var jsonFormatting = new WebBodyFormatMessageProperty(WebContentFormat.Json);
                fault.Properties.Add(WebBodyFormatMessageProperty.Name, jsonFormatting);
                var httpResponse = new HttpResponseMessageProperty
                                       {
                    StatusCode = System.Net.HttpStatusCode.BadRequest,
                    StatusDescription = "Bad Request"
                };
                fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponse);
            }
        }