简体   繁体   中英

Proxy for interacting with WCF services on client

Please, Help me ! I've some problems with my project (WPF with WCF). My project its client-server interaction. In server I've EF that interaction with PatternRepository . On server it`sa wcf interaction I have services. Each service its repository. In each service I have a set of commands for communication between server and client . Between client and server data transfer occurs via Json . Example, it's service:

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();
    }
}

Example, its ProductSeviceLogics that interaction with ProductService :

    ProductRepositoryServiceClient _service1Client;

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

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

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

It's mean that if I'll create services. I'll be create those methods for each service on the server and the client. I think that it's very bad .

So my question, How can i do so that these methods will be for all services ? That is, I want not to create this methods for each service and for each client.

I recommend you to have a look into this ASP.NET MVC Solution Architecture article. Download the code and have a look into that, how they maintain the Repositories/Services/Models in separate class library and make use in User interface or WCF or WebAPI.

Here I will provide some sample solution pattern.

Create a new blank solution : File -> New Project -> Other Project Type -> Blank Solution and name it as MyProject.

Create new class library listed below

MyProject.Model

  • Create POCO class like Product, Sale

MyProject.Data

  • Add a reference of Model.

  • Contains EF(DbSet and DbContext) and Repositories like ProductRepository, SalesRepository.

MyProject.Service

  • Add reference Model and Data.
  • Make a call to your repositories from this project.

Create User Interface and WCF services

MyProject.Web

MyProject.WCF

Add a reference of Model and Service.

Your work flow like this WCF or Web Calls --> Service --> Repositories --> EF, So you can avoid creating multiple service for client and server.

Hope this helps you.

I solved this issue. Generate proxy for WCF Service

Generate Proxy by implementing ClientBase class*

Generating proxy by using ClientBase class option has an advantage that it creates proxy at run time, so it will accommodate service implementation changes. Let's follow the steps to generate proxy.

Add a Client Project to solution named as “ClientApp2″ that is basically a Console Application.

enter image description here

Add reference of StudentService Project to ClientApp2. Add following proxy class using ClientBase as:

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

Note: Don't forget to add “using StudentService” to class.

Following is the code for program.cs class in ClientApp2. We are using above created proxy class to communicate with WCF Service “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();
    }
}

Note: Don't forget to add “using System.ServiceModel” to class.

 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>

Now, when we run the client application, we will receive the following same output as we get in earlier option “Adding Service Reference”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM