WCF

WCF Examples

WCF

WCF Projects

WCF Project

WCF Channels Working
Previous Home Next

We can use channels directly to invoke operations on the service without ever resorting to using a proxy class.

The ChannelFactory<T> class:

ChannelFactory<T> class enables we to create a proxy on the fly.

public class contractexample
{
public Type ContractType
{get;set;}
//More members
}
public class ServiceEndpoint
{
public ServiceEndpoint(contractexample contract,Binding binding,
	   EndpointAddress address);
public EndpointAddress Address
{get;set;}
public Binding Binding
{get;set;}
public contractexample Contract
{get;}
//More members
}
public abstract class ChannelFactory : ...
{
public ServiceEndpoint Endpoint
{get;}
//More members
}
public class ChannelFactory<T> : ChannelFactory,...
{
public ChannelFactory(ServiceEndpoint endpoint);
public ChannelFactory(string configurationName);
public ChannelFactory(Binding binding,EndpointAddress endpointAddress);
public static T CreateChannel(Binding binding,EndpointAddress endpointAddress);
public T CreateChannel( );
//More Members
}

 

we pass the constructor of ChannelFactory<T> with the endpointeither the endpoint name from the client config file, or the binding and address objects, or a ServiceEndpoint object.Use the CreateChannel( ) method to obtain a reference to the proxy and use its methods. At the end close the proxy by either casting it to IDisposable and calling the Dispose( )method or to ICommunicationObject and calling the Close( ) method:

ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>( );
IMyContract proxy1 = factory.CreateChannel( );
using(proxy1 as IDisposable)
{
proxy1.MyMethod( );
}

IMyContract proxy2 = factory.CreateChannel( );
proxy2.MyMethod( );
ICommunicationObject channel = proxy2 as ICommunicationObject;
Debug.Assert(channel != null);
channel.Close( );
The InProcFactory Class

Class InProcFactory, defined as:

public static class InProcFactory
{
public static I CreateInstance<S,I>( ) where I : class
                                          where S : I;
public static void CloseProxy<I>(I instance) where I : class;
   //More members
}

Implementing InProcFactory<T>

public static class InProcFactoryexample
{
struct HostRecordstrustru
{
public HostRecordstrustru(ServiceHost host,string address)
{
Host = host;
Address = address;
}
public readonly ServiceHost Host;
public readonly string Address;
}
static readonly Uri BaseAddress = new Uri("net.pipe://localhost/");
static readonly Binding NamedPipeBinding;
static Dictionary<Type,HostRecordstrustru> m_Hosts = new Dictionary
	<Type,HostRecordstrustru>( );
static InProcFactoryexample( )
{
NetNamedPipeBinding binding = new NetNamedPipeBinding( );
binding.TransactionFlow = true;
NamedPipeBinding = binding;
AppDomain.CurrentDomain.ProcessExit += delegate
{
  foreach(KeyValuePair<Type,HostRecordstrustru> pair in m_Hosts)
{
  pair.Value.Host.Close( );
}
};
}
public static I CreateInstance<S,I>( ) where I : class
where S : I
{
HostRecordstrustru HostRecordstrustru = GetHostRecordstrustru<S,I>( );
return ChannelFactory<I>.CreateChannel(NamedPipeBinding,
new EndpointAddress(HostRecordstrustru.Address));
}
static HostRecordstrustru GetHostRecordstru <S,I>( ) where I : class
 where S : I
{
HostRecordstru HostRecordstru;
if(m_Hosts.ContainsKey(typeof(S)))
{
HostRecordstru = m_Hosts[typeof(S)];
}
else
{
ServiceHost host = new ServiceHost(typeof(S), BaseAddress);
string address = BaseAddress.ToString() + Guid.NewGuid().ToString( );
HostRecordstru = new HostRecordstru(host,address);
m_Hosts.Add(typeof(S),HostRecordstru);
host.AddServiceEndpoint(typeof(I),NamedPipeBinding,address);
host.Open( );
}
return HostRecordstru;
}
public static void CloseProxy<I>(I instance) where I : class
{
ICommunicationObject proxy = instance as ICommunicationObject;
Debug.Assert(proxy != null);
proxy.Close( );
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: