WCF Facility and console self-hosted web services

97 views
Skip to first unread message

Robert M.

unread,
Jan 25, 2009, 6:25:50 AM1/25/09
to Castle Project Users

Does anyone can help with an example on how can I configure and start
a webservice using WCF facility in a console application?

I used the following configuration, but although it compiles and runs,
the endpoint is not available (I believe the host is not started).

IoC.Container
.AddFacility<WcfFacility>()
.Register(

Component.For<IMyService>().ImplementedBy<MyService>()
.Named("my_wcf")
.ActAs(
new DefaultServiceModel().LogMessages()
.AddEndpoints(
WcfEndpoint.BoundTo(new
BasicHttpBinding("Basic")).At("http://localhost:9001/my/service.svc")
)
));


The problem I am trying to resolve is to use a WCF webservice setup
with WCF facility but with config settings taken from App.config. All
examples found in facility's unit tests use a web hosted service which
works just fine. My app is a console application only.



Craig Neuwirt

unread,
Jan 25, 2009, 5:27:55 PM1/25/09
to castle-pro...@googlegroups.com
It's probable that you one or more dependencies have not been satisfied so the ServiceHost is not available.  Since you are using logging, you need to register the logger facility
Message has been deleted

Robert M.

unread,
Jan 26, 2009, 2:36:18 PM1/26/09
to Castle Project Users
It is not any missing dependency nor logging facility... If I use the
following code and open ServiceHost explicitly it just works:

var factory = new DefaultServiceHostFactory
(IoC.Container.Kernel);
host = factory.CreateServiceHost("sms_wcf", new[] {new Uri
("http://localhost:9001/my/service.svc")});
host.Open();

The problem with this approach is that I do not know how to "pass"
config settings ( bindings, behaviours) from app.config.

R.

Craig Neuwirt

unread,
Jan 26, 2009, 2:40:03 PM1/26/09
to castle-pro...@googlegroups.com
That will always create the service host regardless of missing dependencies.  However, when you invoke it, it would then fail.

What is the definition os sms_wcf component?

On Mon, Jan 26, 2009 at 1:33 PM, Robert M. <Robert...@gmail.com> wrote:

Is not the dependency nor logging facility... If I use the following
code it just works:


           var factory = new DefaultServiceHostFactory
(IoC.Container.Kernel);
           host = factory.CreateServiceHost("sms_wcf", new[] {new Uri
(Config.WebserviceListenUri)});



On Jan 26, 12:27 am, Craig Neuwirt <cneuw...@gmail.com> wrote:

Robert M.

unread,
Jan 27, 2009, 1:46:33 PM1/27/09
to Castle Project Users
It is declared in Binsor:
component 'sms_wcf', IMyService, MyService

Craig Neuwirt

unread,
Jan 27, 2009, 1:57:07 PM1/27/09
to castle-pro...@googlegroups.com
I really think it's dependencies.  Can you send me test case and I'll figure out exactly why

Robert M.

unread,
Jan 27, 2009, 2:31:11 PM1/27/09
to Castle Project Users
Is there any reason why a missing dependency is not surfaced as an
exception by the container in this case?

I will try to see if I can simplify the service into an independent
testcase.

Craig Neuwirt

unread,
Jan 27, 2009, 2:37:31 PM1/27/09
to castle-pro...@googlegroups.com
Here is fragment of response from another email...

This is the most commonly discussed request.  However, this is the expected behavior for a good reason.
Creating a ServiceHost at the moment the service is registered assumes all dependencies
(services, behaviors, extensions) are available since once the ServiceHost is open for business, it is not
possible to add new behaviors.  Since behaviors and/or dependencies are specified at different time and
from different sources I delay creation of the ServiceHost until it is fully resolved.  I will consider adding a
flag to override.

In a nutshell, since the ServiceHost is not available, I can't give an exception why its not

Robert M.

unread,
Jan 27, 2009, 2:59:25 PM1/27/09
to Castle Project Users
I've added a sample in the Files section. It is called
TestWCFFacility.zip.

Thanks a lot for helping me.

Craig Neuwirt

unread,
Jan 27, 2009, 5:19:56 PM1/27/09
to castle-pro...@googlegroups.com
Where can I find the zip?

Craig Neuwirt

unread,
Jan 27, 2009, 5:21:16 PM1/27/09
to castle-pro...@googlegroups.com
Found it

Robert M.

unread,
Jan 28, 2009, 1:20:30 PM1/28/09
to Castle Project Users
Hi Craig,

Any hints related to my problem?

R.

On Jan 28, 12:21 am, Craig Neuwirt <cneuw...@gmail.com> wrote:
> Found it
>
> On Tue, Jan 27, 2009 at 4:19 PM, Craig Neuwirt <cneuw...@gmail.com> wrote:
> > Where can I find the zip?
>

Craig Neuwirt

unread,
Jan 28, 2009, 1:38:57 PM1/28/09
to castle-pro...@googlegroups.com
What do I do when I run the executable in WCFTestFacility?

Robert M.

unread,
Jan 28, 2009, 4:00:03 PM1/28/09
to Castle Project Users
you should try open a web browser and navigate to http://localhost:9001/SMS/service.svc

In normal condition, it would need to show at least the standard "wcf
help page" of the service, but in this case the initial page won't
load.

I've check also if the WCF service starts listening on 9001 using
tcpview but the port is not opened. I suspect that host is not open.

Eric Hauser

unread,
Jan 28, 2009, 4:43:58 PM1/28/09
to Castle Project Users
You need to add a base address:

new DefaultServiceModel()
.AddBaseAddresses("http://localhost:9001/
SMS/service.svc")
.AddEndpoints(
WcfEndpoint.BoundTo(new
BasicHttpBinding("Basic")).At("http://localhost:9001/SMS/service.svc")



On Jan 28, 4:00 pm, "Robert M." <RobertCMir...@gmail.com> wrote:
> you should try open a web browser and navigate tohttp://localhost:9001/SMS/service.svc

Craig Neuwirt

unread,
Jan 28, 2009, 4:54:20 PM1/28/09
to castle-pro...@googlegroups.com
The Service seemed to get registered fine and I was able to communicate on that port.  I got a comm error since I was trying to invoke an action with get, but it looks like ServiceHost came up fine.  What do you see when you hit the page?

Robert M.

unread,
Jan 28, 2009, 5:17:18 PM1/28/09
to Castle Project Users
Thanks,

Great... after adding .AddBaseAddresses("http://localhost:9001/SMS/
service.svc") it started and it works.
Now I go back to the initial question of the post... how do I make the
service read all config settings from app.config instead of hardcoding
them into app or building custom config section? So, instead of
calling AddBaseAddresses and At I would like to point the service to
the config section in the app.config.

For example, app.config contains for this test app the following:

<system.serviceModel>

<bindings>
<basicHttpBinding>
<binding name="Basic" receiveTimeout="00:30:00"
sendTimeout="00:30:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" >
<security mode="None">
<transport clientCredentialType="Basic"
realm="norealmhere" />
</security>
</binding>
</basicHttpBinding>
</bindings>

<services>
<service behaviorConfiguration="MyServiceTypeBehaviors"
name="sms_wcf">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="Basic"
contract="TestWCFFacility.IMyService" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/svc/service.svc" /
>
</baseAddresses>
</host>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"
httpsGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="100000" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>

Eric Hauser

unread,
Jan 28, 2009, 6:13:24 PM1/28/09
to Castle Project Users
Component.For<IMyService>().ImplementedBy<MyService>()
.ActsAs(new DefaultServiceModel());

Check out
http://svn.castleproject.org:8080/svn/castle/trunk/Facilities/Wcf/Castle.Facilities.WcfIntegration.Tests/ServiceHostFixture.cs
--- CanCreateServiceHostAndOpenHostFromConfiguration

Craig Neuwirt

unread,
Jan 28, 2009, 6:32:58 PM1/28/09
to castle-pro...@googlegroups.com
AddBaseAddresss is used for relative addressing so not sure why you see different results with it.

Eric Hauser

unread,
Jan 28, 2009, 6:38:43 PM1/28/09
to Castle Project Users
Craig -- I think the service was working fine without the base
address. I was able to hit the URL fine without it (nothing returned
because it was a GET, but it worked). It is required for the metadata
behavior to work. Haven't really dug into why.


On Jan 28, 6:32 pm, Craig Neuwirt <cneuw...@gmail.com> wrote:
> AddBaseAddresss is used for relative addressing so not sure why you see
> different results with it.
>

Craig Neuwirt

unread,
Jan 28, 2009, 6:46:40 PM1/28/09
to castle-pro...@googlegroups.com
That's what I saw too.
Reply all
Reply to author
Forward
0 new messages