开发者

Encapsulating WCF Proxies in Static Classes

I am designing a web service which will call different external web services according to the properties of a given object (a "request", for instance). A reference to these web services is added through the "Add Web Reference" menu in Visual Studio 2008, which, as you know, creates a proxy class for each endpoint which inherits from System.ServiceModel.ChannelBase<ISomeInterface> (where ISomeInterface is the endpoint defined by the specific service's WSDL).

The question is that I would like to encapsulate all those proxies in a single ServiceManager (for instance) static class containing, for example, an internal list of all the proxies, so that, on the one hand, all calls to a given service may go through ServiceManager instead of being scattered around the main application, and, on the other hand, new services which may be added latter can be made known to ServiceManager by a simple addition of a reference to the new proxy class.

I thought abo开发者_如何学Cut desinging ServiceManager like

public static class ServiceManager
{
    #region Properties

    public static Dictionary<string, TProxy> ServiceList { get; private set; }

    #endregion
}

but I don't know what I should replace TProxy by so that all of the different proxies can be called by using ServiceManager.ServiceList["ServiceName"]. Can anyone please help me out with this?


Since each service implements a different interface, it would have to be object... Unless you can create a common base interface, make the proxies inherit from that interface, and then create a List<MyBaseInterface>.

Why can't you just have one property on your class per proxy? At least then you could access the proxies in a strongly-typed way.


Do not reuse proxies. Re-instantiate them. Magic strings to differentiate between proxies are equally bad.

You are better off using static factories that return your service proxies than you are keeping only one instance of them.

Something like this:

public static class ServiceFactory
{
    public static Proxy CreateProxy();
    public static Proxy2 CreateProxy2();
    // etc.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜