簡體   English   中英

我如何使用WCF數據服務?

[英]How do i Consume WCF Data Service?

我已經創建了一個wcf服務,但我已經使用了3個項目;
1)ServiceLibrary(WCF庫)
2)網絡
3)ConsoleTestClient
我的ServiceLibrary app.config文件看起來像這樣;

  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel> <br />

現在,為了托管這個庫,我在Web Project的Web.Config文件中完成了以下設置。
svc文件名是WcfDataService1.svc

    public class WcfDataService1 : DataService<AdvertisementService>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/WcfDataService1.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

現在,當我使用WCF測試客戶端直接使用(ServiceLibrary項目)測試此服務時,我看到以下內容並且一切都很好; 在此輸入圖像描述
問題是當我嘗試運行我的Web項目(我用作wcf服務的主機)。 然后轉到控制台測試客戶端,並希望使用添加引用添加引用。 我沒有看到我的GetSet方法(比如測試客戶端) 在此輸入圖像描述 為什么我沒有看到我的IAdvertisementService接口和方法
我是否必須將其部署到執行IIS?

若要使用ASP.NET開發服務,我們必須將WebService屬性添加到類中,並將WebMethodAttribute添加到任何類方法中。

[WebService] 
 public class Service : System.Web.Services.WebService 
  { 
  [WebMethod] 
  public string Test(string strMsg) 
  { 
      return strMsg; 
  } 
 }

要在WCF中開發服務,我們將編寫以下代碼:

[ServiceContract] 
public interface ITest 
{ 
   [OperationContract] 
   string ShowMessage(string strMsg); 
 } 


public class Service : ITest 
   { 
       public string ShowMessage(string strMsg) 
       { 
          return strMsg; 
       } 
   }

ServiceContractAttribute指定接口定義WCF服務合同,OperationContract屬性指示接口的哪些方法定義服務合同的操作。

實現服務合同的類在WCF中稱為服務類型。

托管服務

ASP.NET Web服務被編譯為類庫程序集,擴展名為.asmx的服務文件將具有該服務的代碼。 該服務文件將被復制到ASP.NET應用程序的根目錄中,而Assembly將被復制到bin目錄中。 可以使用服務文件的URL訪問該應用程序。

WCF服務可以在IIS或WindowsActivationService中托管。

將服務類型編譯到類庫中將擴展名為.SVC的服務文件復制到虛擬目錄中,然后將其匯編到虛擬目錄的bin子目錄中。 將web.config文件復制到虛擬目錄中。

客戶開發

使用命令行工具WSDL.EXE生成ASP.NET Web服務的客戶端。

WCF使用ServiceMetadata工具(svcutil.exe)生成服務的客戶端。

有關更多詳細信息, 請訪問此鏈接http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

上一篇文章刪除:


更新:

Microsoft開發人員網絡實際上涵蓋了這一點,它們提供的一些資源包括:

還有幾本書我解決了這個特別的努力。 由於有人說提供解決此問題的鏈接並不能真正回答您將要嘗試解決的問題。

  1. 在Visual Studio中單擊“ 文件” ,然后繼續“ 新建項目”
  2. 在對話框中,展開Visual C# ,選擇“ Web”和“ ASP.NET Web窗體應用程序”
  3. 為您的項目命名您選擇的NorthwindWeb

至此,您已經創建了一個項目。 由於服務的復雜性,忽略微小的細節可能會導致災難性的后果。 這就是為什么我從頭開始。

在我的示例中,我將其鏈接到數據庫 所以我將添加一個Ado.Net實體數據模型 我將命名我的模型: NorthwindModel 我還將基於現有數據庫進行生成。 所以在這一點上只需遵循Visual Studio向導。 在這些表中選擇數據庫對象 ,然后單擊完成。

重要的部分,構建我的Data Service

  1. 項目添加新項目
  2. 選擇Web並選擇WCF數據服務
  3. 放一個名字, NorthwindCustomer - 然后添加

找到第一個Todo:注釋並刪除代碼然后放入:

public class DemonDbCustomer : DataService<demonDbEntities>

然后在InitializeService事件處理程序中找到注釋:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

此時按CTRL + F5運行該服務。 瀏覽器將打開,並且將生成該服務的XML模式。 地址欄中 ,在NorthwindCustomers.svc的URL末尾鍵入客戶 ,然后按Enter

**有時Internet Explorer會將其弄亂,因此可能需要進行其他故障排除。 **

現在,我們將創建客戶部分。

  1. 添加 新項目
  2. 選擇Windows窗體應用程序
  3. 將文件命名為NorthwindClient然后單擊確定
  4. Solution Explorer中,選擇NorthwindClient ProjectSet As Startup Project
  5. 右鍵單擊項目: 添加服務引用單擊“ 發現”

此時,您NorthwindCustomers Service的URL將顯示在該“ 地址”字段中。 這是從.svc文件生成的。

現在我們必須提供數據綁定到我們的服務。

  1. 數據菜單上,我們要顯示數據源
  2. 添加新數據源
  3. 選擇Data Source的類型,然后按照向導(單擊對象)。
  4. 選擇您想要綁定的對象。

現在,此時您只需要創建一個User Interface 為此,只需將“ Customers NodeData Sources拖動到Form

  • DataGridView
  • BindingSource
  • BindingNavigation

它們都是自動添加的。 然后只需雙擊您的Form並將以下內容添加到Form1_Load Event Handler

ServiceReference1.northwindModel.northwindEntities proxy = new 
     ServiceReference1.northwindModel.northwindEntities(new
         Uri("http://localhost:53397/NorthwindCustomers.svc/"));

// As you see it pointed to our SVC file, because that includes our Address, Binding, Contract information.

this.customersBindingSource.DataSource = proxy.Customers;

現在,在解決方案資源管理器中,右鍵單擊NorthwindCustomers.svc ,然后單擊“在瀏覽器中查看” 將添加XML模式,因此您只需從地址欄中復制該URL。 然后用您剛復制的Uri替換Uri

運行您的應用程序,您已經完成以下操作:

  • 主辦
  • 客戶
  • 服務
  • 消費

這就是使用WCF數據服務的方式, 這里有更多詳細信息

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM