2012-03-08 13 views
0

私はhereのような操作を動的に追加するWCFサービス契約(IService1など)を持っています。 私が持っているのは、IService1トランスペアレントプロキシとIClientChannelがClientChannelFactory経由で作成されたときに、クライアント側から動的に追加された操作を呼び出す方法はありますか?サービス契約に動的に追加された操作を呼び出す

更新

私は透過プロキシからのRealProxyを得ることができますthis methodを使用してのChannelFactoryから返されました。

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

は、動的に追加したメソッドを呼び出すにプロキシをだまして偽のメッセージをrealyProxy.Invoke(IMessage)を呼び出すことが可能でしょうか?

答えて

0

この1とGeneratePingMethodを置き換えます

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

、そのように、あなたのクライアントを作成します。

// 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

を、私は解決策は、様々なサービス間で再利用できるようにしたいので、これは、許容可能なソリューションではありません、私はちょうど行動としてそれをフックしたい。私はすべての私のサービスインタフェースが、追加のメソッドが定義されているインタフェースから継承しなければならないという考えが嫌いです。 – m0sa

+0

"私のすべてのサービスインターフェイスは、追加のメソッドが定義されているインターフェイスから継承しなければならないという考えが嫌いです。" 私の提案する解決策はあなたにこの制約を課していません。単にサービスインタフェースを持っていて、動的WCFオペレーション契約が必要な場合は、新しいオペレーション契約の定義でインタフェースを拡張してください。 – CSharpenter

+0

しかし、あなたは新しい拡張インターフェースをクライアントに課します。クライアントのchannelfactoryがIExtendedInterfaceのインスタンスを作成しても元のインスタンスを使用することは望ましくありません。 ChannelFactoryを拡張して(例えば、ビヘイビアを追加するなど)、RealProxyなどを拡張することによって実現することによって、これを達成する方法であるに違いありません。 – m0sa

関連する問題