WCF服務類庫、宿主、web客戶端配置

參照webcast的一個實例想了解一下wcf開發Data Contract中的KnowType相關使用,結果源代碼無法正常編譯,看來看去可能是Service References沒有更新,哪知道Service References下面的東西是localhost.map、localhost.cs,哭自己都不知道這個map爲何物啊,(稍後還要再去學習一下這個東西),還是先自己重新創建一個實例工程慢慢來吧,折騰了許久纔好,原來自己之前所謂會使用wcf都是在自欺欺人啊,所謂”知其然而不知其所以然“,失敗....

         這裏給自己留一個”足跡“吧,等真正懂的時候也好知道自己原來是多”無知“,O(∩_∩)O~

WCF服務類庫:WcfServiceLibrary

        只有接口IGigManagerService和其實現類GigManagerService,沒有配置文件

IGigManagerService.cs:

    [ServiceContract(Name = "GigManagerServiceContract", Namespace = "http://www.thatindigogirl.com/samples/2006/06", SessionMode = SessionMode.Required)]
    [ServiceKnownType(typeof(GigInfo))]
    public interface IGigManagerService
    {
        [OperationContract]
        [ServiceKnownType(typeof(GigInfo))]
        void SaveGig(LinkItem item);

        [OperationContract]
        [ServiceKnownType(typeof(GigInfo))]
        LinkItem GetGig();
    }

宿主工程:Host(需要添加對WcfServiceLibrary的引用)

        是console項目,有配置文件App.config和Program.cs,

App.config中主要的配置節:

<system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.GigManagerService" behaviorConfiguration="metadataBehaver" >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"/>
          </baseAddresses>
        </host>
        <endpoint binding="wsHttpBinding" contract="WcfServiceLibrary1.IGigManagerService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
	  <behaviors>
		  <serviceBehaviors>
        <behavior name="metadataBehaver">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
		  </serviceBehaviors>
	  </behaviors>
  </system.serviceModel>
Program.cs:

        static void Main(string[] args)
        {

            using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.GigManagerService)))
            {
                host.Open();

                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate Host");
                Console.ReadLine();
            }
        }

web客戶端:GigEntryWeb(添加服務引用GigManagerService)

         配置文件Web.config的主要配置節:

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_GigManagerServiceContract" >
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_GigManagerServiceContract"
        contract="GigManagerService.GigManagerServiceContract" name="WSHttpBinding_GigManagerServiceContract">
        <identity>
          <userPrincipalName value="betty-PC\betty" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
      客戶端初始化:

GigManagerService.GigManagerServiceContractClient m_proxy = new GigManagerService.GigManagerServiceContractClient();//其中GigManagerService是服務引用名稱

發佈了14 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章