2

对于 WCF 客户端,我有一个IServiceProxyFactory设置凭据的界面。

public interface IServiceProxyFactory<T>
{
    T GetServiceProxy();
}

public class ServiceProxy1 : IServiceProxyFactory<ServiceClient1>
{
    public ServiceClient1 GetServiceProxy()
    {
        var client = new ServiceClient1();
        // set credentials here
        return client;
    }
}

public class ServiceProxy2 : IServiceProxyFactory<ServiceClient2> { 
    // ... 
} 

从问题WCF 客户端“使用”块问题的最佳解决方法是什么?,我创建了一个助手,如下所示:

public static class Service<TProxy, TClient>
    where TProxy : IServiceProxyFactory<TClient>, new()
    where TClient : ICommunicationObject
{
    public static IServiceProxyFactory<TClient> proxy = new TProxy();

    public static void Use(Action<TClient> codeBlock)
    {
        TClient client = default(TClient);
        bool success = false;
        try
        {
            client = proxy.GetServiceProxy();
            codeBlock(client);
            ((ICommunicationObject)client).Close();
            success = true;
        }
        finally
        {
            if (!success)
            {
                ((ICommunicationObject)client).Abort();
            }
        }
    }
}

我将助手用作:

Service<ServiceProxy1, ServiceClient1>.Use(svc => svc.Method()); 

问题:

  1. 有没有一种方法可以让我摆脱TClientor TProxy(更新的)类型,以便我可以使用:

    Service<ServiceProxy1>.Use(svc => svc.Method()); 
    

    (更新)

    Service<ServiceClient1>.Use(svc => svc.Method()); 
    
  2. 有没有比使用ICommunicationObjectfor Close()and更好的方法Abort()

4

2 回答 2

3
  • 代码

    partial class TestClass {
        public static void TestMethod() {
            Service<ServiceProxy1>.Use(svc => svc.Method());
            Service<ServiceProxy1>.Use(svc => svc.Method());
            Service<ServiceProxy1>.Use(svc => svc.Method());
            Service<ServiceProxy2>.Use(svc => svc.Method());
        }
    }
    
    public partial interface IServiceProxyFactory<T> {
        T GetServiceProxy();
    }
    
    public partial class Service<T> where T: ServiceProxy, new() {
        public static void Use(Action<T> codeBlock) {
            using(var client=ServiceProxy.GetServiceProxy<T>().GetServiceProxy() as T)
                try {
                    codeBlock(client);
                }
                catch {
                    throw;
                }
        }
    }
    
    public abstract partial class ServiceProxy: CommunicationObject, IDisposable {
        public static T GetServiceProxy<T>() where T: ServiceProxy, new() {
            var proxy=m_List.FirstOrDefault(x => typeof(T).Equals(x.GetType())) as T;
    
            if(null==proxy) {
                proxy=new T();
                m_List.Add(proxy);
            }
    
            return proxy;
        }
    
        public abstract ServiceProxy GetServiceProxy();
        public abstract void Method();
    
        protected virtual void Dispose(bool disposing) {
            lock(ThisLock)
                if(!this.IsDisposed&&disposing) {
                    this.Close();
    
                    if(!this.IsDisposed)
                        this.Abort();
                }
        }
    
        public void Dispose() {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        ~ServiceProxy() {
            this.Dispose(false);
        }
    
        static List<ServiceProxy> m_List=new List<ServiceProxy>();
    }
    
    public partial class ServiceProxy1: ServiceProxy {
        protected override IAsyncResult OnBeginClose(
            TimeSpan timeout, AsyncCallback callback, object state
            ) {
            throw new NotImplementedException();
        }
    
        protected override IAsyncResult OnBeginOpen(
            TimeSpan timeout, AsyncCallback callback, object state
            ) {
            throw new NotImplementedException();
        }
    
        protected override void OnAbort() {
        }
    
        protected override void OnEndClose(IAsyncResult result) {
        }
    
        protected override void OnEndOpen(IAsyncResult result) {
        }
    
        protected override void OnClose(TimeSpan timeout) {
        }
    
        protected override void OnOpen(TimeSpan timeout) {
        }
    
        protected override TimeSpan DefaultCloseTimeout {
            get {
                return TimeSpan.Zero;
            }
        }
    
        protected override TimeSpan DefaultOpenTimeout {
            get {
                return TimeSpan.Zero;
            }
        }
    
        public override ServiceProxy GetServiceProxy() {
            var client=new ServiceProxy1();
            // set credentials here
            return client;
        }
    
        public override void Method() {
        }
    }
    
    public partial class ServiceProxy2: ServiceProxy1 {
        public override ServiceProxy GetServiceProxy() {
            var client=new ServiceProxy2();
            // set credentials here
            return client;
        }
    }
    

有一点要提:

  1. lock可以安全重新进入

  2. 我不可能对 from到 any 的类型(例如 from to )进行完全相同的反向推理声明。TGeneric<T>ServiseClientServiceProxy<ServiceClient>

  3. 根据 2,ServiceProxyServiceClient在代码中只是一样的东西,所以没有ServiceClient.

  4. ServiceProxy本身就是抽象的。为方便起见,ServiceClient需要转换为ICommunicationObject3 plus 的要求,ServiceProxy源自CommunicationObject; 然后对于有没有比……东西更好的方法,它实现了IDisposible

  5. 对于继承的具体类的静态实例ServiceProxy,每个实例只有一个实例,并存储在 中m_List,并调用GetServiceProxy<T>()just get 它们的静态泛型版本。这看起来更像是享元模式。

  6. 根据5,界面IServiceProxyFactory<T>完全没用,只是为了好看和开心就放在那里。

  7. 的实例版本GetServiceProxy()保持原始用法,但具体类需要覆盖它。

  8. 在该Use方法中,创建的客户端与using语句一起使用。我已经阅读了Using Statement 的避免问题,但似乎我不知道您想要处理异常的策略,因此我只是尝试重新抛出

  9. 根据1,我认为是一种线程安全的方式,通过锁来原子地处理,继承IsDisposedThisLock使用。并相应CloseAbort在那里做。

  10. 哦十!类ServiceProxy2ServiceProxy1仅用于示例,并ServiceProxy2派生自ServiceProxy1.

代码看起来很冗长,但实际上设计非常简单。只需报告我的问题,我会尽力纠正它。希望帮助和一个好

于 2013-03-24T09:50:15.177 回答
1

我通过使用技巧将ServiceProxy1类合并到其中来实现它。ServiceClient1ServiceClient1 : IServiceProxyFactory<ServiceClient1>

public interface IServiceProxyFactory<T>
{
    // can have a better name like SetCredentials()
    T GetServiceProxy();
}

// Got rid of ServiceProxy1 class
public partial class ServiceClient1 : IServiceProxyFactory<ServiceClient1>
{
    public ServiceClient1 GetServiceProxy()
    {
        var client = this;
        // set credentials here
        //client.ClientCredentials = "";
        return client;
    }
}

public partial class ServiceClient2 : IServiceProxyFactory<ServiceClient2> { ... } 

public static class ServiceMod<TClient>
    where TClient : class, ICommunicationObject, IServiceProxyFactory<TClient>, new()
{
    public static TReturn Use<TReturn>(Func<TClient, TReturn> codeBlock)
    {
        TClient client = default(TClient);
        bool success = false;
        try
        {
            client = new TClient().GetServiceProxy();
            TReturn result = codeBlock(client);
            client.Close();
            success = true;
            return result;
        }
        finally
        {
            if (!success)
            {
                client.Abort();
            }
        }
    }
}

现在我可以这样做:

Service<ServiceClient1>.Use(svc => svc.Method());
于 2013-03-24T20:54:11.163 回答