c# - Hosting WCF REST Service as Windows Service -
i want create rest wcf service , install windows service. have created rest wcf service , ran that, working fine both xml amd json. below code files. irestwcfservicelibrary.cs:
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.text; namespace restwcfservicelibrary { // note: if change class name "service1" here, must update reference "service1" in app.config. public class restwcfservicelibrary : irestwcfservicelibrary { public string xmldata(string id) { return "id:" + id; } public string jsondata(string id) { return "id:" + id; } } }
restwcfservicelibrary.cs
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.text; using system.servicemodel.web; namespace restwcfservicelibrary { // note: if change interface name "iservice1" here, must update reference "iservice1" in app.config. [servicecontract] public interface irestwcfservicelibrary { [operationcontract] [webinvoke(method = "get", responseformat = webmessageformat.xml, bodystyle = webmessagebodystyle.wrapped, uritemplate = "xml/{id}")] string xmldata(string id); [operationcontract] [webinvoke(method = "get", responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrapped, uritemplate = "json/{id}")] string jsondata(string id); } }
app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- when deploying service library project, content of config file must added host's app.config file. system.configuration not support config files libraries. --> <system.servicemodel> <services> <service behaviorconfiguration="restwcfservicelibrary.service1behavior" name="restwcfservicelibrary.restwcfservicelibrary"> <endpoint address="" binding="webhttpbinding" contract="restwcfservicelibrary.irestwcfservicelibrary" behaviorconfiguration="web"> </endpoint> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange" /> <host> <baseaddresses> <add baseaddress="http://localhost:8001/restwcfservicelibrary/" /> </baseaddresses> </host> </service> </services> <behaviors> <servicebehaviors> <behavior name="restwcfservicelibrary.service1behavior"> <!-- avoid disclosing metadata information, set value below false , remove metadata endpoint above before deployment --> <servicemetadata httpgetenabled="true"/> <!-- receive exception details in faults debugging purposes, set value below true. set false before deployment avoid disclosing exception information --> <servicedebug includeexceptiondetailinfaults="false" /> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="web"> <webhttp/> </behavior> </endpointbehaviors> </behaviors> </system.servicemodel> </configuration>
now want host/install windows service, added window service project , gave reference of resr wcf created above. named service class myrestwcfrestwinser
myrestwcfrestwinser.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.diagnostics; using system.linq; using system.serviceprocess; using system.text; using system.servicemodel; using restwcfservicelibrary; namespace restwcfwinservice { public partial class myrestwcfrestwinser : servicebase { servicehost oservicehost = null; public myrestwcfrestwinser() { initializecomponent(); } protected override void onstart(string[] args) { oservicehost = new servicehost(typeof(myrestwcfrestwinser)); oservicehost.open(); } protected override void onstop() { if (oservicehost != null) { oservicehost.close(); oservicehost = null; } } } }
program.cs
using system; using system.collections.generic; using system.linq; using system.serviceprocess; using system.text; namespace restwcfwinservice { static class program { /// <summary> /// main entry point application. /// </summary> static void main() { servicebase[] servicestorun; servicestorun = new servicebase[] { new myrestwcfrestwinser() }; servicebase.run(servicestorun); } } }
and added project installer, ran installer. after running registered service command prompt using installutil. service registered , listed in services. if start service giving error "the restwcfwinservice service on local computer started , stopped. services stop automatically if not in use other services or programs"
but if using soap working perfect.
so please me install rest wcf service windows service.
i believe there 2 issues - 1 of have corrected per comments.
first, you're using servicehost
instead of webservicehost
. i'm not 100% that's part of problem, based on comments (no errors in event viewer when using servicehost
, error when changed webservicehost
seem indicate was).
the second issue appears related configuration file. have wcf service library (a dll). design, dlls not use app.config file included in project template - use config file of consuming application. in case, windows service. copy <system.servicemodel>
section library config file app.config file of windows service. wcf class library should pick endpoint @ point.
note windows service config file, once project compiled, named myrestwcfrestwinser.exe.config
, not app.config
.
Comments
Post a Comment