1. 创建WCF服务
在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1)
IService1接口如下:
-
using System.Runtime.Serialization;
-
using System.ServiceModel;
-
using System.ServiceModel.Web;
-
using System.Text;
-
namespace WcfService
-
{
-
[ServiceContract]
-
public interface IService1
-
{
-
[OperationContract]
-
string GetData(int value);
-
-
[OperationContract]
-
CompositeType GetDataUsingDataContract(CompositeType composite);
-
}
-
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
-
[DataContract]
-
public class CompositeType
-
{
-
bool boolValue = true;
-
string stringValue = "Hello ";
-
-
[DataMember]
-
public bool BoolValue
-
{
-
get { return boolValue; }
-
set { boolValue = value; }
-
}
-
-
[DataMember]
-
public string StringValue
-
{
-
get { return stringValue; }
-
set { stringValue = value; }
-
}
-
}
-
}
Service1实现类如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Runtime.Serialization;
-
using System.ServiceModel;
-
using System.ServiceModel.Web;
-
using System.Text;
-
-
namespace WcfService
-
{
-
public class Service1 : IService1
-
{
-
public string GetData(int value)
-
{
-
return string.Format("You entered: {0}", value);
-
}
-
-
public CompositeType GetDataUsingDataContract(CompositeType composite)
-
{
-
if (composite == null)
-
{
-
throw new ArgumentNullException("composite");
-
}
-
if (composite.BoolValue)
-
{
-
composite.StringValue += "Suffix";
-
}
-
return composite;
-
}
-
}
-
}
2.创建Window Service ,把WCF服务放在window Service中
先在window Service中添加引用,在对话框中选择Projects->Solution然后将wcfservice引入,这就在windows service中引用wcfservice里的service1时就不会报错了。
-
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 WcfService;
-
-
namespace WindowsServiceDemo
-
{
-
public partial class Baowg : ServiceBase
-
{
-
-
private ServiceHost host;
-
-
public Baowg()
-
{
-
InitializeComponent();
-
}
-
-
protected override void OnStart(string[] args)
-
{
-
if (this.host != null)
-
{
-
this.host.Close();
-
}
-
this.host = new ServiceHost(typeof(WcfService.Service1));
-
this.host.Open();
-
-
}
-
-
protected override void OnStop()
-
{
-
if (this.host != null)
-
{
-
this.host.Close();
-
}
-
}
-
}
-
}
增加app.config文件
-
<?xml version="1.0" encoding="utf-8" ?>
-
<configuration>
-
<system.serviceModel>
-
<services>
-
<service name="WcfService.Service1" behaviorConfiguration="basicBehavior">
-
<host>
-
<baseAddresses>
-
<add baseAddress="http://localhost:8999/Baowg"/> <!--windows service的地址-->
-
</baseAddresses>
-
</host>
-
<!--wcfservice的地址-->
-
<endpoint address="http://localhost:8999/Service1" contract="WcfService.IService1" binding="basicHttpBinding" />
-
</service>
-
</services>
-
<behaviors>
-
<serviceBehaviors>
-
<behavior name="basicBehavior">
-
<serviceMetadata httpGetEnabled="true" />
-
</behavior>
-
</serviceBehaviors>
-
</behaviors>
-
</system.serviceModel>
-
-
</configuration>
增加安装服务类。
在服务类的设计面板上,点鼠标右键,然后在弹出的菜单上,点add installer项,然后一个叫ProjectInstaller类增加成功。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
3. 安装或卸载Windows 服务
在windows service上生成解决方案,得到exe
管理员身份运行vs2010的命令行,在exe所在目录执行installutil xxxx.exe
在服务管理中启动baowg服务
4. 客户端调用WCF服务
把baowg服务启动后,给Client项目增加服务引用。输入服务地址http://localhost:8999/Baowg,也就是第一步中配置文件中的地址。
自动生成配置文件app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" 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://localhost:8999/Service1" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>