开发者

WCF - how to set "enableWebScript" dynamically?

I'm working on my first WCF service, which will support several Ajax calls. I have an endpoint configured this way:

  <service behaviorConfiguration="ServiceBehavior" name="AQM">
    <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="Binding1" contract="IAQM" />
  </service>

and my behavior configuration:

   <endpointBehaviors>
      <behavior name="web">
         <webHttp />
         <enableWebScript /> 
      </behavior>
   </endpointBehaviors>

I need to create my own error handling so that I can format some specific information back to the client (see here http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/). My WebServiceHostFactory looks like this:

public class MyServiceFactory : WebServiceHostFactory
{
    public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
    {
        var sh = new ServiceHost(typeof(AQM), baseAddresses);
        sh.Description.Endpoints[0].Behaviors.Add(new WebHttpBehaviorEx());
        return sh;
    }
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return base.CreateServiceHost(serviceType, baseAddresses);
    }
}

public class WebHttpBehaviorEx : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        // Clear default error handlers
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
        // Add our own error handler
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandlerEx());
    }    

However, after I created my own error handler, it seems it overrides the "enableWebScript" setting I had in my config above, which I think makes sense because now I'm creating my very own behavior dynamically which doesn't have any of the config settings above.

I read that this setting should be used with WCF/Ajax for security purposes (see here http://www.asp.net/ajaxlibrary/Using%20JSON%20Syntax%20with%20Ajax.ashx). So my question is, how can I set the the "enableWebScript" setting on my dynamically created behavior? Or is it not possible?

Update (6/1/2011): I'm also looking to dynamically change the behavior to use Windows credentials for authentication. In the config file it's done like this:

<bindings>
  <webHttpBinding>
    <binding name="Binding1">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </webHttpBi开发者_如何学Pythonnding>
</bindings>

This is another setting I need to make programmatically since it seems to ignore the config setting.


For me it worked after adding the following constructor in WebHttpBehaviorEx

    public  WebHttpBehaviorEx()
    {
        DefaultBodyStyle = WebMessageBodyStyle.Wrapped;
        DefaultOutgoingRequestFormat = WebMessageFormat.Json;
        DefaultOutgoingResponseFormat = WebMessageFormat.Json;
    }


There is a class WebScriptEnablingBehavior that you should be able to create in instance of programmatically and add it to the Behaviors collection of your endpoint. I've never tried it, and don't know how exactly that would work having multiple behaviors defined on a single endpoint, but I think that's basically what you're doing in your declarative configuration. Unfortunately WebScriptEnablingBehavior (which inherits from WebHttpBehavior) is sealed, so you can't just inherit from it.

Update: (from here)

The WebScriptEnablingBehavior is a "profile" of the WebHttpBehavior functionality designed specifically for interop with ASP.NET AJAX clients. It adds in some AJAX-isms like the ability to automatically generate ASP.NET AJAX client proxies.

I'm not sure you actually need to use <enableWebScript/>, like Carlos said, it sounds like it's only needed when you're using ASP.NET AJAX.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜