|
-
July 20th, 2015, 04:11 PM
#1
Can a WCF service hosted in a Windows service questions
The only examples I've found of hosting a WCF service in a Windows service have the WCF service configured inside the Windows service's OnStart() method. I would think that would severely hamper the WCF service's flexibility. Is there some reason why the examples I've found do it that way?
The example I've been working with, http://www.codeproject.com/Articles/...indows-Service, uses svcutil to create a process class for a client project. But I saw something on one website that said that a proxy is not needed if the service and the client are going to be running on the same machine, which is my situation. Can someone point me to an example of building a WCF client without using a proxy?
I tried to replace the programmatic configuration of the service in the above article with configuration using app.config and a proxy, but I always get complaints about the service not being able to handle utf-8. It seems that the service is not able to handle messages in the format used by svcutil, but I don't know what svcutil uses. Here is my configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<services>
<service behaviorConfiguration="MetadataBehavior" name="WcfInWindowsService2.WcfService">
<endpoint address="http://localhost:9001/CalcService2"
binding="basicHttpBinding"
contract="WcfInWindowsService2.IWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<endpoint address="net.tcp://locahost:9002/CalcService2"
binding="netTcpBinding"
contract="WcfInWindowsService2.IWcfService" />
<endpoint address="net.tcp://localhost:9002/mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfInWindowsService2/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Thank you very much.
-
July 22nd, 2015, 04:53 AM
#2
Re: Can a WCF service hosted in a Windows service questions
Check out my Tray Notify - Part III article. http://mobile.codeguru.com/csharp/.n...CF-Service.htm
I show how to host one or more wcf or web services in a windows service. My approach dynamically reads and host the services listed in the app.config file.
As far as I know you need to generate a proxy even when service and client are on the same machine. I guess you could do it without the proxy by sending raw requests, but why since generating a proxy is so easy (you know you can generate a proxy by right clicking on your project's references node inside the solution explorer and choose 'Add sevice reference')?
Not sure why you are concerned with starting the hosting of the wcf services in OnStart (). In general when working with a Windows Services, initial start up is done in OnStart and broken down in OnStop and OnShutdown. You can put the startup code anywhere besides OnStart, but it may not behave like a service when the systen tries to autostart it after a reboot or a user tries to start or stop it from the control panel.
Last edited by Arjay; July 22nd, 2015 at 05:10 AM.
-
July 24th, 2015, 08:55 AM
#3
Re: Can a WCF service hosted in a Windows service questions
Thanks very much! The app.config file in your article appeared fundamentally similar to mine, but after a couple of minutes staring at it I noticed you had an endpoint like this:
<endpoint address=""
binding="wsDualHttpBinding"
contract="CG.TrayNotify.Common.Interface.ITrayNotify">
<identity>
<dns value="localhost" />
</identity>
And you also had this:
<host>
<baseAddresses>
<add baseAddress="http://localhost:8071/TrayNotify/" />
</baseAddresses>
</host>
Your endpoint has an empty address. Here's my corresponding endpoint and host element:
<endpoint address="http://localhost:9001/CalcService2"
binding="wsHttpBinding"
contract="WcfInWindowsService2.IWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfInWindowsService2/Service1/" />
</baseAddresses>
</host>
When I created this project, I just left the <host> element unchanged from what Visual Studio automatically gave me. To get the project to allow me to create my proxy files, I copied the endpoint address into the base address, and changed the endpoint address to an empty string, as in your sample. Then, svcutil was able to generate my proxies!
Thanks again!
RobR
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|