簡體   English   中英

與客戶端上的WCF服務進行交互的代理

[英]Proxy for interacting with WCF services on client

請幫我 ! 我的項目存在一些問題(帶有WCF的WPF)。 我的項目是客戶端與服務器的交互。 在服務器中,我具有與PatternRepository進行交互的功能。 在服務器上,有wcf交互,我有服務。 每個服務都有其存儲庫。 在每個服務中,我都有一組用於服務器和客戶端之間通信的命令。 在客戶端和服務器之間,通過Json進行數據傳輸。 例如,它是服務:

public class ProductRepositoryService : IProductRepositoryService
{
   public void AddProduct(string json)
    {
        _productRepository.Add(wrapperProduct.DeserializeProduct(json));
        _productRepository.Save();
    }

    public void DeleteProduct(string json)
    { productRepository.Delete(_productRepository.GetById(wrapperProduct.DeserializeProduct(json).Id));
        _productRepository.Save();
    }
}

例如,與ProductService交互的ProductSeviceLogics:

    ProductRepositoryServiceClient _service1Client;

    public ProductSeviceLogics()
    {
      this._service1Client = new ProductRepositoryServiceClient();
    }

    public void AddProduct(string json)
    {
        _service1Client.AddProduct(json);
    }

    public void DeleteProduct(string json)
    {
        _service1Client.DeleteProduct(json);
    }

這意味着如果我要創建服務。 我將為服務器和客戶端上的每個服務創建這些方法。 我認為這很糟糕。

所以我的問題是,我該怎么做才能使這些方法適用於所有服務? 也就是說,我不想為每個服務和每個客戶端創建此方法。

我建議您閱讀此ASP.NET MVC解決方案體系結構文章。 下載代碼並研究它們如何在單獨的類庫中維護存儲庫/服務/模型,以及如何在用戶界面或WCF或WebAPI中使用它們。

在這里,我將提供一些示例解決方案模式。

創建一個新的空白解決方案:文件->新建項目->其他項目類型->空白解決方案,並將其命名為MyProject。

創建下面列出的新類庫

MyProject.Model

  • 創建產品,銷售等POCO類

MyProject.Data

  • 添加模型參考。

  • 包含EF(DbSet和DbContext)和諸如ProductRepository,SalesRepository之類的存儲庫。

MyProject.Service

  • 添加參考模型和數據。
  • 調用此項目中的存儲庫。

創建用戶界面和WCF服務

MyProject.Web

我的項目

添加模型和服務的參考。

您的工作流程就像WCF或Web調用->服務->存儲庫-> EF,因此您可以避免為客戶端和服務器創建多個服務。

希望這對您有所幫助。

我解決了這個問題。 生成WCF服務的代理

通過實現ClientBase類生成代理*

通過使用ClientBase類選項生成代理的優點是,它在運行時創建代理,因此可以適應服務實現的更改。 讓我們按照步驟生成代理。

將客戶端項目添加到名為“ ClientApp2”的解決方案中,該項目基本上是一個控制台應用程序。

在此處輸入圖片說明

將StudentService項目的引用添加到ClientApp2。 使用ClientBase添加以下代理類:

public class StudentServiceProxy : ClientBase<IStudentService>, IStudentService
 {
     public string GetStudentInfo(int studentId)
     {
           return base.Channel.GetStudentInfo(studentId);
     }
 }

注意:不要忘記在課程中添加“ using StudentService”。

以下是ClientApp2中program.cs類的代碼。 我們正在使用上面創建的代理類與WCF服務“ StudentService”進行通信。

   class Program
   {
     static void Main(string[] args)
    {
                StudentServiceProxy myclient;
                myclient = new StudentServiceProxy();

                int studentId = 1;
                Console.WriteLine(“Calling StudentService with StudentId =1…..”);
                Console.WriteLine(“Student Name = {0}”, myclient.GetStudentInfo(studentId));
                Console.ReadLine();
    }
}

注意:不要忘記在類中添加“ using System.ServiceModel”。

 App.Config file will have following configuration: 
 <system.serviceModel>
      <bindings>
          <wsHttpBinding>
              <binding name=”WSHttpBinding_IStudentService” />
          </wsHttpBinding>
      </bindings>
      <client>
           <endpoint address=”http://localhost:4321/StudentService”
                            binding=”wsHttpBinding”
                            bindingConfiguration=”WSHttpBinding_IStudentService”
                            contract=”StudentService.IStudentService”
                            name=”WSHttpBinding_IStudentService”>
           </endpoint>
       </client>

現在,當我們運行客戶端應用程序時,我們將收到與先前選項“添加服務引用”中相同的以下輸出。

暫無
暫無

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

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