通过代码添加绑定扩展 [英] Add a Binding Extension via Code

查看:15
本文介绍了通过代码添加绑定扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种通过代码而不是通过配置文件向端点添加绑定扩展的方法.理想情况下,我希望它只是我放置在服务方法上的一个属性.

I'm trying to figure out a way to add a binding extension to an endpoint via code, instead of through configuration files. Ideally I want it to simply be an attribute I place on the service method.

到目前为止,似乎唯一没有公开的是绑定扩展.

So far it seems like the only thing that isn't exposed publicly is the binding extensions.

推荐答案

所以我想通了,我必须先将 WebServiceHostFactory 子类化:

So I figured it out, I had to first subclass WebServiceHostFactory:

/// <summary>
/// RestServiceFactory extends WebServiceHostFactory and adds support for JSONP encoding    
/// </summary>
public class RestServiceFactory : WebServiceHostFactory
{        
    /// <summary>
    /// Creates a ServiceHost using the first address in baseAddresses
    /// </summary>
    /// <param name="serviceType"></param>
    /// <param name="baseAddresses"></param>
    /// <returns></returns>
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {            
        Uri[] defaultAddresses = new Uri[1];
        defaultAddresses[0] = baseAddresses[0];

        ServiceHost host = new RestServiceHost(serviceType, defaultAddresses);

        // Bind up the JSONP extension            
        CustomBinding cb = new CustomBinding(new WebHttpBinding());
        cb.Name = "JSONPBinding";

        // Replace the current MessageEncodingBindingElement with the JSONP element
        var currentEncoder = cb.Elements.Find<MessageEncodingBindingElement>();
        if (currentEncoder != default(MessageEncodingBindingElement))
        {                                
            cb.Elements.Remove(currentEncoder);                
            cb.Elements.Insert(0, new JSONP.JSONPBindingElement());                
        }

        host.AddServiceEndpoint(serviceType.GetInterfaces()[0], cb, defaultAddresses[0]);

        return host;
    }                
}

然后我不得不继承 WebServiceHost 以修改它设置行为的方式:

And then I had to subclass WebServiceHost to modify how it was setting up behaviors:

/// <summary>
/// RestServiceHost extends WebServiceHost to add JSONP support
/// </summary>
public class RestServiceHost : WebServiceHost
{
    public RestServiceHost() : base() { }
    public RestServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType,baseAddresses) { }

    protected override void OnOpening()
    {
        base.OnOpening();

        if (base.Description != null)
        {                            
            foreach (ServiceEndpoint endpoint in base.Description.Endpoints)
            {
                if ((endpoint.Binding != null) && (endpoint.Binding.CreateBindingElements().Find<JSONP.JSONPBindingElement>() != null))
                {
                    if (endpoint.Behaviors.Find<WebHttpBehavior>() == null)
                    {
                        endpoint.Behaviors.Add(new WebHttpBehavior());
                    }
                }
            }
        }
    }
}

此更改的原因是 WebServiceHost 由于自定义绑定而未添加任何行为.

The reason for this change is because WebServiceHost was not adding any behaviors because of the custom binding.

完成这两项更改后,请求通过正确的绑定扩展进行管道传输,无需更改 web.config.

With these two changes done, the requests were piped through the correct binding extension, no web.config changes needed.

这篇关于通过代码添加绑定扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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