2012-03-08 3 views
0

here과 같은 작업을 동적으로 추가하는 WCF 서비스 계약 (IService1)이 있습니다. 내가 가지고있는 것이 모두 IService1 투명 프록시이고 IClientChannel이 ClientChannelFactory를 통해 생성되었을 때 어떻게 클라이언트 측에서 동적으로 추가 된 작업을 호출 할 수 있습니까?서비스 계약에 동적으로 추가 된 작업 호출

업데이트

투명 프록시가 this method를 사용하여 ChannelFactory에에서 반환에서 나는 RealProxy를 얻을 수 있습니다.

var realProxy = System.Runtime.Remoting.RemotingServices.GetRealProxy(transparentProxy); 

IT는 동적으로 추가 메소드를 호출에 프록시를 속여 가짜 메시지와 함께 realyProxy.Invoke(IMessage)를 호출 할 수 있을까요? ,

// this is your base interface 
[ServiceContract] 
public interface ILoginService 
{ 
    [OperationContract(Action = "http://tempuri.org/LoginService/Login", Name = "Login")] 
    bool Login(string userName, string password); 
} 

[ServiceContract] 
public interface IExtendedInterface : ILoginService 
{ 
    [OperationContract(Action = "http://tempuri.org/LoginService/Ping", Name="Ping")] 
    DateTime Ping(); 
} 


class Program 
{ 
    static void Main(string[] args) 
    { 
     IExtendedInterface channel = null; 
     EndpointAddress endPointAddr = new EndpointAddress("http://localhost/LoginService"); 
     BasicHttpBinding binding = new BasicHttpBinding(); 

     channel = ChannelFactory<IExtendedInterface>.CreateChannel(binding, endPointAddr); 

     if (channel.Login("test", "Test")) 
     { 
      Console.WriteLine("OK"); 
     } 
     DateTime dt = channel.Ping(); 

     Console.WriteLine(dt.ToString()); 

    } 
} 

답변

0

이 하나로 GeneratePingMethod 교체 나는 단지 그것을 행동으로 끌어 들이고 싶다. 나는 모든 서비스 인터페이스가 추가 메서드가 정의 된 인터페이스에서 상속되어야한다는 생각을 좋아하지 않는다.
+0

나는이 솔루션은 다양한 서비스를 통해 재사용되고 싶어하기 때문에 이것은, 수용 가능한 해결책이 아니다 :

private static void GenerateNewPingMethod(ServiceHost sh) { foreach (var endpoint in sh.Description.Endpoints) { ContractDescription contract = endpoint.Contract; OperationDescription operDescr = new OperationDescription("Ping", contract); MessageDescription inputMsg = new MessageDescription(contract.Namespace + contract.Name + "/Ping", MessageDirection.Input); MessageDescription outputMsg = new MessageDescription(contract.Namespace + contract.Name + "/PingResponse", MessageDirection.Output); MessagePartDescription retVal = new MessagePartDescription("PingResult", contract.Namespace); retVal.Type = typeof(DateTime); outputMsg.Body.WrapperName = "PingResponse"; outputMsg.Body.WrapperNamespace = contract.Namespace; outputMsg.Body.ReturnValue = retVal; operDescr.Messages.Add(inputMsg); operDescr.Messages.Add(outputMsg); operDescr.Behaviors.Add(new DataContractSerializerOperationBehavior(operDescr)); operDescr.Behaviors.Add(new PingImplementationBehavior()); contract.Operations.Add(operDescr); } } 

과 같은 클라이언트를 만들 : – m0sa

+0

"모든 서비스 인터페이스가 추가 메서드가 정의 된 인터페이스에서 상속되어야한다는 생각은 마음에 들지 않습니다." 내 제안 된 솔루션이 당신 에게이 제약 조건을 부과하지 않습니다. 간단하게 서비스 인터페이스를 사용하고 동적 WCF 운영 계약이 필요할 때 새로운 운영 계약의 정의로 인터페이스를 확장하면됩니다. – CSharpenter

+0

하지만 내 확장 인터페이스를 클라이언트에 적용해야합니다. 내 클라이언트 channelfactory가 IExtendedInterface의 인스턴스를 만들지 만 원래의 인스턴스를 계속 사용하고 싶지는 않습니다. 확실히 ChannelFactory를 확장하여 (예 : 비헤이비어 추가) RealProxy 등을 확장하여이 작업을 수행하는 방법이어야합니다. – m0sa

관련 문제