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

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.

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.

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!

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.

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 []

Related

UseLegacyPathHandling is not loaded properly from app.config runtime element

I'm trying to use the new long path support at my app. In order to use it, without forcing clients to have the newest .NET 4.6.2 version instelled on their machines, one should only add those elements to his app.config (see the link for more info https://blogs.msdn.microsoft.com/dotnet/2016/08/02/announcing-net-framework-4-6-2/) :
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />
</runtime>
When I use it in my execution project it works perfectly. The problem is in my testing projects (which use Nunit). I've added app.config to my test project in the same way I've added it to the execution project.
Using the ConfigurationManager class I've managed to ensure that app config indeed loaded (in short: using an app setting which i was able to retrieve in a unit test).
Using ConfigurationManager.GetSection("runtime"), I even managed to ensure the runtime element has been loaded properly (_rawXml value is the same as in app.config).
But (!) for some reason the app config runtime element is not influencing the UseLegacyPathHandling variable and therefore all of my calls with long path fail.
I guess the problem is somehow relates to the fact that testing projects become dll's that are loaded using the Nunit engine, which is the execution entry point.
I'm facing the exact same problem in another project I have, which is a dll loaded by Office Word application. I believe the problem is the same in both cases and derived from the fact that the projects are not meant to be an execution entry point.
It's important to understand that I've no access to the executions their self (Word Office or Nunit) and therefore I can't configure them myself.
Is there an option to somehow make the AppContextSwitchOverrides get loaded from scratch dynamically? Other ideas will be most welcome.
I've been having the same issue, and have noted the same lack of loading of that particular setting.
So far, what I've got is that the caching of settings is at least partly to blame.
If you check out how it's implemented, disabling the cache has no effect on future calls to values (i.e. if caching is enabled and something is accessed during that time, then it will always be cached).
https://referencesource.microsoft.com/#mscorlib/system/AppContext/AppContext.cs
This doesn't seem to be an issue for most of the settings, but for some reason the UseLegacyPathHandling and BlockLongPaths settings are getting cached by the time I can first step into the code.
At this time, I don't have a good answer, but if you need something in the interim, I've got a highly suspect temporary fix for you. Using reflection, you can fix the setting in the assembly init. It writes to private variables by name, and uses the specific value of 0 to invalidate the cache, so it's a very delicate fix and not appropriate for a long term solution.
That being said, if you need something that 'just works' for the time being, you can check the settings, and apply the hack as needed.
Here's a simple example code. This would be a method you'll need in your test class.
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
// Check to see if we're using legacy paths
bool stillUsingLegacyPaths;
if (AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths) && stillUsingLegacyPaths)
{
// Here's where we trash the private cached field to get this to ACTUALLY work.
var switchType = Type.GetType("System.AppContextSwitches"); // <- internal class, bad idea.
if (switchType != null)
{
AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false); // <- Should disable legacy path handling
// Get the private field that is used for caching the path handling value (bad idea).
var legacyField = switchType.GetField("_useLegacyPathHandling", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
legacyField?.SetValue(null, (Int32)0); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.
// Ensure the value is set. This changes the backing field, but we're stuck with the cached value for now.
AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths);
TestAssert.False(stillUsingLegacyPaths, "Testing will fail if we are using legacy path handling.");
}
}
}

Working with reference lists in canned workflows

I am experimenting with canned workflows and need to know if it's possible to work with lists of domain objects in a reference variable, i.e. reference lists. Here is some code to illustrate the concept...
public void Run(Workflow workflow)
{
ReferenceVariable variable = workflow.InputReferenceVariables.First();
WorkflowRunner runner = new WorkflowRunner(workflow);
List<PointSet> pointSets = PetrelProject.Inputs.Selected.OfType<PointSet>().ToList();
runner.SetInputVariableBinding(variable, pointSets);
runner.Run();
}
This following picture shows the basic workflow.
"Local reference variable 1" is the input variable to which I am assigning the reference list. I know the example is hacky, but I tried to simplify everything as much as possible.
Anyways, it doesn't seem to work by binding a list, but it also doesn't throw any exceptions. Nothing happens at all when I run it. Anyone have any ideas?
Official response from Schlumberger is that reference lists are not yet supported. There is a work item in their system to provide a reference list API, but it is not yet target for a release.

#ValidateConnection method is failing to be called when using "#Category component"

I have an issue in a new devkit Project where the following #ValidateConnection method is failing to be called (but my #processor methods are called fine when requested in the flows)
#ValidateConnection
public boolean isConnected() {
return isConnected;
}
I thought that the above should be called to check whether to call the #Connect method.
I think it is because I am using a non default category (Components) for the connector
#Category(name = "org.mule.tooling.category.core", description = "Components")
And the resulting Behavoir is different to what I am used to with DevKit in Cloud connector mode.
I guess I will need to do checks in each #processor for now to see if the initialization logic is done, as there doesn't seem to be an easy way to run a one time config.
EDIT_________________
I actually tried porting it back to a cloud connector #cat and the same behaviour, maybe its an issue with devkit -DarchetypeVersion=3.4.0, I used 3.2.x somthing before and things worked a bit better
The #ValidateConnection annotated method in the #Connector is called at the end of the makeObject() method of the generated *ConnectionFactory class. If you look for references of who is calling your isConnected() you should be able to confirm this.
So no, you should not need to perform the checks, it should be done automatically for you.
There must be something else missing... do you have a #ConnectionIdentifier annotated method?
PS. #Category annotation is purely for cosmetic purposes in Studio.

WCF Generated Proxy Crashes Monodroid and Monotouch

I've generated a proxy client for my WCF RIA service class via slsvcutil.exe as described in the Xamarin "Introduction to Web Services" document, however when I try to make a call to one of the async methods, my "completed" event handler never gets called -- after a couple of minutes, the app simply terminates from the Android emulator (it does the same thing in Monotouch as well).
After several days of troubleshooting, I finally found the source of the crash, but have no idea how to fix it. I suspect it's a bug in Mono somewhere, but don't know how to troubleshoot any further.
My generated proxy has a class defined like so:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name = "ChangeSetEntry", Namespace = "DomainServices")]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(SL5Proxy.ChangeSetEntry[]))]
public partial class ChangeSetEntry : object
{
...
}
(The code above is greatly simplified - there are about a dozen KnownTypeAttribute() lines, but I've determined that the one shown above is the source of the problem. It's important to note that the ChangeSetEntry object does not have any reference to a ChangeSetEntry[] array.)
I found that if I simply comment out the
[System.Runtime.Serialization.KnownTypeAttribute(typeof(SL5Proxy.ChangeSetEntry[]))]
line from the proxy code, everything works 100% correct and my async "completed" handler is called exactly as expected. I don't know why slsvcutil.exe generates the problematic KnownTypeAttribute() line, but it does.
My guess is that there's some kind of infinite recursion that gets created when the ChangeSetEntry class tells the runtime serializer that ChangeSetEntry[] is a known type -- the runtime tries to resolve the known type, comes back to ChangeSetEntry and does it all again.
So I'm curious if anyone else has seen this issue, whether it's a known problem and whether there is a workaround -- perhaps some way to suppress the generation of the KnownTypeAttribute() lines from slsvcutil.exe

Internal fault in MS auto-generated method of WCF

I have a problem with WCF. My testing code is pretty simple.
I call a service layer method on my server from my silverlight application and print the result in a textbox.
Everything of this is surrounded by try-catch.
When my service layer method simply returns a constantly defined string there seems to be no problems - however as soon as it calls a more complex method it fails.
While debugging it does not even reach the complex model method; it fails before that inside some auto-generated code from microsoft:
/WuSIQ.jpg
As the error message "NotFound" is not exactly the most helpful or specific you can imagine my trouble googling for hints.
I thought maybe the auto-generated code could only send simple data so I made a temporary string and returned that, but this did not help.
I have already: a client access policy, a service reference added, removed duplicate reference in ServiceReferences.ClientConfig and a ServiceLayer.svc.cs.
I am debugging by running from the main window and my breakpoints are picked up.
Anyone?
I had some errors in the server side method that were quickly found after debugging was fixed.
I fixed this, as I said in comments, setting the project to have "Multiple Startup Projects".
Whenever I had troubles with updating the WCF service methods one of these usually solved it all:
1 Delete all bin and obj folders (specifically selecting re-build might do the same).
2 The servicelayer will not succesfully auto-update (but will work!) unless this:
[ServiceContract(Namespace = "")]
... is set to this:
[ServiceContract(Namespace = "YourServiceLayerName")]
3 Right clicking on the servicereference and selecting "update...".
Sometimes it would stop debugging again, but a forced full re-build would return it to normal.
I hope this helps someone.