What is the starting point for a WCF Library app? - wcf

Where is the best place to put code for when a WCF service is just started up? Similar to Page_Load in a web application.

The service implementation constructor would be the obvious place but it also depends on your hosting model
1) run once code: IIS hosting = global.asax; self-hosting = Main / OnStart
2) run once per service instance: constructor of service class
3) Something more exotic: implement IInstanceProvider

In the class constructor of the service implementation.

Honestly the best place is the constructor and/or static constructor for your service class (for static members). There is no other WCF specific lifecycle event that will notify your service implementation when it's being created by the service host.
Now, if you're talking about the creation of services generically, that would mean you're willing to hook into the WCF runtime and there you can do things like get in the middle of the instance creation with a custom IInstanceProvider implementation.

Related

Service Bus in ASP.NET Core

I would like my ASP.NET Core application to send messages to a Azure Service Bus.
In Microsoft's article Best Practices for performance improvements using Service Bus Messaging they argue that you should re-use instances of clients.
It is recommended that you do not close messaging factories or queue, topic, and subscription clients after you send a message, and then re-create them when you send the next message.
So I take that as I should not instantiate a new instance of the client (TopicClient or QueueClient) inside my controller using the new keyword.
I guess that I should use dependency injection in ASP.NET Core.
Should I directly inject a TopicClient/QueueClient or should I create an own class that wraps an instance of the client and expose a SendAsync method?
When registering the service with dependency injector should I register it as a singleton?
We did it with a wrapper class that is then returning the TopicClient/QueueClient and registered it as a singleton and found no big issues with this approach.
We based our approach on this example provided by Microsoft eshopOnContainers.
The example code for this functionality is found in this file. They than register this class as singleton in Startup.cs in the services where they require ServiceBus.

What is the entry method for WCF service hosted on IIS?

A little background info -
I'm trying to host a RESTful WCF service on Azure. As I understand, unless I have the ASP.NET type hosting on the role, I don't really need the global.asax class (which has the application_start method).
From basic prototyping, all I needed was the svc file and the implementation behind it and it automatically gets initialized on role startup (I mean, hosted up on IIS).This is great because I need no extra code other than web.config and my service is up and running. I don't need to create a new service host and start listening on it, etc. I'm able to deploy the role and POST messages to my service.
The problem -
I have custom logging and initialization classes implemented that I need to initialize when my service starts up. I configured my service to be a singleton and I'm not sure where I should put my custom initialization components.
Without an explicit application start method and my service configured as a singleton, can I assume that when the first request comes in, my service constructor gets called? (along with all my custom initialization?).
can I assume that when the first request comes in, my service constructor gets called?
Yes, but you should ask yourself whether you really want your service to run as a singleton. If you're happy with this then it will work fine; if you don't want it to run as a singleton then you should look into Russell's answer using a custom factory.
Look at Should WCF service typically be singleton or not? for some discussion about whether WCF services should be singletons. You need to decide for your situation, but generally WCF services are not singletons unless they need to be.
To implement a custom factory, see this MSDN link Extending Hosting Using ServiceHostFactory. As the link describes, extend the service host factory like so
public class DerivedFactory : ServiceHostFactory
{
public override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
{
return new ServiceHost(t, baseAddresses )
}
}
And then specify your factory in the ServiceHost directive
<% # ServiceHost
Service="MyNamespace.MyService"
Factory="MyNamespace.DerivedFactory" %>
You're looking for ServiceHostFactory. You can add a part to the SVC file to use a factory, where you can do any logging etc. you may need.
I have used this in the past to start a background worker to launch a separate thread for some background work.
http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.servicehostfactory.aspx
Hope this helps you get where you need to be. :)

Workflow in existing WCF

I have an existing WCF service.
Is it possible to add operation contract to the service interface and have the implementation in a workflow?
Or I absolutly need a seperate service interface for my workflow?
When you publish a WCF service you are publishing the interface and telling it what implementation to use. You can specify only one implementation, otherwise how would WCF know where to route which request. So in short you need to use a separate interface for your workflow services. That said, if you don't want to change your public facing API there is no reason you can't create a minimal implementation that just passes request on to your worklflow service.

WCF PerCall instance server and Dependency Injection using Prism?

If I have a client/server type of application built using both Prism and WCF, and I would like on the serverside to have the wcf service to be instatiated per call BUT I would like to use dependency injection (using the UnityContainer in Prism). How could I possibly do this? Should I have a single instance service it would be no problem, but are there any hooks in WCF to allow for a delegate to be called whenever a service is supposed to be instantiated and have this instance returned?
Any ideas?
Cheers!
It is possible! WCF provides a plugin to the WCF responsibility chain called an IInstanceProvider. This allows for you to replace the construction with your own.
Someone has implemented this as a service behavior and is available here:
http://code.msdn.microsoft.com/WCFResources/Release/ProjectReleases.aspx?ReleaseId=1252

What is meant by hosting a wcf service?

I want to know what happens when I create an instance of a ServiceHost class?
What it does ?
The ServiceHost (whether instantiated by yourself directly, or whether you delegate that job to IIS/WAS) is the runtime environment for your WCF class - which is just a simple .NET class after all (which needs to run somewhere).
The ServiceHost basically provides all the "plumbing" around your WCF service - creating the endpoints and the listeners on those endpoints to listen for messages and catch those as they come in; it provides the whole channel stack from the transport level through all the layers of WCF up through deserializers on to the dispatcher which then decides which class and which method on that class to call, and so on.
In WCF, in your service class, you write only the actual busienss logic of your service - the ServiceHost and all its classes around it handle all the nitty-gritty details of the receiving of messages and sending back of responses etc.
Marc
Typically the ServiceHost class is used to host your WCF services in a standalone app (such as a console app), if you are not using IIS or Windows Activation Service (WAS).
In simple terms it will deal with the COMs (listening for messages for a particular service).
You can also derive from ServiceHost to add customization if required, in combination with a specialization of ServiceHostFactory.
See MSDN example.
HTH
Phil'
It creates channels that are responsible for things like reliable transfer and security. It listens for incoming messages and calls your operation methods.