Resolving of metadata of one service is required in many situations when the data has to be explored dynamically. Unfortunately this is often not always a trivial task. Most examples describe briefly how to retrieve the metadata (ServiceDescription) from the running service. However, sometimes it is required to obtain the description from the type. This is the case when the service is possible not running or not reachable. Following example show how to do that.
Imagine there is a contract described with following interface:
[ServiceContract]
public interface ITestInterface
{
[OperationContract]
void operation1();
[OperationContract]
string operation2(int i);
}
The idea behind this example is to obtain the contract description in the way shown below:
ContractDescription desc =
ExportServiceContract(typeof(ITestInterface).AssemblyQualifiedName);
In this case the implementation of the magic function would be:
public static ContractDescription ExportServiceContract(string contract)
{
Type dynamicFactoryType =
typeof(ChannelFactory<>).MakeGenericType(Type.GetType(contract));
ChannelFactory dynamicFactoryInstance =
dynamicFactoryType.InvokeMember(".ctor",
System.Reflection.BindingFlags.CreateInstance,
null, null, null) as ChannelFactory;
return dynamicFactoryInstance.Endpoint.Contract;
}
Posted
Oct 01 2007, 06:15 PM
by
Damir Dobric