Call an operation that was dynamically added to the service contract

我与影子孤独终老i 提交于 2019-12-13 04:20:14

问题


I have a WCF service contract (say IService1) to which I dynamically add an operation like described here. How could I call a dynamically added operation from the client-side when all I have is an IService1 transparent proxy and the IClientChannel created via the ClientChannelFactory?

Update

I can get the RealProxy from the transparent proxy returned from the ChannelFactory using this method.

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

Would it be possible to call realyProxy.Invoke(IMessage) with a fake message to trick the proxy into calling the dynamically added method?


回答1:


Replace the GeneratePingMethod with this one:

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

and create your clients as such:

// 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());

    }
}


来源:https://stackoverflow.com/questions/9617930/call-an-operation-that-was-dynamically-added-to-the-service-contract

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!