2013-10-24 3 views
1

우리는 WCF 서비스에 여러 클라이언트가 연결되어 있습니다.wcf 서비스 생성 후킹

Google 서비스는 인스턴스 서비스별로 설정됩니다. 서비스가 작업을 수행하려면 다른 인스턴스 객체에 대한 액세스 권한이 있어야합니다. 필요한 인스턴스는 wcf 서비스가 아니며 필요한 인스턴스를 싱글 톤으로 만들지는 않습니다.

서비스가 내가 만든 객체 인 경우 상호 작용해야하는 인스턴스 객체를 전달합니다. 그러나 이것이 wcf에 의해 생성 된 wcf 서비스이기 때문에.

서비스 작성시 사용할 데이터/인터페이스를 전달하기 위해 어떻게해야합니까? 또는 필요한 인스턴스를 전달할 수 있도록 작성된 후 서비스에 대한 포인터를 얻는 방법은 무엇입니까?

팀 S. 제안으로
[ServiceContract]  
public interface IMyService 
{ 
    [OperationContract(IsOneWay = true)] 
    void DoSomethingCool(); 
} 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] 
class MyService : IMyService 
{ 
    private IHelper helper; 
    void DoSomethingCool() 
    { 
     // How do I pass an instance of helper to MyService so I can use it here??? 
     helper.HelperMethod(); 
    } 
} 
+1

당신이 [WCF의 의존성 삽입 (Dependency Injection)]를 본 적이 (http://msdn.microsoft.com/en-us/library/vstudio/hh273093 (절 = vs.100) .aspx) 기사? –

+0

@TimS 나는 그것을 보지 않고있다. – chollida

답변

0

좋아, 내가 한 일은 내 도우미 클래스를 싱글 톤이 아닌 중개자로 싱글 톤 ServiceGlue 개체로 만드는 것이 었습니다.

서비스와 도우미는 모두 싱글 톤인 중개자와 자체 등록되며 싱글 톤은 인스턴스를 앞뒤로 전달합니다.

내 서비스가 설정되기 전에 각 서비스가 도우미 객체를 가져올 수 있도록 싱글 톤을 인스턴스화하고 도우미를 등록합니다.

이 같은 내 코드 조회 수 :

public class ServiceGlue 
{ 
    private static ServiceGlue instance = null; 
    public static ServiceGlue Instance 
    { 
     get 
     { 
      if (instance == null) 
       instance = new ServiceGlue(); 
      return instance; 
     } 
    } 

    public Helper helper { get; set; } 
} 


[ServiceContract]  
public interface IMyService 
{ 
    [OperationContract(IsOneWay = true)] 
    void DoSomethingCool(); 
} 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode =  ConcurrencyMode.Multiple)] 
class MyService : IMyService 
{ 
    private IHelper helper; 
    public MyService() 
    { 
     // use an intermidiary singleton to join the objects 
     helper = ServiceGlue.Instance.Helper(); 
    } 

    void DoSomethingCool() 
    { 
     // How do I pass an instance of helper to MyService so I can use it here??? 
     helper.HelperMethod(); 
    } 
} 
+0

이것은 IoC 방법과 매우 유사합니다. 본질적으로 IoC 컨테이너 (모든 종속성을 등록)가있는 정적 클래스가 있으면 IoC.GetContainer()와 같은 호출을 할 것입니다. 해결 (); – gleng

1

, 당신은 의존성 주입으로 읽어야합니다 (자신의 의견에서, 링크는 여기 http://msdn.microsoft.com/en-us/library/vstudio/hh273093%28v=vs.100%29.aspx 찾을 수 있습니다). poor mans dependency injection이처럼 사용할 수 있습니다 :이 특정 구현을 원하는 경우

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] 
class MyService : IMyService 
{ 
    private IHelper _helper; 

    public MyService() : this(new Helper()) 
    { 

    } 

    public MyService(IHelper helper) 
    { 
     _helper = helper; 
    } 

    void DoSomethingCool() 
    {    
     helper.HelperMethod(); 
    } 
} 

,이 의존성을 해결하기 위해, IOC는를 얻기 위해 (Inversion of Control) 용기가 필요합니다. 이 중 톤이 있지만 특히 Castle Windsor를 사용합니다.

+0

이것은 Helper의 새로운 인스턴스를 생성하기 때문에 작동하지 않습니다. MyService에 전달 된 도우미의 특정 인스턴스가 필요합니다. 어떻게 작동하도록 샘플을 수정할 수 있습니까? – chollida

+0

@chollida 사실, 당신이 지금 설정 한 방식대로, 당신의 프로그램은 그 메소드를 호출하려고 할 때'null 참조 예외 '를 얻을 것이다. 의존성을 해결하기 위해 IoC (Inversion of Control) 컨테이너를 가져와야합니다. 이는 내가 보여준 것과 매우 유사합니다. – gleng

+0

> 참이지만 지금 설정 한 방식대로 프로그램에서 해당 메소드를 호출 할 때 null 참조 예외가 발생합니다. 네, 질문의 요점입니다 :) 어떻게이 메소드에 특정 인스턴스를 전달합니까 :) – chollida