使用JQGrid与WCF Web服务联合使用

4
我正在尝试从我的ASP.NET 2.0 WebForms应用程序中运行的WCF Web服务获取JQGrid的数据。问题在于,WCF Web服务希望数据格式化为JSON字符串,而JQGrid正在执行HTTP Post并将其传递为Content-Type:application/x-www-form-urlencoded。
尽管似乎有几种选项可以返回给JQGrid的数据格式(它接受JSON、XML和其他格式),但似乎没有办法改变它向Web服务传递输入的方式。
因此,我正在尝试找出如何调整WCF服务以使其接受
Content-Type: application/x-www-form-urlencoded

不是…而是
Content-Type:"application/json; charset=utf-8"

当我使用 JQuery 进行测试时,发送了一个使用 URL 编码的 Ajax 请求(如下所示):
$.ajax({
    type: "POST",
    url: "../Services/DocLookups.svc/DoWork",
    data: 'FirstName=Howard&LastName=Pinsley',
    contentType: "Content-Type: application/x-www-form-urlencoded",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    }
});

调用失败。使用Fiddler检查流量,我发现服务器返回的错误信息如下:

{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":
"The incoming message has an unexpected message format 'Raw'. The expected
message formats for the operation are 'Xml', 'Json'. This can be because 
a WebContentTypeMapper has not been configured on the binding. 
See the documentation of WebContentTypeMapper for more details."...

请注意,由于编码的差异,此代码确实有效。
$.ajax({
    type: "POST",
    url: "../Services/DocLookups.svc/DoWork",
    data: '{"FirstName":"Howard", "LastName":"Pinsley"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    }
});

在服务器上,服务看起来像:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class DocLookups {
    // Add [WebGet] attribute to use HTTP GET

    [OperationContract]
    public string DoWork(string FirstName, string LastName) {
        return "Your name is " + LastName + ", " + FirstName;
    }
}

我的web.config文件包含以下内容:

<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="DocLookupsAspNetAjaxBehavior">
     <enableWebScript />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  <services>
   <service name="DocLookups">
    <endpoint address="" behaviorConfiguration="DocLookupsAspNetAjaxBehavior"
     binding="webHttpBinding" contract="DocLookups" />
   </service>
  </services>
</system.serviceModel>

感谢任何帮助!

你在这里使用REST Starter Kit吗?如果是的话,我可能需要将我的答案从RequestInterceptor修改为MessageInterceptor。 - bendewey
我更新了我的答案,加入了WCF版本。 - bendewey
1个回答

5
如果您无法控制ajax调用,建议创建一个拦截器来覆盖Content-Type头。
public class ContentTypeOverrideInterceptor : RequestInterceptor
{
    public string ContentTypeOverride { get; set; }

    public ContentTypeOverrideInterceptor(string contentTypeOverride) : base(true)
    {
        this.ContentTypeOverride = contentTypeOverride;
    }

    public override void ProcessRequest(ref RequestContext requestContext)
    {
        if (requestContext == null || requestContext.RequestMessage == null)
        {
            return;
        }
        Message message = requestContext.RequestMessage;
        HttpRequestMessageProperty reqProp = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
        reqProp.Headers["Content-Type"] = ContentTypeOverride;
    }
}

如果您查看您的.svc文件,您会看到AppServiceHostFactory类,请将其更改以包括拦截器。

class AppServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var host = new WebServiceHost2(serviceType, true, baseAddresses);
        host.Interceptors.Add(new ContentTypeOverrideInterceptor("application/json; charset=utf-8"));
        return host;
    }
}

这对你来说应该足够了。

更新

如评论中所述,上述方法适用于WCF REST Starter Kit。如果您只是使用普通的WCF服务,则需要创建IOperationBehavior并将其附加到您的服务。以下是行为属性的代码:

public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter
{
    private IDispatchMessageFormatter innerFormatter;
    public string ContentTypeOverride { get; set; }

    public WebContentTypeAttribute(string contentTypeOverride)
    {
        this.ContentTypeOverride = contentTypeOverride;
    }


    // IOperationBehavior
    public void Validate(OperationDescription operationDescription)
    {

    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        innerFormatter = dispatchOperation.Formatter;
        dispatchOperation.Formatter = this;
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {

    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {

    }

    // IDispatchMessageFormatter
    public void DeserializeRequest(Message message, object[] parameters)
    {
        if (message == null)
            return;

        if (string.IsNullOrEmpty(ContentTypeOverride))
            return;

        var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
        httpRequest.Headers["Content-Type"] = ContentTypeOverride;
    }

    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
        return innerFormatter.SerializeReply(messageVersion, parameters, result);
    }
}

你需要修改你的服务合同,使其与此示例一致。

[OperationContract]
[WebContentType("application/json; charset=utf-8")]
public string DoWork(string FirstName, string LastName)
{
    return "Your name is " + LastName + ", " + FirstName;
}

链接

根据您的要求,以下是一些描述WCF扩展的链接。


顺便说一句,这个拦截器可以更好,并且松散地基于WCF REST Starter Kit示例中的XHttpMethodOverrideInterceptor。 - bendewey
我在 .svc 文件中没有看到任何代码,而且后台代码也没有提到工厂类。此外,我不清楚您是否在动态更改传入请求的内容类型(例如从 URL 编码更改为 JSon?),如果是这样,那么是什么将传递的参数从一种格式转换为另一种格式。 - Howard Pinsley
你是在使用WCF REST Starter Kit还是仅仅使用了WCF服务? - bendewey
感谢所有的帮助。不,我没有使用WCF REST Starter Kit。也许我应该研究一下,因为我对它及其功能不熟悉。我也会查看你的回答。你能指点一下这个技术在哪里更详细地讨论吗?再次感谢! - Howard Pinsley
暂时忽略REST Starter kit,专注于我的答案更新。这就是我们所谓的WCF可扩展性,我在底部添加了几个链接,讨论IOperationBehavior。没有特别针对此的真实示例,因为我是从头开始编写的。 - bendewey
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接