使用WindowsService为宿主实装WCF 服务
1. 写WCF服务
创建一个接口类,与一个实现类。接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract]
(注意:增加System.ServiceModel 类的引用
代码如下:
- 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
- {
- 实现接口方法的代码
- }
- }
增加app.config文件,增加WCF服务信息的结点如下:
- <system.serviceModel>
- <services>
- <service name="Angelia.WinService.DemoWinService.MyService" --服务的类名 behaviorConfiguration="basicBehavior">
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8999/MyService"/>
- </baseAddresses>
- </host>
- <endpoint address="http://localhost:8999/MyServiceService" --指定服务的位置
- contract="Angelia.WinService.DemoWinService.IMyService" --接口类的名字,即是contract
- binding="basicHttpBinding" />
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="basicBehavior">
- <serviceMetadata httpGetEnabled="true" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
2.创建Window Service ,把WCF服务放在window Service中
找到visual studio 自动帮助创建的OnStart方法
- protected override void OnStart(string[] args)
- {
- ServiceHost host = new ServiceHost(typeof(MyService)); --把WCF的service宿主在这里
- host.Open(); --打开服务。
- }
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
- set WindowsServiceExeName="MyService.exe" --第一步中编译出来的exe文件。
- set WindowsServiceName="MyServiceDemo" --在安装类中设置的服务的名字。
- @ECHO.
- @ECHO -----------------------------------------------------------------
- @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:(A;;GX;;;NS)
- @ECHO.
- @ECHO -----------------------------------------------------------------
- @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 -------------------------------------------
- @REM Handle errors
- @REM
- @REM Use the following after any call to exit
- @REM and return an error code when errors occur
- @REM
- @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 ----------------------------------------
- :exit
- popd
- set pause=
- PAUSE
- echo on
卸载Windows 服务
- set WindowsServiceExeName="MyService.exe" --第一步中编译出来的exe文件。
- set WindowsServiceName="MyServiceDemo" --在安装类中设置的服务的名字。
- @ECHO
- @ECHO -----------------------------------------------------------------
- @ECHO Stop the services for %WindowsServiceName%
- @ECHO -----------------------------------------------------------------
- @ECHO
- .net stop %WindowsServiceName%
- @ECHO
- @ECHO -----------------------------------------------------------------
- @ECHO Uninstalling Services for %WindowsServiceName%@ECHO -----------------------------------------------------------------
- @ECHO
- if Exist %WindowsServiceExeName% installutil /u %WindowsServiceExeName%
- @if errorlevel 1 goto :error
- @ECHO
- @ECHO ----------------------------------------
- @ECHO UninstallServices.bat Completed
- @ECHO ----------------------------------------
- @ECHO
- @REM ----------------------------------------
- @REM Restore the command prompt and exit
- @REM ----------------------------------------
- @goto :exit
- @REM -------------------------------------------
- @REM Handle errors
- @REM
- @REM Use the following after any call to exit
- @REM and return an error code when errors occur
- @REM
- @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 ----------------------------------------
- :exit
- popd
- set pause=PAUSE
- echo on
3.客户端调用WCF服务
首先增加service reference.把服务启动后,输入服务地址,也就是第一步中配置文件中的地址,
(注意第一步中的locahost要改成机器的IP地址)
添加完引用后。app.config中有如下代码:
- <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>
调用服务方法:如下是调用代码:
- static void Main(string[] args)
- {
- ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient();
- string text = client.OutputString("dsfsdfsdfsdf");
- Console.WriteLine("string: " + text);
- Console.Read();
- }
使用WindowsService为宿主实装WCF 服务的更多相关文章
- Windows 服务为宿主的WCF服务,详细图解。
废话不多说,直接进入主题: 1.打开vs2010新建项目,选择Windows服务. 2.选中WindowsService右击,添加WCF服务. 3.添加成功后,为下图.将接口类ITestService ...
- WCF系列教程之WCF服务宿主与WCF服务部署
本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF ...
- [问题]通过IIS宿主发布WCF服务,客户端添加服务引用出错的解决办法
环境配置:Web服务器:Windows Server 2008,iis7.5,.net4.0客户端:XPsp3 vs2010 sp1 问题描述:1.确定WCF服务访问地址 http://servic ...
- 使用IIS发布WCF服务
上一篇是Windows服务为宿主的WCF服务,现在用IIS为宿主发布WCF服务. 第一步:肯定是新建一个WCF服务啦[是WCF服务应用程序],然后在解决方案上再次添加一个新项目[我们选择WCF服务库, ...
- WCF服务二:创建一个简单的WCF服务程序
在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...
- WCF初探-8:WCF服务承载 (上)
前言 任何一个程序的运行都需要依赖一个确定的进程中,WCF服务也不例外.如果使用WCF服务,我们就必须将服务承载于创建它并控制它的上下文和生存期的运行时环境中,承载服务环境的程序,我们称之为宿主.WC ...
- WCF服务的创建和发布到IIS
一. WCF服务的创建 有两种创建方式: 1.WCF服务库 2.WCF服务应用程序 如下图所示: 这里选择WCF服务库.注意事项: 1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高 ...
- 【转】WCF服务的创建和发布到IIS
一. WCF服务的创建 有两种创建方式: 1.WCF服务库 2.WCF服务应用程序 如下图所示: 这里选择WCF服务库.注意事项: 1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高 ...
- 添加宿主为控制台应用程序的WCF服务
1.创建WCF服务库:WcfServiceLibrary,根据自动创建的代码修改自己的WCF 服务协议.操作协议.数据协议.本次先实现简单的WCF最基本的通信方式:请求->应答模式. 定义服务. ...
随机推荐
- debug启动项目很慢
用debug启动项目比正常启动慢,从网上找到的是这样说的.删除所有的断点就可以了. 这个问题可能是由于eclipse和tomcat的交互而产生的,在以debug模式启动tomcat时,发生了读取文件错 ...
- Python项目自动生成当前项目的requirements文件
使用pip freeze $ pip freeze > requirements.txt 这种方式是把整个环境中的包都列出来了,如果是虚拟环境可以使用. 通常情况下我们只需要导出当前项目的r ...
- UESTC - 1607 ad-hoc
#include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) using namespace s ...
- php 的基本语法
八种数据类型: 4种标量类型:boolean.integer.float.string 2种复合类型:array.object 2种特殊类型:resource.NULL 如果想看某个表达式的值和类型用 ...
- sed 练习
[root@server3 mnt]# cat -n passwd 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nol ...
- D15 模块
模块
- 【OpenCV-python】CV2和PIL按box信息实现图像裁剪
# 用cv2实现裁剪 import cv2 import os img = cv2.imread("./test_and_verification/1406170100001.jpg&quo ...
- 阿里云centos 7 中tomcat 自启动
这里我的tomcat的安装路径为 /usr/local/tomcat 1 为tomcat添加自启动参数 catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此 ...
- 【Linux】修改ubuntu默认字符集
今天把以前的项目移植到linux上了,我装的是ubuntu,web服务器是tomcat,发现用freemark模板生成的静态页面全 乱码了,在windows都是正常的,猜想可能是linux字符集的问题 ...
- php之mongodb插入数据后如何返回当前插入记录ID
<?php /** *插入记录 *参数: *$table_name:表名 *$record:记录 * *返回值: *成功:true *失败:false */ function insert($t ...