Prism Module and WCF Injection with dynamic bindings

Topics: Prism v4 - Silverlight 4
May 25, 2011 at 7:12 PM
Edited May 25, 2011 at 7:14 PM

Hi,

Every time I deploy my PRISM application to another server, I have to open the xap file and edit the ServiceReferences.ClientConfig file.

Therefore, I decided to try and use the initparams <param name="initparams" value="servicepoint=Local" /> to pass the Binding name to the App.xaml.cs file.

Then in my module I was registering the service like this:

_container.RegisterType<CodeGeneratorServiceClient>(new InjectionConstructor());

However, I need the instance that is in the _container to know about the Bindind so that it finds the service

I tried different options but to no avail:
---------------------------------------------------
// Create an instance of the service
CodeGeneratorServiceClient proxy = new CodeGeneratorServiceClient("BasicHttpBinding_ICodeGeneratorService_" + (Application.Current).Host.InitParams["servicepoint"]);

_container.RegisterType<CodeGeneratorServiceClient>(new InjectionConstructor(proxy));
or
_container.RegisterType<proxy>(new InjectionConstructor());

Then I tried:
-------------------
_container.RegisterInstance(proxy, new InjectionConstructor())

1 - How do I extract a service instance from the container so that I can set its bindings like this:

BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_ICodeGeneratorService_" + (Application.Current).Host.InitParams["servicepoint"];
proxy.Endpoint.Binding = binding;

2 - How do I change the Bindins of a WCF service dyanamically in the module so that when I deploy it I do not have to change the ServiceReferences.ClientConfig file in xap file?

Please note that the service is injected in the my ViewModels, they need to have the right Bindings set.

public DetailViewViewModel(CodeGeneratorServiceClient ctx)
{
   _ctx = ctx;
}

Cheers

Claudio

Developer
May 26, 2011 at 3:35 PM

Hi Claudio,

If you have trouble using the Resolve method of the Unity Container to retrieve an instance of your auto-generated service proxy, you could create a wrapper class that holds an instance of that service, and could be easily registered with an interface mapping (which would also increase testability, as the wrapper could be mocked, unlike the auto-generated service proxy).

As for how to change the binding of a WCF service dinamically, you might find this article useful:

Dynamically Invoking Web Services... With WCF This Time

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi



May 26, 2011 at 5:14 PM

Hi Guido,

It was very helpful. Thanks.

"you could create a wrapper class that holds an instance of that service, and could be easily registered with an interface mapping (which would also increase testability, as the wrapper could be mocked, unlike the auto-generated service proxy)."

Are you talking about adding a DataService layer and create an Instance of CodeGeneratorServiceClient service inside this class and use this class to call the methods on the service?

1 - How would my ViewModels use this class? Would I inject this class into each ViewModel's parameters like I was injecting the CodeGeneratorServiceClient?

2 - I have another problem that I simply cannot find any articles on line showing how to solve it. Basically, all my code is sitting on my local computer i.e. the Silverlight Client and Web.  From the Silverlight client application I add a service reference to a standard WCF service that is in another project.

When I run this service I have configured cassini to run it using port 2587. Therefore, it simulates it being in another domain since the Silverlight Web is running on port 3346.

I have a ClientPolicyFile.xml in the root of this service given access to everyone so that I can access the WCF service from my Silverlight client application.

The problem is that I will have to put this service on a Server that will be behind a firewall. This is for security reason. The database server and the WCF service will be both behind a firewall.

How will I be able to access this service from the Silverlight client if it is behind a protected firewall?

Cheers

Claudio

 

 

 

Developer
May 26, 2011 at 6:03 PM

Hi Claudio,

By wrapper class, I'm referring to a shared service in your client; that is, just a class that wraps the methods of the generated proxy class, but which implements an interface and can be mocked. Since you would register an interface mapping pointing to an implementation that wraps your proxy's methods, you could obtain a reference to the wrapper in your view models by resolving the instance through the container.

In case you use a firewall in your server, you should add the pertinent rules in your server's firewall to allow the communication in the ports associated to your service. As that is not related to Prism, you might find better support in the WCF forum.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

May 27, 2011 at 9:50 PM

Hi Guido,

Thanks. I will try that.

Cheers

Claudio

Developer
Oct 12, 2011 at 3:59 PM

Hi,

For those concerned with this issue, you could check the following blog post, which provides guidance on how to use WCF services with Prism in Silverlight:

Using WCF services in Prism Silverlight applications

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi