Understanding WcfIISHost Class Library in a Project - wcf

I am investigating a web project and I have encountered a class library. Its name is "UnitName.SampleProject.WcfIISHost" and it only has two files, one is "CategoryService.svc" and "NinjectFileLessServiceHostFactory.cs". And weirdly, "CategoryService.svc" has only one row, as such:
<%# ServiceHost Language="C#" Debug="true" Service="UnitName.SampleProject.BusinessLayer.Concrete.ProductManagement.CategoryService" Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" %>
and content of "NinjectFileLessServiceHostFactory.cs". is:
public class NinjectFileLessServiceHostFactory : NinjectServiceHostFactory
{
public NinjectFileLessServiceHostFactory()
{
var kernel = new StandardKernel(new BusinessModule());
kernel.Bind<ServiceHost>().To<NinjectServiceHost>();
SetKernel(kernel);
}
}
There are a Wcf library and Wcf Console host libraries out there and I understand them. What could the purpose of this library?
Thanks in advance.

In short, due to changes in the hosting environment, IIS hosted WCF is different from console hosted WCF, in the console, we can add additional logic, add additional services, and endpoint behavior before the service is started through ServiceHost event, or initialize the database and so on.
Because IIS does not instantiate Service host, events linked to the service lifecycle are all implemented through the Factory property.
Here is a detailed explanation.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/extending-hosting-using-servicehostfactory
NinjectServiceHostFactory extension adds support for Dedenpency injection for WCF service and behaviors.
https://github.com/ninject/Ninject.Extensions.Wcf
Feel free to let me know If there is anything I can help with.
 

Related

How to do dependency injection in a WCF application

I have an IIS-hosted WCF application. Right now the service has this constructor:
public ClassService()
: this(new ClassRepository())
{
}
public ClassService(IClassRepository repository)
{
_Repository = repository;
}
The parameterless constructor is because WCF requires you to have a parameterless constructor when generating service proxies. Now, when I have the service proxy in the UI assembly, the constructor with the IClassRepository is not present so I can't inject an implementation of the repository.
How is this commonly done? One idea I have is that injection would take place not in the UI but in the Service but I am not sure if this would have some repercussions or just plain bad. Can someone give me some ideas?
What you need to do is implement a WCF InstanceProvider, delegating creation requests to your container of choice. Once you have your instance provider coded, you can install it in a ServiceHost by implementing a WCF service behavior. The service behavior, in turn, is installed by adding it to the collection ServiceHostBase.Description.Behaviors.
Here's an MSDN Magazine article on WCF extensibility.

Composition root for a singleton WCF service

I am usually working with WCF services that uses single instance context mode. Also service contract and behavior is in a WCF service Library.
If the service is hosted as a Windows Service or a Web service, they get their own projects that references the WCF service library. I like to keep this WCF service library running (for simply debugging with WCF Test Client) but it requires too much effort when I try to resolve dependencies through the behavior class.
Using Castle Windsor, I also have to make additional configuration for Castle Windsor WCF Integration Facility. I am just trying to resolve the object graph on service startup and I feel like it shouldn't be this hard.
I thought, since behavior instance is created only once I should be able to use it as the composition root. I can provide all dependencies using a single object and resolve it from the container like this:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class SampleService : ISampleService, IDisposable
{
private readonly IServiceManager _serviceManager;
private readonly IWindsorContainer _container;
public SampleService()
{
_container = new WindsorContainer();
_container.Install(new ServiceInstaller());
_serviceManager = _container.Resolve<IServiceManager>();
}
public string GetMessage()
{
return _serviceManager.GetMessage();
}
public void Dispose()
{
_container.Dispose();
}
}
I know it's a bad idea to explicitly ask the container to resolve a dependency but this saves me from so much trouble. I get to keep the default WCF configuration, I can run the WCF service library for debugging since I have a default constructor now. I also don't have to use Castle Windsor WCF integration Facility which was missing documentation last time I checked.
I guess Castle Windsor WCF integration Facility offers more features but I just want to resolve the dependencies. Is this approach likely to cause some problems? I haven't seen anyone do it like this so I would like to know if this would be legitimate use.

how to use latest castle windsor wcf integration facility

I'm playing around with Castle WCF integration facility because I want to integrate my WCF services with windsor IOC.
The issue I'm facing is that I can't set IKernel for DefaultServiceHostFactory.
I setup the container:
container = new WindsorContainer().AddFacility<WcfFacility>();
container.Register(Component.For<DefaultServiceHostFactory>());
container.Register(Component.For<IFoo>().ImplementedBy<Foo>());
Then I setup the SVC file:
<%# ServiceHost
Language="C#"
Debug="true"
Service="Foo"
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration"%>
When WCF tries to instantiate DefaultServiceHostFactory it says kernell is null and recommends using DefaultServiceHostFactory.RegisterContainer(IKernel kernel) method. However this method is not available in latest version of WCF facility.
Any help appriceated!
Thanks
figured it out, I was deploying the application to windows Azure and I used the web role startup for container registration (WRONG!) once I putted the container setup into Global.asax file it started working for IIS and Azure emulator as well.

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. :)

WCF Service Application Question

I am trying to create a WCF application that will be hosted in a Windows Service
I have two options in Visual Web Developer
1) New Website -> WCF Service
2) New Project -> WCF Service Application
Which will be suitable for creating service that can be hosted in windows service? Is there any tutorial explaining hosting in windows service using any of the above mentioned methods?
Thanks
Lijo
In most cases what you'll actually want to do is create two projects; first a WCF Service Library (that's under the WCF templates) and then a Windows Service (which is under the Windows templates), then reference the WCF Service Library from your Windows Service.
The WCF Service Library holds all of your WCF-specific classes - contracts, services, etc. - then you just create a ServiceHost inside the Windows service. This last part requires very little code:
public class MyService : ServiceBase // Windows Service class
{
private ServiceHost host;
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(MyWcfLibrary.MyWcfService));
host.Open();
}
protected override void OnStop()
{
host.Close();
}
}
There's a more detailed tutorial here.
As for creating your service - if you already plan to use a NT Service as your host, just use a class library and then add a WCF service to that. Neither of the two options given will give you something suitable for using in a NT Service.
For hosting, see:
MSDN: Host a WCF Service in a managed application
Hosting WCF Services
Lijo,
This page in the MSDN help shows you how to create the WCF service by starting with a Console application. I was able to get everything done with this as a starting point.