Decorate a WCF service with Autofac - wcf

I want to decorate WCF service with the following setup:
SVC definition
<%# ServiceHost Language="C#" Debug="true"
Service="Foo.IBarService, Foo"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"%>
Autofac registration
builder.RegisterType<FooService>().Named<IFooService>("fooservice");
builder.RegisterType<LogFooService>().Named<IFooService>("logfooservice");
builder
.RegisterDecorator<IFooService>(
(context, inner) => context.ResolveNamed<IFooService>("logfooservice", TypedParameter.From(inner)),
fromKey: "fooservice")
.As<IFooService>();
This approach does work when the decorator is not the service itself. So for example an concrete service with a decorated IWhatever in the constructor. What is different with the svc service?

The docs state:
The standard Autofac service hosting works well for almost every case, but if you are using decorators on your WCF service implementation then you need to use the multitenant WCF service hosting mechanism rather than the standard Autofac service host.

Related

Understanding WcfIISHost Class Library in a Project

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.
 

Create WCF Service to be hosted differently

I have a WCF service which programmatically creates its endpoints rather than using a config file - I'm looking into this as our field engineers are prone to break XML easily and we may use different types of binding in different scenarios.
This works well in self-hosted environments (console app, windows app) and as a Windows Service.
Can I do this with a service in IIS or do I have to provide a .SVC file for each endpoint ?
Also will the endpoint address from the client end have to include the .SVC extension ?
This is not a service intended to be used by third parties, only by our client components. We may expose parts of our API later but not initially.
If you're using .NET Framework 4.0 (and later), you can use the ASP.NET routing integration to define a service using a custom ServiceHostFactory implementation. A few things you'll need:
In web.config, set the attribute aspNetCompatibilityEnabled on the <system.serviceModel / serviceHostingEnvironment> element to true
Add a global.asax / global.asax.cs file, and in the Application_Start add a new ServiceRoute to the ASP.NET RouteTable.Routes collection. The service route requres you to define a new service host factory, where you can define your endpoints programmatically.
With that you'll be able to have endpoints without the ".svc" in their addresses. You can also use the service host factory without using routes, by creating a .svc file for each service (not endpoint), and using the Factory attribute in the <%# ServiceHost directive.
For more information about service host factories, check the post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx.

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.

WCF PerSession service and AutoMapper

where should I put the code for configuring AutoMapper mapping between DTO and EntityFramework Entities for WCF PerSession services?
If in the default constructor, does that mean every session will create an individual mapping and causes lots of overhead on the server side?
Thanks
Configuration of AutoMapper is global for the whole application so your service should not do any configuration itself. AutoMapper should be configured in your application bootstraper (when the application starts). For example in WCF service hosted in IIS (over HTTP) you can use standard Global.asax and Application_Start.

WCF and HTTP GET

My WCF service exposes this function
public SerialNumberInfo GetSerialNumberInfo(string serialNumber) { }
Is there a way to enable HTTP GET on my WCF service? Example:
http://localhost:8004/MyService/GetSerialNumberInfo?serialNumber=4
yes, you need to use the webHttpBinding on your WCF service.
See the WCF REST Starter Kit for more information on REST Support in WCF.
REST in WCF
Overview of REST in WCF
Series of screencasts on how to use REST in WCF
If you're hosting your service in IIS, you need to create a separate *.svc file for the REST service (let's call it RESTService.svc), which contains:
<%# ServiceHost Service="YourServiceName" Language="C#" debug="False"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Now you should be able to connect to the URL
http://localhost:8004/MyService/RESTService.svc
and get your data RESTfully.
Marc
What you want to do sounds like building a RESTful service with WCF. Check out the following article on MSDN, it has all the details:
A Guide to Designing and Building RESTful Web Services with WCF 3.5