繁体   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