调用动态添加到服务契约的操作 [英] Call an operation that was dynamically added to the service contract

查看:22
本文介绍了调用动态添加到服务契约的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务合同(比如 IService1),我动态地向其中添加了一个操作,就像描述的 此处.当我只有一个 IService1 透明代理和通过 ClientChannelFactory 创建的 IClientChannel 时,如何从客户端调用动态添加的操作?

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?

更新

我可以使用 这个方法.

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

是否可以使用假消息调用 realyProxy.Invoke(IMessage) 以欺骗代理调用动态添加的方法?

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

推荐答案

用这个替换 GeneratePingMethod:

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

并创建您的客户:

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

    }
}

这篇关于调用动态添加到服务契约的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆