As needed, you need to host the WCF Service in Windows Service. The following describes the steps of the demo:
1. Write the WCF Service
Create an interface class and an implementation class. Add the [servicecontract] label to the interface class and the [operationcontract] label to the exposed service method
(Note: Add a reference to the system. servicemodel class.
The Code is as follows:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. servicemodel; using system. configuration; namespace Angelia. winservice. demowinservice {[servicecontract] interface imyservice {[operationcontract] string outputstring (string paramstring);} public class myservice: imyservice {code for implementing interface methods }}
Add the app. config file and add the following node for the WCF Service Information:
<System. servicemodel> <services> <service name = "Angelia. winservice. demowinservice. myservice "-- service class name behaviorconfiguration =" basicbehavior ">
2. Create a window service and place the WCF Service in the window service.
Find the onstart method automatically created by Visual Studio
Protected override void onstart (string [] ARGs) {servicehost host = new servicehost (typeof (myservice); -- host the WCF Service here. open (); -- open the service. }
Add the installation service class. On the Design Panel of the service class, right-click, and click Add installer on the pop-up menu. Then, a projectinstaller class is added successfully.
There are two controls on the design panel:
Select serviceprocessinstaller1. in the Properties window, select account. You can select the login user identity of Windows servcie. Generally, select NetworkService.
Select serviceinstaller1. in the Properties window, you can set the service name, Startup type, and other settings about the service.3. install or uninstall Windows ServicesRun the following Batch File Installation Service.
Set windowsserviceexename = "myservice.exe" -- the EXE file compiled in step 1. Set windowsservicename = "myservicedemo" -- Name of the Service set in the installation class. @ Echo. @ echo success @ echo installing services % windowsservicename % @ echo ----------------------------------------------------------------- @ echo. if exist % windowsserviceexename % installutil % windowsserviceexename % @ if errorlevel 1 Goto: Error httpcfg set urlacl-u http: // +: 8999/ingrianservice/-a d :( ;; GX; NS) @ echo. @ echo restart @ echo start the services for % windowsservicename % @ echo --------------------------------------------------------------- @ echo. net start % windowsservicename % @ if errorlevel 1 Goto: Error @ echo. @ echo ---------------------------------------- @ echo installservices. bat completed @ echo ---------------------------------------- @ echo. @ REM -------------------------------------- @ REM restore the command prompt and exit @ REM ---------------------------------------- @ Goto: exit @ REM handle errors @ REM use the following after any call to exit @ REM and return an error code when errors occur @ REM if errorlevel 1 Goto: error @ REM -------------------------------------------: Error @ echo an error occured in installservices. bat-% errorlevel % @ pause @ exit errorlevel @ REM ------------------------------------------ @ REM the exit label @ REM labels: Exit popd set pause = pause echo on
Uninstall Windows Services
Set windowsserviceexename = "myservice.exe" -- the EXE file compiled in step 1. Set windowsservicename = "myservicedemo" -- Name of the Service set in the installation class. @ Echo stopping @ echo stop the services for % windowsservicename % @ echo stopping @ echo.net stop % windowsservicename % @ echo stopping @ echo uninstalling services for % windowsservicename % @ echo stopping -----------------------------------------------------------------@ echoif exist % windowsserviceexename % installutil/U % windowsserviceexename % @ if errorlevel 1 Goto: error @ echo ---------------------------------------- @ echo uninstallservices. bat completed @ echo ---------------------------------------- @ echo @ REM ------------------------------------------ @ REM restore the command prompt and exit @ REM reply @ Goto: exit @ REM handle errors @ REM use the following after any call to exit @ REM and return an error code when errors occur @ REM if errorlevel 1 Goto: error @ REM -------------------------------------------: Error @ echo an error occured in installservices. bat-% errorlevel % @ pause @ exit errorlevel @ REM ---------------------------------------- @ REM the exit label @ REM labels: exitpopdset pause = pauseecho on
3. The client calls the WCF Service
First, add service reference. After the service is started, enter the service address, that is, the address in the configuration file in step 1,
(Note that the locahost in step 1 should be changed to the IP address of the machine)
After adding a reference. App. config has the following code:
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IIngrianService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://10.1.24.143:8999/IngrianService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IIngrianService" contract="ServiceReference1.IIngrianService" name="BasicHttpBinding_IIngrianService" /> </client> </system.serviceModel>
Call the service method: The following is the call code:
static void Main(string[] args) { ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient(); string text = client.OutputString("dsfsdfsdfsdf"); Console.WriteLine("string: " + text); Console.Read(); }