2012-08-24 2 views
0

현재 WCF 서비스를 만들고 명령 줄 응용 프로그램에서 호스팅하려고합니다. 서비스를 호스팅하는 동안 MyProxy라는 객체의 인스턴스로 초기화하려고합니다. 서비스 호출은 모두 MyProxy에 위임해야합니다.WCF 서비스 초기화 문제

나는 서비스를 만들고 전화를 걸었습니다. 그러나 서비스에서 MyProxy 인스턴스를 초기화 할 수 없습니다. 항상 null입니다. 따라서 서비스 호출이 이루어지면 프록시에 위임 할 수 없습니다.

저는 지난 이틀 동안이 작업을하고 있습니다. 지금 무슨 일이 일어나고 있는지 모르겠다. 친절하게 도와주세요. 나는 또한 Debugger.Launch을 (넣어

class MyServiceLauncher 
{ 
    ServiceHost host; 
    IMasOperations proxy; 
    ChannelFactory<IMasOperations> factory; 

    public void StartService(MyProxy proxyInstance) 
    { 
     string baseAddress = "http://localhost:8730/Design_Time_Addresses/MASService/Service1"; 
     host = new ServiceHost(typeof(MasOperationsService), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(IMasOperations), GetBinding(), ""); 
     host.Description.Behaviors.Add(new MyServiceBehavior()); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     factory = new ChannelFactory<IMasOperations>(GetBinding(), new EndpointAddress(baseAddress)); 
     proxy = factory.CreateChannel(); 

     using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy)) 
     { 
      OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Name", "http://my.namespace", proxyInstance)); 
     } 
    } 

    public void ShutDownService() 
    { 
     ((IClientChannel)proxy).Close(); 
     factory.Close(); 
     host.Close(); 
    } 

    static Binding GetBinding() 
    { 
     BasicHttpBinding result = new BasicHttpBinding(); 
     return result; 
    } 
} 

서비스를 사용하여 시작됩니다

public class MasOperationsService : IMasOperations 
{ 
    //This MyProxy instance should be used to delegate all calls to service. 
    public MyProxy myProxyInstance; 

    public MasOperationsService() 
    { 
     myProxyInstance = null; 
    } 
    public MasOperationsService(MyProxy proxy) 
    { 
     myProxyInstance = proxy; 
    } 

    public CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long authorCellId, uint levelsToExtract) 
    { 
     //The service will delegate the call to MyProxy. 
     //myProxyInstance is always null 

     return myProxyInstance.GetProxyData(...); 
    } 
} 

public class MyInstanceProvider : IInstanceProvider 
{ 
    public object GetInstance(InstanceContext instanceContext, Message message) 
    { 
     MyProxy name = message.Headers.GetHeader<MyProxy>("Name", "http://my.namespace"); 
     if (name != null) 
     { 
      return new MasOperationsService(name); 
     } 
     return null; 
    } 
    public object GetInstance(InstanceContext instanceContext) 
    { 
     return new MasOperationsService(null); 
    } 
    public void ReleaseInstance(InstanceContext instanceContext, object instance) 
    { 
    } 
} 

public class MyServiceBehavior : IServiceBehavior 
{ 
    MyInstanceProvider myProvider = new MyInstanceProvider(); 
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { } 
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) 
     { 
      foreach (EndpointDispatcher ed in cd.Endpoints) 
      { 
       ed.DispatchRuntime.InstanceProvider = this.myProvider; 
      } 
     } 
    } 
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } 
} 

는) 거의 모든 곳에서, 그냥 초기화 (IInstanceProvider 서비스와의 두 생성자)되는 것을 볼 수 있습니다. 복용량이 해고됩니다.

+0

MyInstanceProvider 클래스의 GetInstance (InstanceContext instanceContext, Message message) 메소드를 호출 할 코드를 제공 할 수 있습니까? 또한 계약 정의를 제공하는 것이 좋습니다. –

+0

Visual Studio에 코드를 붙여 넣었으므로 컴파일하지 않았기 때문에 아무 것도 할 수 없습니다 (MyProxy 및 다른 유형은 정의되지 않음). 디버깅에 도움을 요청하고 있습니다. 내가 디버그 세션을 쉽게 할 수 있다면 그렇게 할 것이다. 그렇지 않으면 아마 대답을 얻지 못할 것입니다. – ErnieL

+0

@kobac - 어떻게하고 왜해야하는지 잘 모르겠습니다. 현재 저는 클라이언트에서 MasOperationsClient라는 서비스 클래스의 객체를 만들고 ExtractCoAuthorsFromAuthor() 메서드를 호출합니다. –

답변

0

누락 된 유형은 다음과 같습니다. 또한이 서비스에서 전화가 있습니다 return myProxyInstance.GetProxyData (...); 도트를 제거하면 응용 프로그램이 컴파일됩니다.

[ServiceContract] 
public interface IMasOperations 
{ 
    [OperationContract] 
    CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long AuthorCellId, uint LevelsToExtract); 
} 

public class CoAuthorSearchResult 
{ } 

public class MyProxy 
{ 
    public CoAuthorSearchResult GetProxyData() 
    { 
     return new CoAuthorSearchResult(); 
    } 
} 

@kobac 가하는 GetInstance가 호출되고 보여주는 코드 조각에 대해 물었다. 어떻게 그리고 왜해야하는지 잘 모르겠습니다.

현재 클라이언트에서 MasOperationsClient 서비스 클래스의 개체를 만들고 ExtractCoAuthorsFromAuthor() 메서드를 호출합니다.