通过代码添加绑定扩展

时间:2010-04-06 02:11:29

标签: wcf

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

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

2 个答案:

答案 0 :(得分:3)

如果你给我一些更多的上下文,我可以使用你的代码片段重写。只是让我知道你的绑定扩展名....

如果您需要使用WebServiceHostFactory

  public class ServiceHostFactory : WebServiceHostFactory
  {

     protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
          ServiceHost serviceHost = new ServiceHost(typeof(IService));
          CustomBinding customBinding= new CustomBinding();
          customBinding.Name = “myCustomBinding”;
          customBinding.Elements.Add(new SomeBindingExtensionElement());

          serviceHost.AddServiceEndpoint(typeof(IService), customBinding, 
           “http://localhost/service/CustomEndpoint”);

答案 1 :(得分:0)

所以我想通了,我必须首先继承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来修改它设置行为的方式:

/// <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因为自定义绑定而未添加任何行为。

完成这两项更改后,请求通过正确的绑定扩展传递,无需更改web.config。