Step by Step 建立一個WCF Service

 

原創地址:http://www.cnblogs.com/jfzhu/p/4025448.htmlhtml

轉載請註明出處post

 

(一)建立WCF Service

(1)建立WCF Service類庫ui

 

建立一個Class Library的項目:spa

image

 

刪除掉默認的Class1.cs文件,而後添加一個WCF Service項目:3d

image

 

Visual Studio會自動幫助你生成兩個文件:HelloService.cs 和 IHelloService.cs,另外還自動添加了System.ServiceModel引用,它是WCF的核心。code

image

 

 

修改IHelloService.cs和HelloService.cs文件。orm

IHelloService.cs:xml

 

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string GetMessage(string name);
    }
}

 

HelloService.cs:htm

 

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.
    public class HelloService : IHelloService
    {

        public string GetMessage(string name)
        {
            return "Hello " + name;
        }
    }
}

 

(2)建立WCF的Hostblog

添加一個新的ASP.NET Empty Web Application:

image

 

添加一個新Item WCF Service

image

image

 

刪除HelloService.svc.cs和IHelloService.cs文件。

添加HelloService Class Library的項目引用:

 

image

 

修改HelloService.svc爲:

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

  

Web.config:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

 

其中,service name=」命名空間.類名」,behaviorConfiguration是用來關聯下面behavior的定義的。

endpoint address中定義的是相對地址,與baseAddress結合起來爲完整地址

endpoint contract=」命名空間.接口名」

兩個endpoint,第一個binding是basicHttpBinding,用於HTTP協議;第二個endpoint用於交換metadata,binding爲mexHttpBinding。

其中behavior的定義是用來容許交換metadata的。

 

Build解決方案,若是沒有錯誤就進行到下一步,部署WCF Service到IIS

 

(二)部署WCF Service到IIS

(1)Publish HelloServiceIISHost項目

image

image

image

image

image

image

 

(2)部署到IIS

image

image

image

 

瀏覽HelloService.svc

image

image

 

 

(三)建立一個Windows Form來調用WCF Service

image

 

添加一個服務引用:

image

image

image

image

 

private void button1_Click(object sender, EventArgs e) 
{ 
    HelloService.HelloServiceClient client = new HelloService.HelloServiceClient(); 
    label1.Text = client.GetMessage(textBox1.Text); 
} 

 

運行代碼,效果以下:

image

 

(四)總結

svc文件中,包含着服務指令,Service屬性指明文件指向的是哪一個服務

<%@ ServiceHost Language="C#" Debug="true" Service="HelloService.HelloService" %>

 

service的代碼能夠在

(1) XXX.svc.cs的文件中

(2) 一個獨立的Assembly(如同本文)

(3) App_Code文件夾下

相關文章
相關標籤/搜索