WCF Self Hosting example

1,469 views
Skip to first unread message

schaibaa

unread,
Apr 24, 2012, 1:48:58 PM4/24/12
to ninject
Hello,

I'm looking for an example explaining how to self host WCF services
with NInject. It seems like the vast majority of the classes are
there but I'm not exactly sure how they should be wired up.

I'd like to be able to use the built in context modes, probably per
call - so I can scope some of my dependencies to the "request".

I've found this blog:
http://pieterderycke.wordpress.com/2011/05/09/using-an-ioc-container-to-create-wcf-service-instances/

But it seems like most of this is already done with Ninject.

The constructor for NinjectServiceHost requires IServiceBehavior and
the instance.

NinjectServiceBehavior requires and InstanceProviderFactory, and an
IDispatchMessageInspector -- neither of which I'm familiar with. I
can definitely implement the factory and get a new instance from the
kernel.. but I do not think that's the correct way to proceed.

Thanks in advance

Morten Nilsen

unread,
Apr 24, 2012, 2:35:03 PM4/24/12
to nin...@googlegroups.com
Den 24.04.2012 19:48, skrev schaibaa:
> I'm looking for an example explaining how to self host WCF services
> with NInject. It seems like the vast majority of the classes are
> there but I'm not exactly sure how they should be wired up.
>
> I'd like to be able to use the built in context modes, probably per
> call - so I can scope some of my dependencies to the "request".

Hi, I was looking into this a while back, and ended up building an
example that was checked into the source.
Please have a look at this project:

https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WcfServiceLibrary

Does that help?

--
Regards, Morten

schaibaa

unread,
Apr 24, 2012, 2:52:50 PM4/24/12
to ninject
Thanks a lot - this looks like it will help!
> https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Exa...

schaibaa

unread,
Apr 24, 2012, 3:04:19 PM4/24/12
to ninject
OK, I may have spoken too soon. This is definitely a step in the
right direction, but I want to host this in a windows service - this
looks like it's designed to be hosted in IIS?

Morten Nilsen

unread,
Apr 24, 2012, 3:06:40 PM4/24/12
to nin...@googlegroups.com
Den 24.04.2012 21:04, skrev schaibaa:
> OK, I may have spoken too soon. This is definitely a step in the
> right direction, but I want to host this in a windows service - this
> looks like it's designed to be hosted in IIS?
Yes, that sample is for file-less activation hosting in IIS.
Maybe
https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WindowsTimeService
would be more relevant for you?

--
Cheers, Morten

schaibaa

unread,
Apr 24, 2012, 4:51:19 PM4/24/12
to ninject
Yup .. this is a huge help -- however, any idea how to do the endpoint
configuration in app.config and/or expose metadata?

On Apr 24, 3:06 pm, Morten Nilsen <mor...@runsafe.no> wrote:
> Den 24.04.2012 21:04, skrev schaibaa:> OK, I may have spoken too soon.  This is definitely a step in the
> > right direction, but I want to host this in a windows service - this
> > looks like it's designed to be hosted in IIS?
>
> Yes, that sample is for file-less activation hosting in IIS.
> Maybehttps://github.com/ninject/ninject.extensions.wcf/tree/master/src/Exa...

Morten Nilsen

unread,
Apr 24, 2012, 5:09:16 PM4/24/12
to nin...@googlegroups.com
Den 24.04.2012 22:51, skrev schaibaa:
> Yup .. this is a huge help -- however, any idea how to do the endpoint
> configuration in app.config and/or expose metadata?
Endpoint configuration is pretty much the same for any hosting scenario
with WCF.
To expose metadata, you will only need to add a enpoint for
IMetadataExchange.
The minimal sample I wrote here:
https://github.com/ninject/ninject.extensions.wcf/blob/master/src/Examples/WcfServiceLibrary/App.config

Should work also for a windows service hosted endpoint, just without the
serviceHostingEnvironment bit.
You would also need to specify a different baseAddress.

For most simple scenarios, you won't need to specify any binding
configuration.
The most common exception to this that I have come across is if one
sends large object graphs over the wire - which is probably indicative
of poorly designed APIs.

--
Cheers, Morten

Reply all
Reply to author
Forward
0 new messages