WCF之Windows宿主(可安装成服务自动并启动)
创新互联是一家专注于做网站、网站制作与策划设计,崇左网站建设哪家好?创新互联做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:崇左等地区。崇左做网站价格咨询:13518219792图1:图2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace WCFService.Models
{
[DataContract]
[Serializable]
public class Book
{
[DataMember]
public string Name { get; set; }
[DataMember]
public double Price { get; set; }
}
}
Bookusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCFService.Models;
namespace WCFService
{
public class BookService : IBookService
{
List<Book> list = new List<Book>();
public bool Add(string name, double price)
{
list.Add(new Book() { Name = name, Price = price });
return true;
}
public List<Book> GetList()
{
return list;
}
}
}
BookServiceusing System;
using System.ServiceModel;
namespace WCFService
{
[ServiceContract]
public interface IBookService
{
[OperationContract]
bool Add(string name, double price);
[OperationContract]
System.Collections.Generic.List<WCFService.Models.Book> GetList();
}
}
IBookService
图3:图4:
在Service1的设计界面中右击,选择“属性”,把其中的(Name)和ServiceName都改为BookServiceHost
编写代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using WCFService;
namespace WindowsServiceHost
{
public partial class BookServiceHost : ServiceBase
{
ServiceHost _Host= new ServiceHost(typeof(BookService));
public BookServiceHost()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
_Host.Open();
}
protected override void OnStop()
{
_Host.Close();
}
}
}
BookServiceHost图5:
Http(高级Web服务互操作性)
图20: 图21: 图22:
到目前为止我们配置好了两个http通道下的两个终结点,但这两个终结点的地址我们都使用的是相对地址,它们是相对于当前ServiceHost地址,所以我们还需要配置当前ServiceHost的地址.
图23:
这样我们两个终结点算是配置完成了。
“自运行WCF服务”与“在IIS布运行WCF服务”不一样的是,“自运行WCF服务"除了可以使用Http方式发布WCF服务,可以使用TCP、命名管道和微软消息队列进行信息传输。
下面我们再配置两个终结点,一个是使用TCP通信模式,另一个使用命名管道通信模式。
TCP:
图24:图25:
命名管道:
图26:图27:
到此为至,我们已经为该WCF服务建立了四个数据传输的终结点
下面我们为该ServiceHost程序配置“元数据终结点”,以向客户端发送服务元数据信息
图28:图29:
图30:
图31:图32:
图34:图33:
图35:图36:
图37:图38:
最终页面:到目前为止我们已经将Window服务宿主配置完毕,现在保存,关闭WCF配置工具,并打开App.Config<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WCFService.BookService">
<clear />
<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="ws" binding="ws2007HttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:8082/BookService" binding="netTcpBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="net.pipe://localhost/BookService" binding="netNamedPipeBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
App.Config然后把下面代码删掉:
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
最终的App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WCFService.BookService">
<clear />
<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
</endpoint>
<endpoint address="ws" binding="ws2007HttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
</endpoint>
<endpoint address="net.tcp://localhost:8082/BookService" binding="netTcpBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
</endpoint>
<endpoint address="net.pipe://localhost/BookService" binding="netNamedPipeBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
</endpoint>
<endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
App.Config以上所写不仅适用与Windows宿主,同时适用IIS、控制台,因此后面关于IIS以及控制台宿主的发布不再重复以上配置
在Service1设计界面中右击,选择“添加安装程序”
图40:图41:
图42:图43(此图网络引用):
进入vs2012 开发命令提示,进入项目对应的盘符并进入exe所在文件夹,执行命令 :installutil WindowsServiceHost.exe
图44:
图45:
图46:
图
在VS2008命令窗口中输入:wcftestclienthttp://localhost:8081/Service 出现下面的界面
47::
Demo下载
本文题目:WCF之Windows宿主(可安装成服务自动并启动)-创新互联
文章链接:https://www.cdcxhl.com/article46/dhppeg.html
成都网站建设公司_创新互联,为您提供定制网站、企业建站、网站设计公司、关键词优化、微信公众号、电子商务
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联