1

我目前遇到工作流服务的一些问题。如果我以较短的顺序开始 4、5,它们可以正常工作,但是如果我增加这个值(从 ~10 开始),那么我会得到以下异常:

此通道不能再用于发送消息,因为输出会话由于服务器启动的关闭而自动关闭。通过将 DispatchRuntime.AutomaticInputSessionShutdown 设置为 false 来禁用自动关闭,或者考虑使用远程服务器修改关闭协议。

我认为问题在于我创建代理的方式。我使用以下代码提供代理,尝试重用现有的:

public abstract class ProxyProvider<TService>
    where TService : class
{
    /// <summary>
    /// Static reference to the current time provider.
    /// </summary>
    private static ProxyProvider<TService> current = DefaultProxyProvider.Instance;

    private TService service;

    /// <summary>
    /// Gets or sets the current time provider.
    /// </summary>
    /// <value>
    /// The current time provider.
    /// </value>
    public static ProxyProvider<TService> Current
    {
        get
        {
            return ProxyProvider<TService>.current;
        }

        set
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            ProxyProvider<TService>.current = value;
        }
    }

    /// <summary>
    /// Resets to default.
    /// </summary>
    public static void ResetToDefault()
    {
        ProxyProvider<TService>.current = DefaultProxyProvider.Instance;
    }

    /// <summary>
    /// Loads the proxy.
    /// </summary>
    /// <param name="forceNew">if set to <c>true</c> [force new].</param>
    /// <returns>The instance of the proxy.</returns>
    public virtual TService Provide(bool forceNew = false)
    {
        if (forceNew || !this.IsInstanceValid())
        {
            this.service = this.CreateInstance();
            return this.service;
        }

        return this.service;
    }

    /// <summary>
    /// Internals the load.
    /// </summary>
    /// <returns>The new created service.</returns>
    protected abstract TService CreateInstance();

    private bool IsInstanceValid()
    {
        var instance = this.service as ICommunicationObject;
        if (instance == null)
        {
            return false;
        }

        return instance.State != CommunicationState.Faulted && instance.State != CommunicationState.Closed && instance.State != CommunicationState.Closing;
    }

    /// <summary>
    /// Defines the default <see cref="ProxyProvider&lt;TService&gt;"/> which uses the System DateTime.UtcNow value.
    /// </summary>
    private sealed class DefaultProxyProvider : ProxyProvider<TService>
    {
        /// <summary>
        /// Reference to the instance of the <see cref="ProxyProvider&lt;TService&gt;"/>.
        /// </summary>
        private static ProxyProvider<TService> instance;

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static ProxyProvider<TService> Instance
        {
            get
            {
                if (DefaultProxyProvider.instance == null)
                {
                    DefaultProxyProvider.instance = new DefaultProxyProvider();
                }

                return DefaultProxyProvider.instance;
            }
        }

        /// <summary>
        /// Loads the specified force new.
        /// </summary>
        /// <returns>A non-disposed instance of the given service.</returns>
        protected override TService CreateInstance()
        {
            var loadedService = Activator.CreateInstance<TService>();
            return loadedService;
        }
    }

使用额外的“惰性”提供程序:

public class CustomConstructorProxyProvider<TService> : ProxyProvider<TService>
    where TService : class
{
    private readonly Func<TService> constructor;

    /// <summary>
    /// Initializes a new instance of the <see cref="CustomConstructorProxyProvider&lt;TService&gt;"/> class.
    /// </summary>
    /// <param name="constructor">The constructor.</param>
    public CustomConstructorProxyProvider(Func<TService> constructor)
    {
        this.constructor = constructor;
    }

    /// <summary>
    /// Internals the load.
    /// </summary>
    /// <returns>The new created service.</returns>
    protected override TService CreateInstance()
    {
        var service = this.constructor();
        return service;
    }
}

这样使用:

var proxy = ProxyProvider<IWorkflowService>.Current.Provide();
proxy.DoSomething();

像这样初始化:

ProxyProvider<IWorkflowService>.Current = new CustomConstructorProxyProvider<IWorkflowService>(() => new WorkflowServiceProxy("endpoint"));

工作流服务由 IIS 托管,我添加了以下限制设置:

<serviceThrottling
                maxConcurrentCalls="512"
                maxConcurrentInstances="2147483647"
                maxConcurrentSessions="1024"/>

这应该足以满足我的需要。

我希望有人可以帮助我配置客户端和服务器以实现所需的可伸缩性(数百个按顺序启动并并行运行,使用 WorkflowInstance sql 存储)。

更新:我对所有服务都使用 NetTcpBinding。

更新 2:所有服务现在都在本地托管和使用。

谢谢弗朗西斯科

4

0 回答 0