Public Class ClientBase(Of TChannel As Class)
Private requestChannel As IRequestChannel
Private messageVersion As MessageVersion
Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
'this.remoteAddress = remoteAddress;
Me.messageVersion = binding.MessageVersion
Dim parameters = New System.ServiceModel.Channels.BindingParameterCollection()
' Specifies the X.509 certificate used by the client.
Dim cc As New ClientCredentials()
cc.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "testuser")
parameters.Add(cc)
Dim channelFactory As IChannelFactory(Of IRequestChannel)
channelFactory = binding.BuildChannelFactory(Of IRequestChannel)(parameters)
channelFactory.Open()
Me.requestChannel = channelFactory.CreateChannel(remoteAddress)
End Sub
Public Function [Call](ByVal op As String, ByVal action As String, ByVal varnames() As String, ByVal varvals() As Object, ByVal returntype As Type) As Object
requestChannel.Open(TimeSpan.MaxValue)
'Message msg =
'Message.CreateMessage(MessageVersion.<FromBinding>,
' action,
' new CustomBodyWriter(op, varnames, varvals,
'"<ns passed in from Proxy>"));
Dim msg As Message = Message.CreateMessage(Me.messageVersion, action, New CustomBodyWriter(op, varnames, varvals, "<ns passed in from Proxy>"))
Dim reply As Message = requestChannel.Request(msg, TimeSpan.MaxValue)
Dim reader As XmlDictionaryReader = reply.GetReaderAtBodyContents()
reader.ReadToFollowing(op + "Result")
Return reader.ReadElementContentAs(returntype, Nothing)
End Function
End Class
Friend Class CustomBodyWriter
Inherits BodyWriter
Private op As String
Private varnames() As String
Private varvals() As Object
Private ns As String
Public Sub New(ByVal op As String, ByVal varnames() As String, ByVal varvals() As Object, ByVal ns As String)
MyBase.New(True)
Me.op = op
Me.varnames = varnames
Me.varvals = varvals
Me.ns = ns
End Sub
Protected Overrides Sub OnWriteBodyContents(ByVal writer As XmlDictionaryWriter)
writer.WriteStartElement(op, ns)
Dim i As Integer
For i = 0 To varnames.Length
writer.WriteElementString(varnames(i), varvals(i).ToString())
Next i
writer.WriteEndElement()
End Sub
End Class
public class ClientBase<TChannel>
where TChannel : class
{
private IRequestChannel requestChannel;
private MessageVersion messageVersion;
public ClientBase(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
{
//this.remoteAddress = remoteAddress;
this.messageVersion = binding.MessageVersion;
BindingParameterCollection parameters = new System.ServiceModel.Channels.BindingParameterCollection();
// Specifies the X.509 certificate used by the client.
ClientCredentials cc = new ClientCredentials();
cc.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "testuser");
parameters.Add(cc);
IChannelFactory<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>(
parameters);
channelFactory.Open();
this.requestChannel = channelFactory.CreateChannel(remoteAddress);
}
public object Call(string op, string action, string[] varnames, object[] varvals, Type returntype)
{
requestChannel.Open(TimeSpan.MaxValue);
//Message msg =
//Message.CreateMessage(MessageVersion.<FromBinding>,
// action,
// new CustomBodyWriter(op, varnames, varvals,
//"<ns passed in from Proxy>"));
Message msg =
Message.CreateMessage(this.messageVersion, action,
new CustomBodyWriter(op, varnames, varvals,
"<ns passed in from Proxy>"));
Message reply = requestChannel.Request(msg, TimeSpan.MaxValue);
XmlDictionaryReader reader = reply.GetReaderAtBodyContents();
reader.ReadToFollowing(op + "Result");
return reader.ReadElementContentAs(returntype, null);
}
}
internal class CustomBodyWriter : BodyWriter
{
private string op;
private string[] varnames;
private object[] varvals;
private string ns;
public CustomBodyWriter(string op, string[] varnames, object[] varvals, string ns)
: base(true)
{
this.op = op;
this.varnames = varnames;
this.varvals = varvals;
this.ns = ns;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement(op, ns);
for (int i = 0; i < varnames.Length; i++)
writer.WriteElementString(varnames[i], varvals[i].ToString());
writer.WriteEndElement();
}
}