0

I am working on a project requiring communication between the Presentation layer (MVVM based client) and the Business layer. The application is to be installed on a single machine, and as such could have been executed using a .net remoting based approach. However, I have been suggested to use WCF since .net remoting is deprecated and WCF is the way to go.

So I implemented WCF Service as a WCF library project. I added a service reference (using visual studio tool by discovering services in the solution), which was successfully added on the client side. All works well, since during debug session visual studio launches the service library and the client can connect to it successfully.

Now Since the client and service host will be installed on same machine, I was thinking of using named pipes transport and self hosting for the WCF service. Here is where this gets very confusing to me:-

  1. Since the MVVM client is the "main" app (since it is the application frontend), the client will be launched first. I am unable to come up with a solution to launch the service host "on-demand" when the client needs the same.
  2. What are the solution I can use to do what I require for 1? I am not sure of using a continuous service through windows service for something that will be required "on-demand".
  3. Please suggest clean approach to implement a way to launch "on-demand" service. Thanks in advance.

1 Answers1

0

I'm not quite following you here. MVVM is cake and really doesn't have anything to do with your problem. Using a servicebased architecture is a must today, I just want to smack every old guy/gal sticking to direct SQL on the client side, and don't see the dangers with that.

Well anyhow, you may solve this different ways. WCF is BIG, to big in my opinion. One way to use it, common on small applications like apps(WP8.1, 8.1 apps++), is just to connect, call the method you need and the close the connection. Case solved, given that I understand your needs. The otherway is to keep the session alive for each service... (ugh, loadbalancing etc).

I've been working on large LOB applications the last 4 years my self, what I can tell, is that if I were in charge, I would have done it very differently. For once I would used JSON endpoints with SSL over HTTP, and the json.net serializer. The datacontract serializer which is the default in WCF, is not good news at all. JSON allows easy communication with JavaScript based applications, and the serializer doesn't break like porcelain as the datacontract serializer does. Address/baseaddress may be stored in your config file, so it may be changed upon deployment(you probably have many servers, for different environments).

This is a really old post covering the subject(supporting SOAP aside JSON); REST / SOAP endpoints for a WCF service

Don't be stupid and just call your services directly now! Use the interface(Wrap if necessary) and feed it to your viewmodels for proper TDD! It will also allow you to completely replace WCF with another form of communication.

There also are alternatives to WCF.

IIS? Hosting WCF in IIS rather than a proper service? No way, flush that idea down the waterloo ;) (internal joke)

EDIT:

BTW: Your service must ofc already be running for your client to connect to it! Or nothing will be listening to your configured port. If you are selfhosting, you can use parameters to start in debug mode, that is start your service like a regular application you may debug. In Program.cs;

if (args.Length > 0 && String.Equals(args[0],"debugmode", StringComparison.OrdinalIgnoreCase)
{                    
    Service1.Create(); // Debugging! 
}
else
{
    // Hosting
    service = new Service1{ServiceName="YourService"};
    ServiceBase.Run(new ServiceBase[] {service};);
}

Hope it helps,

Cheers,

Stian

Community
  • 1
  • 1
Stígandr
  • 2,874
  • 21
  • 36
  • Stain,Thanks for the reply. My main doubt is this - the UI client is first instantiated by the user (this is a thick desktop client for Win 7, Win 8). During the operation, the UI client is going to call the business logic (this is part of business layer, which is behind the service layer). How do I invoke the service in this case? Sangram P. – user3905515 Aug 04 '14 at 08:49
  • The application alongwith the service will reside on the desktop of the user. – user3905515 Aug 04 '14 at 08:55
  • @user3905515 I really hope that's only when you are developing, which it most likely is. *All* people do that. :) I always have two 2013.1 running, one withthe frontend and one with the backend. Baseaddresses are changed upon deployment ofc. – Stígandr Aug 04 '14 at 08:57
  • you suggesting to use a Windows Service that will be "listening" to the client request, then when a request is made, spawn a process to serve the request? Can you please elaborate? – user3905515 Aug 04 '14 at 09:00
  • @user3905515 Yes, it will listen to request on the port you configure to listen on. Create your methods on the backend. Open the connection one the client, call your method, pref async, then close it. If you must keep you connection alive, you connect once pr service. Sorry I don't have the time to post code for this right now, but I'm sure you will find alot examples here and on msdn. – Stígandr Aug 04 '14 at 09:04
  • Thanks a lot. I do understand. I will work on the same. I really appreciate the help. – user3905515 Aug 04 '14 at 09:06