2012-10-05 54 views
0

我的WCF客戶端遇到了一些問題。 我已配置endpointBehaviors,但未將其加載到Servicehost中(但綁定已加載...)。WCF - 忽略客戶行爲

配置:

<system.serviceModel> 
     <behaviors> 
     <endpointBehaviors> 
      <behavior name="DefaultBehavior"> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
      </behavior> 
     </endpointBehaviors> 
     </behaviors> 

     <bindings> 
     <netTcpBinding> 
      <binding name="DefaultBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
          maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
          maxNameTableCharCount="2147483647" /> 
      </binding> 
     </netTcpBinding> 
     </bindings> 

     <client> 
      <endpoint address="..." behaviorConfiguration="DefaultBehavior" binding="netTcpBinding" bindingConfiguration="DefaultBinding" contract="..." name="..."/> 
      <endpoint address="..." behaviorConfiguration="DefaultBehavior" binding="netTcpBinding" bindingConfiguration="DefaultBinding" contract="..." name="..."/> 
     </client> 
    </system.serviceModel> 

ServiceWrapper類:

public class ServiceWrapper<T> : IDisposable where T : class 
{ 
    private ChannelFactory<T> factory; 
    private T channel; 

    private readonly NetTcpBinding binding; 
    private readonly EndpointAddress endpoint; 

    private readonly object lockObject = new object(); 
    private bool disposed; 

    public ServiceWrapper(string configName) 
    { 
     ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; 
     ChannelEndpointElementCollection endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection; 

     foreach (ChannelEndpointElement endpointElement in endpointCollection) 
     { 
      if (endpointElement.Name != configName) 
       continue; 

      binding = new NetTcpBinding(endpointElement.BindingConfiguration); 
      endpoint = new EndpointAddress(endpointElement.Address); 

      disposed = false; 
     } 

     if (endpoint == null) 
      throw new ConfigurationErrorsException(configName + " is not present in the config file"); 
    } 

    public T Channel 
    { 
     get 
     { 
      if (disposed) 
       throw new ObjectDisposedException("Resource ServiceWrapper<" + typeof(T) + "> has been disposed"); 

      lock (lockObject) 
      { 
       if (factory == null) 
       { 
        factory = new ChannelFactory<T>(binding, endpoint); 
        channel = factory.CreateChannel(); 
       } 
      } 
      return channel; 
     } 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 


    private void Dispose(bool disposing) 
    { 
     if (!disposed) 
     { 
      if (disposing) 
      { 
       lock (lockObject) 
       { 
        if (channel != null) 
         ((IClientChannel)channel).Close(); 

        if (factory != null) 
         factory.Close(); 
       } 

       channel = null; 
       factory = null; 
       disposed = true; 
      } 
     } 
    } 
} 

我真的不知道如何繼續。有沒有人有關於如何使其工作的想法?

THX

回答

2

嘗試與端點配置名稱使用ChannelFactory constructor。它將簡化您的代碼並加載所有行爲。行爲在ChannelFactory(Factory.Endpoint.Behaviors)中,而不在綁定本身中。

+0

作品完美!多謝。 – Daffi