Blog Archive

Monday, May 26, 2008

Exposing CLR object as WCF REST


Use .Net framework 3.5, we can load CLR objects through WebService Host Factory as WCF REST Message endpoint without any configuration.
Note that we only need one .svc file specifiying Factory and Service CLR class. After that we can just add any new method on the fly to that CLR class and copy paste to Web Service for new functionality.

<%@ ServiceHost Language="C#" Debug="true"
Service="GMO.PostTrade"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
%>

namespace GMO
{
[ServiceContract]
public interface IPostTrade
{
[OperationContract]
[WebGet()]
List<object> GetInstrumentList(string tradeID);

[OperationContract]
[WebGet()]
bool HasSettled(string TradeID,string InstID);

}

public class PostTrade : IPostTrade
{
public List<object> GetInstrumentList(string tradeID)
{
List<Object> list = new List<object>();
for (int i = 0; i < 10; i++)
list.Add("CRD Instrument" + i.ToString() + " for trade ID=" + tradeID);
return list;
}

public bool HasSettled(string TradeID, string InstID)
{
if (InstID=="1" ) return true;
return false;
}


}
}

To access end points added into the CLR class:

http://localhost:49176/PostTrade.svc/GetInstrumentList?TradeID=78
http://localhost:49176/PostTrade.svc/HasSettled?InstID=10
http://localhost:49176/PostTrade.svc/AnyNewMethodAdded?newID=99

2 comments:

খাবির said...

Hi, Jimmy !!! Please write some LINQ blog.

--
Khabir
Aprosoft,Bangladesh

James said...

Hi, Khabir,

Will try LINQ when got interesting ideas. Currently working on Accessing COM without interOp (C++) to support Emerging market and International Active Traders.

Jimmy