IWcfPolicy - 动态添加邮件头

时间:2013-05-08 08:43:03

标签: castle-windsor wcffacility

是否可以动态地将标题信息(或查询字符串)添加到wcf请求中? 我一直在用IWcfPolicy搞砸了这个:

    var xmlObjectSerializer = new DataContractSerializer(typeof(string));

    var addressHeader = AddressHeader.CreateAddressHeader("client", "http://tempuri.org/", "someValue", xmlObjectSerializer);

    var endpointAddress = new EndpointAddress(new Uri(url), new AddressHeader[] { addressHeader });

    invocation.ChannelHolder.ChannelFactory.Endpoint.Address = endpointAddress;

    invocation.Proceed();
然而,这不起作用。任何帮助都会非常适合。

1 个答案:

答案 0 :(得分:1)

好的,所以这里是如何做到的:

    using (var scope = new OperationContextScope((IContextChannel) (invocation.ChannelHolder.Channel)))
                {
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = new HttpRequestMessageProperty
                        ()
                        {
                            Headers =
                                {
                                    {"client", LicenseManager.Instance.GetCurrentLicense().LicenseKey}
                                }
                        };

                    invocation.Proceed();
                }

此代码进入IWcfPolicy实现的Apply方法。 由于这篇文章找到了解决方案:how to add a custom header to every wcf call