我有一个WCF服务合同(比如IService1),我动态地添加了一个像here描述的操作。 当我拥有的是IService1透明代理和通过ClientChannelFactory创建的IClientChannel时,如何从客户端调用动态添加的操作?
更新
我可以使用this method从ChannelFactory返回的透明代理中获取RealProxy。
var realProxy = System.Runtime.Remoting.RemotingServices.GetRealProxy( transparentProxy );
是否可以使用假邮件调用realyProxy.Invoke(IMessage)
来欺骗代理调用动态添加的方法?
答案 0 :(得分:0)
将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());
}
}