简体   繁体   中英

WCF - Client Behavior ignored

I´m having some trouble with my WCF Client. I have configured the endpointBehaviors but its not loaded into the Servicehost (but the bindings were loaded...).

Config:

<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 Class:

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;
            }
        }
    }
}

I really dont know how to move on. Does anyone have an idea on how to make it working?

thx

Try to use the ChannelFactory constructor with endpoint configuration name. It will simplify your code and load all behaviors. Behaviors are in the ChannelFactory (Factory.Endpoint.Behaviors), not in the binding itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM