WCF自定义压缩绑定 [英] WCF custom binding for compression

查看:317
本文介绍了WCF自定义压缩绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照Microsoft的压缩示例。我已经将编码器,编码器工厂和绑定元素添加到我的解决方案。与他们的示例的区别是,我们不通过配置文件(需求)注册我们的端点,而是使用自定义服务主机工厂。

Following the sample for compression by Microsoft. I have added the encoder, encoder factory, and binding element to my solution. The difference from their sample is that we do not register our endpoints via the config file (requirement), but instead use a custom Service Host Factory.

服务主机:

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
     ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);

     if (host.Description.Endpoints.Count == 0)
     {
          host.AddDefaultEndpoints();
     }

     host.Description.Behaviors.Add(new MessagingErrorHandler());      

     return host;
}

所以我试过的是添加一个自定义绑定到我的端点,注册该端点与绑定它看起来像我必须使用 AddServiceEndpoint ,但将需要一个未知的接口。我知道我可以得到所有的接口,serviceType实现,并做一个 getInterfaces()[0] ,但似乎是一个不安全的方法对我来说。
所以有一种方法来注册我的端点与自定义绑定,不知道接口,或有一个可能是一个更好的方法,我应该采取。

So what I have tried is to add a custom binding to my endpoint, but to register that endpoint with the binding it looks like I have to use the AddServiceEndpoint but that will require an interface which is unknown. I know I could get all the interfaces that the serviceType implements and do a getInterfaces()[0], but that seems to be an unsafe approach to me. So is there a way to register my endpoint with the custom binding and not know the interface, or is there a maybe a better approach that I should take.

我尝试添加自定义绑定:

My attempt at adding custom binding:

CustomBinding compression = new CustomBinding();
compression.Elements.Add(new GZipMessageEncodingBindingElement());
foreach (var uri in baseAddresses)
{
     host.AddServiceEndpoint(serviceType, compression, uri);//service type is not the interface and is causing the issue
}


推荐答案

您的自定义绑定需要一个传输绑定元素;当前你只有一个消息编码绑定元素。您还需要向自定义绑定中添加 HttpTransportBindingElement

Your custom binding needs a transport binding element; currently you only have a message encoding binding element. You need to add probably a HttpTransportBindingElement to your custom binding as well:

CustomBinding compression = new CustomBinding(
    new GZipMessageEncodingBindingElement()
    new HttpTransportBindingElement());

从服务类型查找接口时,没有内置的逻辑。 WebServiceHostFactory中使用的逻辑类似于下面的代码(这个代码继承了1个继承/实现级别,但理论上可以更深入。)

As far as finding the interface from the service type, there's no built-in logic for that. The logic used in the WebServiceHostFactory is similar to the one shown below (this code goes 1 inheritance / implementation level deep, but you could in theory go deeper too.

    private Type GetContractType(Type serviceType) 
    { 
        if (HasServiceContract(serviceType)) 
        { 
            return serviceType; 
        } 

        Type[] possibleContractTypes = serviceType.GetInterfaces() 
            .Where(i => HasServiceContract(i)) 
            .ToArray(); 

        switch (possibleContractTypes.Length) 
        { 
            case 0: 
                throw new InvalidOperationException("Service type " + serviceType.FullName + " does not implement any interface decorated with the ServiceContractAttribute."); 
            case 1: 
                return possibleContractTypes[0]; 
            default: 
                throw new InvalidOperationException("Service type " + serviceType.FullName + " implements multiple interfaces decorated with the ServiceContractAttribute, not supported by this factory."); 
        } 
    } 

    private static bool HasServiceContract(Type type) 
    { 
        return Attribute.IsDefined(type, typeof(ServiceContractAttribute), false); 
    }

这篇关于WCF自定义压缩绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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