0

我的 WCF 客户端遇到了一些问题。我已经配置了 endpointBehaviors,但它没有加载到 Servicehost 中(但绑定已加载......)。

配置:

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

服务包装类:

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

我真的不知道如何继续前进。有没有人知道如何使它工作?

谢谢

4

1 回答 1

2

尝试使用带有端点配置名称的ChannelFactory 构造函数。它将简化您的代码并加载所有行为。行为在 ChannelFactory (Factory.Endpoint.Behaviors) 中,而不是在绑定本身中。

于 2012-10-05T18:39:00.910 回答