WCF solution needed for a service class - wcf

I have a class raises some events and do operations. Eg
class MyService
{
EventHandler MessageSent;
EventHandler StatusReceived;
public void StartService(Serviceconfig configObject)
{
//Intialize serial port with OnSerialPortReceived event handler.
}
public void GetStatusForMessage(string messageID)
{
}
public void OnSerialPortReceived(string data)
{
if(data=="123")
MessageSent(this,null);
if(data=="456")
StatusSent(this,null);
}
}
This is a console application, it will be started when the system is started. Now we need a monitor application(basically a client with call back) when some event triggered on the event service, for that we should use WCF. and also the monitor application makes call to the service class.In the above eg GetStatusForMessage method will be invoked by the monitor application. So now how can we implement using WCF. If make the above class as service with the service contract, it wont be initialized and started until the client has initiate a call. This class object will be initialized and starts its function when ever the system restarts.
I found this article http://msdn.microsoft.com/en-us/magazine/cc163537.aspx. With this approach my service will become as publisher client and monitor application will become as subscriber client application. But the client has to make calls to the service class. So my client application should support both callbacks and also it should be able to call the service methods. How can I achieve this using WCF? Note the service class which is monitoring for events is a single instance and initialized when the application started.
Hope I ll get solutions for this. Please let me know for more clarifications.

Don't try to make your service classa WCF service. Make it a singleton and have WCF talk to it.
If you want the events to fire "events" to the monitoring application you will need to use a duplex binding (NetTcpBinding if cross machine or NetNamedPipeBinding on the same machine would be my recommendation). When the monitoring application connects save its callback channel and in the method wired up to the events call back on the callback channel.
Note you will have to keep sessions alive on both sides so the monitoring application and the service will have to fire something to each other more regularly than the configured receiveTimeout (10 minutes by default) but this can simply be a "ping" method to use as a keep-alive
I blogged about duplex communication a while back if it helps

Make your "serivce" a Widnows Service not a console app.
You can make your MyService class a WCF service without any problems. But You can also create some other class to host Your service contract and simply communicate with your windows service implementation.
There is no connection between windows service activation time and first WCF request ( this in not IIS, this is a self hosted WCF service, you start it when you want to).
Here's link to self hosting WCF service tutorial
Install your windows service on your machine with autorun option.

Related

WCF in UWP: should I explicitly call the OpenAsync() of the service client(proxy)?

I'm developing a UWP client project which need to consume some services of a WCF server. I uses the "add service reference" tool of Visual Studio to auto generate service clients(proxies). The binding type is NetTcpBinding. Below is some code snippet which create the service client:
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
MainServiceClient = new MainServiceClient(tcpBinding, new EndpointAddress("net.tcp://localhost:8773/MyWCF/MainService/tcp"));
The question is do I need to call OpenAsync() method of MainServiceClient? It seems the service client can be auto opened when it is first called. But I read from this article that auto-opened service client would have some performance penalty. The article was written in 2007. I just wonder if this mechanism have changed today, especially in UWP project. Can anyone share more light on this topic? Thanks!
To explain this case, you should know three ways to do WCF instance management. WCF has provided three ways by which you can control WCF service instances:Per call, Per session, Single instance.
When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client.
Very often we need to maintain state between method calls or for a particular session. For those kinds of scenarios, we will need to configure the service per session. In per session, only one instance of a WCF service object is created for a session interaction.
Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single instance mode.
And there are three ways by which you can handle concurrency for each service instance in WCF :single, multiple, and reentrant.
Single: A single request has access to the WCF service object at a given moment of time. So only one request will be processed at any given moment of time. The other requests have to wait until the request processed by the WCF service is completed.
Multiple: In this scenario, multiple requests can be handled by the WCF service object at any given moment of time. In other words, requests are processed at the same time by spawning multiple threads on the WCF server object. So you have great throughput here but you need to ensure concurrency issues related to WCF server objects.
Reentrant: A single request thread has access to the WCF service object, but the thread can exit the WCF service to call another WCF service or can also call a WCF client through callback and reenter without deadlock.
In "Instance mode = Per Session and Concurrency = Single" combination, one WCF service instance is created for every WCF client session because the WCF instance mode is set to per session. All the method are executed in a sequential manner one by one. In other words, only one thread is available for all method calls for a particular service instance.
For the above scenario, you should always open WCF client proxy explicitly before you are making any calls. Because it will maintain service state between method calls and obtain high performance.
For more detail you could refer to "WCF Concurrency (Single, Multiple, and Reentrant) and Throttling" and "Three ways to do WCF instance management".

WCF service hosted in Windows service

I have a WCF service hosted in a Windows service. When user modifies the WCF service configuration,he needs to restart the service.
I wanted to know if restarting the windows service is better by using
serviceController.stop()
servicecontroller.start()
or by creating a new instance of the WCF client every time he wants to restart it. No information will be lost if created a new instance of the WCF client.
In your service container which is inherited from System.ServiceProcess.ServiceBase
you should start your service inside method
protected override void OnStart(string[] args)
{
servicecontroller.start()
}
and stop your services inside method
protected override void OnStop()
{
//here clean up code or any tear-down necessary to stop your service.
serviceController.stop()
}
so these methods are called automatically when you start/stop windows service from services pallet.
As others said there is no effect of creating a new instance of the WCF client every time on your service

WWF service - how do I make a service to be asyncronous?

I want to create a service which receives a request from the client, adds the request to a database and than calls another WWF service ASYNCRONOUS which does some time consuming job withthe data from the database.
How do I make a service to be asincronous in Windows Workflow service?
I use the second Windows Workflow service as a queue(as it can only be one instance of this service=I set canCreateInstance to false).
To make a Workflow Service behave asynchronously create a One Way contract by using a Receive activity without a correlated SendReply.
When another Workflow (or WCF client proxy) calls this service it will not wait for a reply from the service.
As for your comment about only one instance of a service you are mistaken. There is no way to have a singleton workflow service (as there is with WCF services) and CanCreateInstance has no effect on this behavior.

How to make IIS wait for WCF service gets ready?

I have a WCF service hosted in IIS 7. It takes some minutes until this service finish to load its data and ready for external calls. Data loads in internal thread. The problem is that from IIS point the service is ready just after it was activated (by some call), and it process a request without waiting for data to be loaded.
Is it possible to tell IIS that the service is still loading and make this service unavailable for requests? No problem if such request will throw an exception.
You could invoke the initialization logic synchronously in the service's default constructor. The service operations won't be invoked until the service instance has been created, which will only happen after the initialization has completed. In the meantime clients simply won't receive a response.
Here's a quick example:
public class MyService : IMyService
{
public MyService()
{
// Blocking call that initializes
// the service instance
this.Initialize();
}
public void GetData()
{
// The service operation will be invoked
// after the service instance has been created
// at which point the initialization is complete
}
private void Initialize()
{
// Initialization logic
}
}
If the initialization logic is expensive, you should consider making your service run as a singleton, so that the price is paid only at the first request. Alternatively you could store the data loaded during initialization in a centralized cache. This way it can be made available to multiple service instances while still having to load it only once.
If the initialization logic is shared across multiple services, you should consider implementing it once in a custom ServiceHost by overriding the OnOpening method. Since you're hosting your services in IIS, you would then also need to implement a custom ServiceHostFactory to create instances of your ServiceHost.You can read more about this approach in this MSDN article.
Found this new feature described in "ASP.NET 4 and Visual Studio 2010 Web Development Overview": http://www.asp.net/LEARN/whitepapers/aspnet4/#0.2__Toc253429241
The problem that it's requires IIS 7.5 running on Windows Server 2008 R2, but works with ASP.NET 2.0+

how to selfhost wcf without iis

Reading up on WCF we have self hosting option available , one limitation here is we have to manage the host process lifecycle ourselves. What I am exploring here is to run the service without IIS and do a self hosting.
Few things come to mind
- How will request management work here. In case of IIS it manages the request and give control to dotnet on a particular thread. In absence of IIS do we need to write code ourselves to manage incoming requests ( say on a tcp port ) or WCF provides some classes to manage request and spawn threads to process each thread.
I am aware that in case of self hosting this needs to be a windows service. In case of self hosting how can me tap on the number of simultaneous requests on the sever , it can be managed by limiting the thread pool ? or we can configure this via wcf ?
Thanks
dc
Self-hosting does not require a Windows service. You can self-host inside a console application if you so desire. It's just that Windows services are a good solution for self-hosting if you require 24/7 access but do not want to, for whatever reason, use IIS.
Managing the lifecycle of the host process is not a big deal. I use a Windows service to host a WCF service. I simply start my WCF service in the OnStart() callback of my Windows service, like so:
private ServiceHost _host;
protected override void OnStart(string[] args)
{
_host = new ServiceHost(typeof(CalculatorService));
_host.Open();
}
Likewise, I close the WCF service in the OnStop() callback of my Windows service:
protected override void OnStop()
{
if (_host != null) _host.Close();
}
This effectively ties the lifecycle of the WCF service to the lifetime of the Windows service. You could do something similar in any kind of application - console, Windows Forms app, etc. For example, in the OnLoad() callback of your Windows Forms app, start the ServiceHost for your WCF service and close it when exiting the app. Simple enough.
WCF gives you a lot of flexibility on how to handle incoming requests. For example, you could make your WCF service a singleton, which means that you'll have one and only one instance of your WCF service. In this case, all incoming requests are handled by this one instance. But you can also have your WCF service handle each incoming request with a new instance of your WCF service. This allows your service to scale better, but will likely require you to synchronize any access to your backend data storage, e.g., database. You can control this behavior using the InstanceContextMode property of the ServiceBehaviorAttribute on your WCF service.
As I read your question again, it sounds like you're just learning WCF, so I hope none of this has overwhelmed you. Check out my answer to this SO question for some links that you may find helpful.
To answer your specific question, the WCF hosting infrastructure will spin up a HTTP listener that works with HTTP.SYS (the same thing IIS uses) which will listen for traffic on the specific port/address you configure.
For any other questions, I'm sure this section in MSDN will answer them.