WCF全球(.asax)行为 [英] WCF Global(.asax) Behavior

查看:188
本文介绍了WCF全球(.asax)行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个全局选项,当REST调用包含& format = json时,将响应作为JSON字符串输出。



如果输入以下内容我的方法中的字符串它的工作原理:

  WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;但是,如果我添加这行,在我的Global.asax文件中的任何地方,我得到一个nullException为当前上下文:

  String format =; 

if(HttpContext.Current.Request.QueryString [format]!= null)
format = HttpContext.Current.Request.QueryString [format];

if(String.Equals(json,format,StringComparison.OrdinalIgnoreCase))
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.Format = System.ServiceModel.Web.WebMessageFormat .Json;

异常在此触发:

  System.ServiceModel.Web.WebOperationContext.Current 

我可以在全局添加这个功能(WCF)?

解决方案

您可以通过服务行为将您自己的DispatchMessageInspector添加到WCF处理管道。 这里是如何做



要通过配置文件应用行为,首先应该从BehaviorExtensionElement派生新类,并覆盖成员BehaviorType和CreateBehavior。
然后添加到配置部分类似(具有完整的类型名称)

 < system.serviceModel> 
< extensions>
< behaviorExtensions>
< add name =myBehaviortype =SomeNamespace.MyBehaviorExtensionElement,AssemblyName,
Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null/>
< / behaviorExtensions>
< / extensions>
< /system.serviceModel>

 < behaviors> 
< behavior configurationName =myServiceBehavior>
< myBehavior />
< / behavior>
< / behaviors>

最后将此配置应用于您的服务。


I want to create a global option that when a REST call contains &format=json to output the response as a JSON string.

If I enter the following String in my method it works:

WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;

However, if I add this line, anywhere in my Global.asax file, I get a nullException for Current Context:

String format = "";

if (HttpContext.Current.Request.QueryString["format"] != null)
  format = HttpContext.Current.Request.QueryString["format"];

if (String.Equals("json", format, StringComparison.OrdinalIgnoreCase))
  System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.Format = System.ServiceModel.Web.WebMessageFormat.Json;

The exception is triggered here:

System.ServiceModel.Web.WebOperationContext.Current

Anyone know how I can add this functionality globally (WCF)?

解决方案

You can add your own DispatchMessageInspector to WCF processing pipeline via service behavior. Here is how to do that.

To apply behavior via config file at first you should derive new class from BehaviorExtensionElement and override members BehaviorType and CreateBehavior. Then add to config section similar to that (with your full type name)

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="myBehavior" type="SomeNamespace.MyBehaviorExtensionElement, AssemblyName,
                Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
       </behaviorExtensions>
    </extensions>
</system.serviceModel>

and that

<behaviors>
    <behavior configurationName="myServiceBehavior">
        <myBehavior />            
    </behavior>
</behaviors>

Finally apply this configuration to your service.

这篇关于WCF全球(.asax)行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆