WCF之Windows宿主(可安裝成服務(wù)自動(dòng)并啟動(dòng))
創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計(jì),崇左網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:崇左等地區(qū)。崇左做網(wǎng)站價(jià)格咨詢: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的設(shè)計(jì)界面中右擊,選擇“屬性”,把其中的(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(高級(jí)Web服務(wù)互操作性)
圖20: 圖21:
圖22:
到目前為止我們配置好了兩個(gè)http通道下的兩個(gè)終結(jié)點(diǎn),但這兩個(gè)終結(jié)點(diǎn)的地址我們都使用的是相對(duì)地址,它們是相對(duì)于當(dāng)前ServiceHost地址,所以我們還需要配置當(dāng)前ServiceHost的地址.
圖23:
這樣我們兩個(gè)終結(jié)點(diǎn)算是配置完成了。
“自運(yùn)行WCF服務(wù)”與“在IIS布運(yùn)行WCF服務(wù)”不一樣的是,“自運(yùn)行WCF服務(wù)"除了可以使用Http方式發(fā)布WCF服務(wù),可以使用TCP、命名管道和微軟消息隊(duì)列進(jìn)行信息傳輸。
下面我們?cè)倥渲脙蓚€(gè)終結(jié)點(diǎn),一個(gè)是使用TCP通信模式,另一個(gè)使用命名管道通信模式。
TCP:
圖24:圖25:
命名管道:
圖26:圖27:
到此為至,我們已經(jīng)為該WCF服務(wù)建立了四個(gè)數(shù)據(jù)傳輸?shù)慕K結(jié)點(diǎn)
下面我們?yōu)樵揝erviceHost程序配置“元數(shù)據(jù)終結(jié)點(diǎn)”,以向客戶端發(fā)送服務(wù)元數(shù)據(jù)信息
圖28:圖29:
圖30:
圖31:圖32:
圖34:圖33:
圖35:圖36:
圖37:圖38:
<?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宿主,同時(shí)適用IIS、控制臺(tái),因此后面關(guān)于IIS以及控制臺(tái)宿主的發(fā)布不再重復(fù)以上配置
在Service1設(shè)計(jì)界面中右擊,選擇“添加安裝程序”
圖40:圖41:
圖42:圖43(此圖網(wǎng)絡(luò)引用):
進(jìn)入vs2012 開發(fā)命令提示,進(jìn)入項(xiàng)目對(duì)應(yīng)的盤符并進(jìn)入exe所在文件夾,執(zhí)行命令 :installutil WindowsServiceHost.exe
圖44:
圖45:
圖46:
圖
在VS2008命令窗口中輸入:wcftestclienthttp://localhost:8081/Service 出現(xiàn)下面的界面
47::
Demo下載
本文題目:WCF之Windows宿主(可安裝成服務(wù)自動(dòng)并啟動(dòng))-創(chuàng)新互聯(lián)
文章鏈接:http://chinadenli.net/article46/dhppeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、微信公眾號(hào)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容