Open Menu

Synergex Blog


Using WCF Services in Synergy .NET Applications

By Steve Ives, Posted on January 11, 2012 at 2:37 pm

Apparently there are some people actually reading this BLOG!!! And … it turns out that quite a few of them have been working through my earlier series of posts related to using WCF. Excellent!

It occurred to me that since I wrote the last of those articles we have had a release of a new version, namely Synergy/DE 9.5.3. And that new version included some important new functionality related to consuming WCF services in Synergy .NET applications.

In previous posts I had to talk about the fact that “Add Service Reference” was not implemented in Synergy .NET, and I demonstrated how you could use the svcutil.exe command line utility to generate client proxy code and configuration information, both of which could then be manually added to a Synergy .NET project.

Good news … in 9.5.3 the Add Service Reference wizard has been implemented in all Synergy project types. I’m not going to go into great detail about how to use this new functionality … because it’s exactly the same as in C# and VB projects, and pretty well documented elsewhere. But briefly, in a Synergy .NET project:

1. Right-click on the project in solution explorer and select “Add Service Reference”.

image

2. Enter the URI of your service and click the “Go” button … or … if the service is in the same solution, click the “Discover” button.

3. Enter the name of the namespace that you wish the generated proxy classes to be placed in.

4. Optionally click the “Advanced…” button and select advanced options. For example, it is common to set “Collection Type” to something a little nicer than System.Array, and very often you’ll want to enable “Generate asynchronous operations” in order to help you build responsive and flexible applications.

image

5. Finally click the “OK” button in the first dialog to generate the service reference within your project.

imageWhen you add a service reference to a project, several things happen:

  • The svcutil.exe utility is used to download the WSDL information from the service, and to generate client proxy classes for the service based on the content of the WSDL.
  • The resulting generated files are added into the project. By default these files are hidden beneath a single node in Solution explorer which represents the service reference, but you can look at all of the files by enabling the “Show All Files” option in solution explorer.
  • Depending on the type of project you are working in, references to several additional assemblies may be added. These referenced assemblies can include System.Runtime.Serialization, System.ServiceModel, and System.Xml.

So … finally … no more manual use of svcutil.exe is required in order to consume WCF services Synergy .NET applications. Thanks development Smile.


De-Serializing Classes containing Synergy Types

By Steve Ives, Posted on at 2:10 pm

A customer recently reported a strange problem that they were seeing when attempting to use WCF. The problem seemed to occur when they attempted to call a method in their WCF service, where that method had a parameter used to send a class TO the service, and where that class included the use of Synergy types (a struct field containing alpha data in this case). By the way, these Synergy types were internal within the class; the external interface of the class was totally CLS compliant … but that didn’t seem to matter.

The actual problem was that the customer was seeing was “null reference” exceptions at runtime. The error seemed to occur when the setter methods of the servers data class were being called. Now, in order to understand the cause of the problem, it might help to take a step back and thing about what is happening under the hood in this type of scenario.

When you add a “Service Reference” in a client application, Visual Studio (or the svcutil.exe utility) downloads the WSDL information that defines the service, analyzes it, and then creates local “proxy” classes for the various classes that are required in order to interact with the service. When a client application needs to call a method that accepts an object as a parameter, it instantiates a copy of the local proxy class and passes that object as a parameter to the method. WCF then serializes that data (to XML or some other format suitable for transmission over the wire) and calls the method. On the server, the data is received and then de-serialized back into an instance of the original server-based data class; all of the properties being set to the various values that were received over the wire.

As it turns out, when WCF performs this de-serialization and creates an instance of a server data object to pass to a method, it does so in such a way that the default constructor of the data class is not called. And herein lies the problem.

In Synergy .NET all classes contain an implicit default constructor. This constructor, amongst other things, is responsible for the instantiation of any Synergy types present in the class. But because WCF circumvents this mechanism, the Synergy types are never instantiated, and hence the null reference errors.

Right now a little bit of head-scratching is taking place in development, and I’m sure that at some point soon a proper resolution to this problem will emerge … but in the meantime we need a workaround, which thankfully (thanks Jeff) turns out to be relatively straight forward:

Code Snippet
.region "De-serialization workaround"

private static myConstructor, @System.Reflection.ConstructorInfo
        
{OnDeserializing}
public method OnDeserializing, void
    context, StreamingContext
proc
    if (myConstructor==^null)
        myConstructor = this.GetType().GetConstructor(new Type[0])
    myConstructor.Invoke(this, new Object[0])
endmethod

.endregion

 

This is the code for a method named OnDeserializing, and you will notice that the method is preceded by an attribute named {OnDeserializing}. This attribute will cause the method to be called whenever an instance of the class is created during a de-serialization operation. Within the method, reflection is used to get a handle on the default constructor for the class, which is then explicitly called. The handle to the constructor is stored in a static variable, so that the code that obtains the handle will only execute once, the first time the method executes. The method then explicitly calls the constructor, which in turn instantiates any Synergy types which may be present in the object.

So … for now … if you want to use a Synergy data class as part of a WCF service, and that class will be used for IN or INOUT method parameters, you need to include this section of code in the class.


Don't miss a post!

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts Tag Cloud Archives