2015-02-04 2 views
0

아래 WebApi 컨트롤러가 있습니다. 이 컨트롤러에는 내부적으로 CustomerDataService이라는 고객 레코드 업데이트 서비스를 호출하는 Update 메서드가 있습니다. 업데이트 할 고객 레코드가 있습니다.아래에 설명 된 시나리오에서 비동기 적으로 일부 작업을 수행하는 C#으로 서비스를 만드는 방법은 무엇입니까?

CustomerDataService의 UpdateMethod가 업데이트를 수행하고 업데이트 응답을 반환합니다.

데이터 조작/데이터 캐시 관리와 같은 업데이트 응답 후에 비동기 적으로 처리해야하는 요구 사항이 있습니다. Update가 성공적으로 수행되면서이 처리는이 API의 사용자와 관련이 없으므로 시간이 오래 걸리기 때문에이 작업을 비동기 적으로 수행해야합니다. 주어진 시나리오에서 C#으로이 작업을 수행 할 수 있습니까? 제발 제안 해주세요.

참고 :가 나는 사용자 세션이 특정 작업 (들)을 수행 할로 이것을 달성하기 위해 어떠한 배치 작업을 생성하고 싶지 않아요.

컨트롤러

public class CustomerController : ApiController 
{ 
     [HttpGet] 
     public string UpdateCustomer() 
     { 
      ICustomerService obj = new CustomerDataService(); 
      return obj.UpdateCustomer(GetCustomerList());   
     } 

     private List<CustomerModel> GetCustomerList() 
     { 
      return new List<CustomerModel>() 
      { 
        new CustomerModel 
        { 
         CustomerId="1", 
         Name="John", 
         Category="P1"      
        }, 
        new CustomerModel 
        { 
         CustomerId="2", 
         Name="Mike", 
         Category="P2"      
        } 
        //....n Records 
       }; 
     } 
} 

모델

[Serializable] 
[DataContract] 
public class CustomerModel 
{ 
    [DataMember] 
    public string CustomerId { get; set; } 
    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public string Category { get; set; } 
} 

인터페이스 및 CustomerDataService

public interface ICustomerService 
{ 
    string UpdateCustomer(List<CustomerModel> customerList); 
} 

public class CustomerDataService : ICustomerService 
{ 
    public string UpdateCustomer(List<CustomerModel> customerList) 
    { 
      //Do Data Processing - DB Call 
      //Return Confirmation Message 
      return "Data Updated Successfully!!!"; 

      //Needs to perform some processing asynchronously i.e. Call ProcessResults() 
    } 

    private void ProcessResults() 
    { 
      //DO Processing 
    } 
} 

답변

0

C#에서 async/await를 사용하고 계신가요? Microsoft 웹 사이트 : Asynchronous Programming with Async and Await에서이 기사를 참조하십시오. 다음은 많은 예제가 들어있는 또 다른 기사입니다 : C# Async, Await.

이것이 어떻게 작동하는지 이해하면이 패턴을 이용하기 위해 코드를 변경하는 것이 매우 쉽습니다. 특정 질문이 있거나 문제가 생기면 알려주십시오.

관련 문제