IWcfPolicy - add message headers on the fly

三世轮回 提交于 2019-12-25 01:01:07

问题


Is it somehow possible to add header info (or querystrings) to a wcf request on the fly? I've been messing around a bit with the IWcfPolicy like this:

    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();

this does not work however. Any help would be very much apperciated.


回答1:


ok so here's how to do it:

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

                    invocation.Proceed();
                }

This code goes into Apply method of the IWcfPolicy implementation. Found solution because of this post: how to add a custom header to every wcf call



来源:https://stackoverflow.com/questions/16436209/iwcfpolicy-add-message-headers-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!