Do i *have* to use ObservableCollection in Silverlight WCF client?

こ雲淡風輕ζ 提交于 2019-12-22 12:18:37

问题


When accessing Silverlight in WCF you get proxies generated with ObservableCollection

Thats fine when you're databinding, but a little clumsy when you're just calling a method. For instance the following service method :

    [OperationContract]
    public SearchOrdersMsgOut SearchOrders(ShippingStatusType[] shippingStatuses,
                                           string[] orderId)
    {
    }

gets generated with ObservableCollection. What! They're just parameters. Why would I ever want to 'observe' them?

I'm fine if I have to do this - but it seems like there should be a way to force simple array structures when I know I'm never databinding - especially on input messages.

I'd much rather do this :

 searchCriteria.PaymentStatus = new [] { PaymentStatusType.PaymentFailed, PaymentStatusType.Unpaid };               

than this :

 searchCriteria.PaymentStatus = new ObservableCollection<PaymentStatusType> { PaymentStatusType.PaymentFailed, PaymentStatusType.Unpaid };

Is there a way?

PS. I do actually use a SearchCriteria object for my search criteria - but I simplified for this example wondering if parameters were handled differently.


回答1:


You can do this service-wide, but not on a per-method basis. In the Add Service Reference dialog box, click on "Advanced", and choose "System.Array" for the Collection type. But I'm not aware of any way to do it method-by-method, i.e., use array for some methods and ObservableCollection for others.




回答2:


OK here's a bizarre twist after having got used to using ObservableCollection for my silverlight clients.

I tried to return a Linq2XSD object from my WCF service - and then suddenly low and behold it changed all the ObservableCollection<T> properties into simple arrays [].

I thought it was something specific to Linq2XSD - so I tried just adding a simple XTypedElement property to the service definition:

    public XTypedElement[] PipelineLogs { get; set; }

This triggers [] instead of ObservableCollection<T> in the generated proxy - where normally string[] would become ObservableCollection<string>.

Don't ask me why!

I've since removed it because I actually prefer ObservableCollection<T>. I just thought the observation might interest someone with a similar problem - especially if anyone can explain why its doing it!




回答3:


Ended having the OPPOSITE problem when VS2010 RC had a bug preventing it from generating ObservableCollections.

Fortunately there are two workarounds:

Option 1: Believe the best option – this is to update the “Reference.svcmap” file for the impacted service reference. In Solution Explorer, select “show all files” and expand the impacted reference node. There you will find the “Reference.svcmap” file, double click to open into the editor. For the observablecollection mapping, you should see currently something like this:

 <CollectionMapping TypeName="System.Collections.ObjectModel.ObservableCollection`1" Category="List" />

Change TypeName value to include the Silverlight assembly “System.Windows” – like below :

 <CollectionMapping TypeName="System.Collections.ObjectModel.ObservableCollection`1, System.Windows" Category="List" />

Option 2: Generate your Reference.vb/.cs service reference proxy code files outside of VS by using directly SLSvcUtil.exe. Example of running the tool via command-line where it will address the observablecollection issue code generation issue: "C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Tools\SlSvcUtil.exe” /r:"C:\Program Files (x86)\Microsoft Silverlight\3.0.40818.0\System.Windows.dll" /ct:System.Collections.ObjectModel.ObservableCollection`1 http:///Service1.svc This will by default generate a C# version of your service reference proxy code. If you need to generate a VB version, you can pass a “/Language:VB” switch.




回答4:


Another thing to check (if you want ObservableCollection<T> but you're getting T[]) - is the Reference.svcmap file

Make sure that you have included 'System.Windows' in the typename.

  <CollectionMapping TypeName="System.Collections.ObjectModel.ObservableCollection`1, System.Windows" Category="List" />

and not

  <CollectionMapping TypeName="System.Collections.ObjectModel.ObservableCollection`1" Category="List" />

I'm guessing perhaps it can't find the Dll and defaults to []



来源:https://stackoverflow.com/questions/1911441/do-i-have-to-use-observablecollection-in-silverlight-wcf-client

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