How to consume an existing WCF service from my Silverlight app? - silverlight

Im porting an "old" silverlight 3 app that used WS* webservices to connect. These services have been changed and are now replaced by an WCF "equivalent". I cant seem to get my silverlight app to work with the WCF service, I add a reference to the: http.....svc and create my client, but the clientConfiguration is empty. The silverlight app doesnt know the datatypes given in the WCF service... all this is working well in the WPF client also using them...
All i seem to get on one of my types from the WCF service is a public variable called "data"... anyone know what this is?
Sincerely,

WCF for silverlight support a subset of bindings for normal WCF. As the time of siverlight4, we support basic http binding, nettcp binding and pollingduplex binding. For nettcp binding, it has some port restriction and requires you setup some policy server. You can find more details on http://msdn.microsoft.com/en-us/library/cc896571(VS.95).aspx
HTH

Related

Service Model in Silverlight

What is the best model for using services in a silverlight application?
I usually add a wcf service to my ASP.net app and add a service reference to my silverlight application, but anytime I change my service I should update the service reference in silverlight app.
Is there any better and more professional way?
If you use WCF RIA Service instead of a plain WCF service, it will automatically be updated whenever something on the service end changes.
If you continue to use plain WCF, you will need to update the service anything you change something.
You can also write your own model and expose them to the silverlight client, just like WCF. Everything is almost the same, except ria services was built for this purpose and more.

Silverlight 4 and WCF

I have a WCF service (using HTTP binding) that our WinForm and WFP apps can use with no problem. We have a Silverlight 4 developer who is saying he's not able to use the WCF service because it needs to be "reconfigured" for Silverlight use. I don't know if this is true or not, because I don't know anything about Silverlight.
So, my question is this. Should a WCF service, that uses HTTP binding, that is successfully being used by our WinForms and WPF applications, also be able to be consumed by Silverlight apps, without modifying or reconfiguring the WCF service?
Silverlight 4 supports the following bindings:
BasicHttpBinding
PollingDuplexHttpBinding
CustomBinding (with a subset of binding elements)
Silverlight and WCF Feature Comparison:
There are several ways to use the WCF
client stack to access a service.
It is used when invoking services
through automatically generated
proxies, as described in How to:
Access a Service from Silverlight. The
proxy must be generated in this
procedure by using the Add Service
Reference tool in Visual Studio 2010
for the Silverlight project.
Caution:
Proxies generated by using the WCF
Svcutil.exe tool will not work in
Silverlight 4. Use the SLsvcutil.exe
tool instead. For more information
about using this Silverlight tool, see
Using SLsvcUtil.exe to Access a
Service.
If you already have a Windows
Communication Foundation (WCF) service
contract definition, you can use the
generic ChannelFactory class without
having to generate a proxy. This might
be the case, for example, if your
client is sharing service definition
code with the project on the server.
In this case you could, for example,
define a service contract interface
IStockQuoteService and then use the
same IStockQuoteService code in both
the server and Silverlight projects.
This would involve using a
ChannelFactory.
This advanced technique is
demonstrated in How to: Call
Operations Asynchronously Using a
Channel Factory and in the Silverlight
4 context with Building and Accessing
Duplex Services. For more information
about defining and using service
contracts, see Designing Service
Contracts.

Custom hosting and "Silverlight-enabled WCF service"

I'd like my WP7 to be able to talk to a service on my local computer. For various reasons it would be more convenient if this service was hosted in a custom host, i.e. an app running locally. However, if I understand correctly "Silverlight-enabled WCF service" need asp.net compatibility, therefore must be hosted in IIS, is this correct?
If so, is there anyway to work round this and get a "Silverlight-enabled WCF service" running in a custom host?
The Silverlight-enabled WCF Service is just a simplified item template from a regular WCF service. It was originally added because the default binding for WCF services wasn't supported in Silverlight, and caused a lot of angst for new Silverlight developers. You should be able to use the regular WCF item in a regular project (e.g. console app) and you'll just have to adjust the bindings.
(Also of note is that the Silverlight-enabled WCF template uses a single code file instead of separating out the service interface from the class implementation.)

Stuck with MVVM Architecture Problem

I am building a silverlight application using MVVM pattern but I don't understand what I am missing here...
ViewsProject (Silverlight Application) contains user controls and page. References ViewModelsProject.
ViewModelsProject (Silverlight class library) contains backend code of views. Also the business logic will reside in here. It will reference Models project.
ModelsProject (Windows class library) contains: Ado.net entity model of a local database and a WCF RIA DomainService built on the entity model. It's purpose is to get data from the local database and the entities are exposed via domain service.
Web project: Contains xap of ViewsProject.
Have I done everything correct so far? If yes then please help me understand:
How do I call the methods of the
domain service in the ViewsModel
project? If I reference the
ModelsProject, I get the error that
only silverlight projects can be
added.
If I change ModelsProject to a
silverlight class library project
then I can't add the entity model of
my database.
For client projects to use WCF RIA Service you need to set WCF RIA Services Link in project property.
As for modularity and separation in project architecture when using WCF RIA Services,
you can use WCF RIA Class libraries.
There isn't much sense in separating your views from your viewmodels IMHO. I've seen this approach used before but the view model (in my mind at least) represents a more 'view appropriate' abstraction of the data than the domain model can offer. The ViewModel provides a layer of indirection to allow the presentation to change without the model doing the same but it's little more than a sort of Adapter/Controller hybrid and lives alongside the view.
To answer your question though...
Typically, you'll have your SL project call your domain service (a web service usually) via commanding from your view to the viewmodel. The viewmodel command will then directly or indirectly make the web service call. This web service does not need to be a SL class library since it sits on the web server away from your client.
I get the feeling your thinking in terms of a desktop app here. Remember that the SL project runs client side and your data is not there. To wire all this up your gonna need some remote service to get the data from. A regular library reference is no good since the data is not on the client.
HTH, Stimul8d
I worked out the answer.
Change ViewModelsProject (Silverlight class library) to Silverlight Application type.
Check Enable Ria services during creation of the project.
In the view model class, add using ModelsProject.
Compile the ViewModelsProject.
All of the methods of the WCF Ria service will then appear in the code.

Is it reasonable to have both DomianService and Silverlight-enabled WCF Service

At the moment it seems that a RIA Domain Services class can not return a sinlge custom type.
There are parts of my app that needs just that, so think the only way to get that is to call back to a simple silverlight-enabled WCF Service, which i beleive will not have a problem returning a custom type. I don't see where the Silverlight client app has any sort of reference to the Ria Services. How do I set up both a Ria services and WCF service on the silverlight client. Will the WCF service be "aware" of the authenication already in place with the RIA services?
This is one of my biggest bugbears of RIA Services :). The good thing is that they are in the process of enabling complex types to be returned from invoke methods (according to the user voice site for RIA Services). However, that won't help you now :).
The best way to implement your requirement to return complex objects is indeed to set up a standard WCF Service to return complex types when required. You do so in the normal way that you would expose a WCF in any web project. You then consume it in your Silverlight application in much the same way you would a WCF Service in any other type of project (by adding it as a service reference). The main difference when consuming it (compared to other types of projects) is that Silverlight only supports asynchronous calls. One difference to be aware of from RIA Services is that whereas RIA Services automatically determines the URI of the domain services (based upon where the XAP file originated from), you will need to handle that aspect yourself (very important when you try to deploy your application :). Do this either by setting the URI in the app.config file, or use the method I use in the code accompanying this article here: http://www.silverlightshow.net/items/Building-a-Framework-for-Silverlight-Line-Of-Business-Applications.aspx.
In regards to your final question, yes, RIA Services uses standard ASP.NET authentication, so the cookie that it uses to maintain your authentication status is also used by any WCF Services in the same project. I talk a bit about security with WCF Services in this article if you want more information: http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Part-3.aspx.
Hope this helps...
Chris

Resources