Cheers Greg, some useful info there. We are embarcking on a Silverlight project ourselves soon, so this will come in handy! ;-)
Steve On 13 June 2010 23:27, Greg Keogh <g...@mira.net> wrote: > Hi Stephen, it looks like an later message of mine may have got lost. I > accidentally found that my worry about WCF behaviours was wasted, as many > WCF interfaces are not supported on the Silverlight client. I stumbled over > an article that pointed I have to do the following (simplified real code). > The code is a bit obtuse because I had to use Action callback combined > with using to keep the OperationContext open while methods are called. I > hope this skeleton code might be of use to others who have to pass arbitrary > extra data from a WCF client to the service. The key bits are: > > > > · The method CallWrapper on the client puts a header into the > client proxy. I have to use this wrapper on each call and it has to be > wrapped in a using. > > · The method GetHeaderValue<> on the server pulls header values > out of the current context. It’s interesting that it’s generic and you can > get strongly typed values. > > > > Cheers, > > Greg > > > > > > *Sliverlight Client* > > > > public void SampleSeviceCall(int xmlToken, Action<bool, Exception, > SampleResultWrapper> callback) > > { > > using (var client = MakeClient()) > > { > > client.Proxy.SampleSeviceCallCompleted += (s, e) => > > { > > callback(e.Cancelled, e.Error, e.Result); > > }; > > CallWrapper(client, () => { > client.Proxy.SampleSeviceCallAsync(xmlToken); }); > > } > > } > > > > private void CallWrapper(SafeClient<AggregationClient, IAggregationService> > client, Action callback) > > { > > using (OperationContextScope ocs = new OperationContextScope > (client.Proxy.InnerChannel)) > > { > > OperationContext.Current.OutgoingMessageHeaders.Add( > > MessageHeader.CreateHeader("secretId", "MyNamespace", > secretId)); > > callback(); > > } > > } > > > > *WCF Service* > > > > public SampleResultWrapper SampleServiceMethod(int xmlToken) > > { > > Guid secretId = GetHeaderValue<Guid>("secretId"); > > SampleResultWrapper result = new SampleResultWrapper(); > > try > > { > > result.SomeXml = GetTheXml(xmlToken); > > } > > catch (Exception ex) > > { > > result.Error = ex; > > } > > return result; > > } > > > > private T GetHeaderValue<T>(string name) > > { > > int index = > OperationContext.Current.IncomingMessageHeaders.FindHeader(name, > " MyNamespace "); > > return (index >= 0 ? > > OperationContext.Current.IncomingMessageHeaders.GetHeader<T>(index) > : > > default(T)); > > } > > > > > > >