WPF consuming WCF - wpf

I am hosting a WCF service on a server, and I want to consume the service from my WPF application. My WPF application is written in the MVVM design pattern in two assemblies, one containing the view (xaml), and another containing the models and viewmodels. This works great on its own, but I'm having trouble figuring out how to make the view model consume the service. I tried making both the view assembly and the model/viewmodel assembly reference the service, but that didn't work. The code fails when I try to create a new instance of the client proxy class:
WCFServiceClient = new WCFServiceClient(new InstanceContext(this));
in the viewmodel. The exception thrown says:
Could not find default endpoint that references contract 'WCFService.IWCFService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
I know my WCF works because I have other client applications in winforms that are accessing it properly. Any idea on how this should be done?

Related

How to communicate between the viewmodel and model if model is in separate project in WPF client application?

I am new to WPF and Entity Framework. I am planning to follow MVVM pattern in a WPF client application and Entity Framework for model. For security reasons I don't want to include the entity framework reference in the client application. I have the following query on this front.
In the above case which is the best way to communicate between viewmodel and model?
Well if you really want to separate the application layers that hard, you could create a WCF Service, which loads the entities from the data source and passes it to the client.
You can find some information on creating a WCF Service here. For information about consuming such services you should have a look here. But you will find many articles in the WWW.

deploying services to use SQL server in silverlight

I've made a service for my silverlight 4 database (SQL) affairs (VS2010, C# ASP.NET web app), now I'm writing my SQL functions in this service, how can I use this functions? I've read some articles about deploying services but I think my case should not be much complicated, I have a big ASP.net web app with several web pages (I have a server running on winserver2008), it works great, also I have a SL app, what kind of files should I upload for using service? do I need an asmx? how can I create it? should I change anything in web.config?
what is the easiest way to use my service for communicating between SQL and SL? thanks
WCF RIA Services is the way to go. You do not want to bother with old-school ASMX web-services.
You will use an EF model and a DomainContext on the server to handle all database access via CRUD methods (Create, Read, Update & Delete).
On the client you will use the generated Domain Service client to access data.
Notes from my previous RIA post:
My suggestion is to always create RIA Service libraries instead of adding directly to a Silverlight application. Then you can link the Client-side library to any number of Silverlight applications, then link the .Web part of the RIA library to your website to provide the WCF service. Again the key is to migrate the config settings.
It will probably make a lot more sense if you create a new RIA services library project, add your EDM etc, then link the halves to a separate Silverlight app and your new ASP.net website.
Step-by-step:
Create RIA Services Library project by selecting Add New Project. Select Silverlight on the left. Select WCF RIA Services Class Library on the right. I will assume it is called the default name RIAServicesLibrary1 for this example. It will create a Silverlight client library called RIAServicesLibrary1 and a standard .Net library called RIAServicesLibrary1.Web for use by the Web server.
Add your EDMX to the RiaServices.web project. Select Add new item. Select Data on the left. Select ADO.Net Entity Data Model on the right. I will assume it is called the default Model1.edmx for this example. Connect it to your database tables etc.
Build your project so that the next step will find your data model.
Create a Domain Service referencing your EDMX models in your RiaServices.web project. Select Add new item. Select Web on the left. Select Domain Service Class on the right. I will assume it is called DomainService1.cs for this example. Choose your data items from the Add New Domain Service Class popup window by ticking the checkboxes. A set of RIA services objects and methods will be created for each item you select.
Add a reference to the client Ria services library project (RIAServicesLibrary1) to your Silverlight application.
Add a reference to the web RIA services library project (RIAServicesLibrary1.Web) to your hosting web application (e.g. you ASP.Net website).
Copy/merge the various sections in the RIAServicesLibrary1.Web/app.config file into your <webapplication>/web.config file. This will include any connection strings and the module sections.
Build the project again so that the Data Source window will see your new Domain Context data sources.
Use the RIAServicesLibrary1 client object (called DomainService1 in this example) directly from your Silverlight code like this:
DomainService1 client = new DomainService1();
or use the Data Sources window to drag/drop a new grid etc onto a page.
If the Data Sources window is not visible select the "Data" menu then the "Show Data Sources" option.
For more information try this Microsoft link: Using WCF RIA Services

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.

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

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

Where to put configuration information in Silverlight?

I am using prism and have a number of modules. In several of them I am making webservice and wcf calls. I want to be able to configure the information about these services in one place. Should I do this in a resources.resx file? I remember a settings.setting file but that was in a web application.
JD
You can put it in the app.config or you can follow this blog on passing server information to the Silverlight client.
Silverlight Bits&Pieces has this piece on Silverlight configuration options.
When you create service reference in a Silverlight Application, it should automatically generate a ServiceReferences.ClientConfig XML file and put it in your project. It will put the binding and endpoint config info in this file. If you create an instance of your service proxy object without specifying any binding/endpoint it will use the info from the file.
What you can do is the following:
In the Bootstrapper application, add a reference to that WCF
Create your own custom proxy class imlpementing an interface that dictates the methods to be called
Register this IProxy with Unity Container, then pass to each of your ViewModels an IProxy parameter. Unity will do the job by injecting the real instance of that IProxy.
This way you have based all the WCF connection in one place.
Does it help?
Regards

Resources