欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

WCF之Windows宿主(可安裝成服務(wù)自動(dòng)并啟動(dòng))-創(chuàng)新互聯(lián)

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
  • 創(chuàng)建解決方案WCFServiceDemo
  • 創(chuàng)建WCF服務(wù)庫(kù)(類庫(kù)或WCF服務(wù)庫(kù))WCFService ,添加引用System.ServiceModel、System.Runtime.Serialization

圖1:圖2: 

  • 創(chuàng)建實(shí)體模型Book
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; }
    }
}
Book
  • 創(chuàng)建實(shí)現(xiàn)類BookService
using 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;
        }
    }
}
BookService
  • 創(chuàng)建接口IBookService(接口必須加上ServiceContract特性,方法必須加上OperationContract特性)
using 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
  • 如圖:

 

  • 創(chuàng)建Windows服務(wù)宿主WindowsServiceHost ,添加引用System.ServiceModel、System.Runtime.Serialization    

圖3:圖4:

  • 修改Service1的屬性

在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
  • 編輯WCF配置(WCF工具配置

圖5:

  • 新增服務(wù),彈出界面,由于該App.Config文件是我們新添加的一個(gè)配置文件,所以左邊的服務(wù)項(xiàng)中是空的。點(diǎn)擊右邊的“新建服務(wù)...”彈出“新建服務(wù)元素向?qū)А贝翱?,單擊“瀏覽”按鈕,選擇Bin/Debug目錄下Services.dll程序集中的Services.BookService服務(wù)。
圖6:圖7:
  • 添加終結(jié)點(diǎn):終結(jié)點(diǎn)(endpoint)分別有:TCP、HTTP、命名管道、MSMQ、對(duì)等、元數(shù)據(jù)。
圖8:圖9:圖10:圖11:圖12:圖13圖14:圖15:  圖16:圖17:
  • 依次添加其他的通信模式的終結(jié)點(diǎn)(上面圖示為:http通信模式基本W(wǎng)eb服務(wù)操作性)模式選擇見下圖
圖18:圖19:
  • 下面依次演示添加其他通信模式的終結(jié)點(diǎn)

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的地址.

  • 配置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ù)信息

  • 添加服務(wù)行為。在左側(cè)配置中選擇“高級(jí)”-“服務(wù)行為”,再點(diǎn)擊右側(cè)的“新建服務(wù)行為分配”,點(diǎn)擊“添加”彈出“添加行為元素?cái)U(kuò)展部份”窗口,選擇“serviceMetaData”

圖28:圖29:

  • 為服務(wù)配置剛剛新建的行為。

圖30:

  • 配置元數(shù)據(jù)終結(jié)點(diǎn)

圖31:圖32:

圖34:圖33:

圖35:圖36:

圖37:圖38:

最終頁(yè)面:到目前為止我們已經(jīng)將Window服務(wù)宿主配置完畢,現(xiàn)在保存,關(guān)閉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宿主,同時(shí)適用IIS、控制臺(tái),因此后面關(guān)于IIS以及控制臺(tái)宿主的發(fā)布不再重復(fù)以上配置

  • 為服務(wù)添加安裝程序

在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:

  • 啟動(dòng)BookServiceHost服務(wù)

圖45:

  • 測(cè)試服務(wù):打開IE,在地址欄中輸入:http://localhost:8081/service出現(xiàn)下面的界面

圖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)

成都app開發(fā)公司