0

是否可以动态地将标头信息(或查询字符串)添加到 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();

但是,这不起作用。任何帮助都会非常感激。

4

1 回答 1

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 each wcf call

于 2013-05-08T09:20:58.897 回答