首页 新闻 会员 周边

WCF中实例化 ActiveX 控件报错,急求解决办法

0
悬赏园豆:100 [已解决问题] 解决于 2013-03-20 16:54

在wcf中实例化一个Active控件的时候报错,说当前线程不在单线程单元中,现在附上截图

我想可能是因为wcf这些服务都是在一个线程池中实例化和使用,有没有什么办法能够在线程中实例化ActiveX控件或者其他的解决办法???

linda-1989的主页 linda-1989 | 初学一级 | 园豆:112
提问于:2012-12-09 15:50
< >
分享
最佳答案
0
View Code
[ServiceContract]
    public interface IMTSServer
    {
        [OperationContract]
        string GetData(string value);
    }
     
    public class STAOperationInvoker : IOperationInvoker
    {
        IOperationInvoker _innerInvoker;
        public STAOperationInvoker(IOperationInvoker invoker)
        {
            _innerInvoker = invoker;
        }

        public object[] AllocateInputs()
        {
            return _innerInvoker.AllocateInputs();
        }

        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            // Create a new, STA thread
            object[] staOutputs = null;
            object retval = null;
            Thread thread = new Thread(
                delegate()
                {
                    retval = _innerInvoker.Invoke(instance, inputs, out staOutputs);
                });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            outputs = staOutputs;
            return retval;
        }

        public IAsyncResult InvokeBegin(object instance, object[] inputs,
          AsyncCallback callback, object state)
        {
            // We don’t handle async…
            throw new NotImplementedException();
        }

        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            // We don’t handle async…
            throw new NotImplementedException();
        }

        public bool IsSynchronous
        {
            get { return true; }
        }
    }

    public class STAOperationBehaviorAttribute : Attribute, IOperationBehavior
    {
        public void AddBindingParameters(OperationDescription operationDescription,
          System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription,
          System.ServiceModel.Dispatcher.ClientOperation clientOperation)
        {
            // If this is applied on the client, well, it just doesn’t make sense.
            // Don’t throw in case this attribute was applied on the contract
            // instead of the implementation.
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription,
          System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
        {
            // Change the IOperationInvoker for this operation.
            dispatchOperation.Invoker = new STAOperationInvoker(dispatchOperation.Invoker);
        }

        public void Validate(OperationDescription operationDescription)
        {
            if (operationDescription.SyncMethod == null)
            {
                throw new InvalidOperationException("The STAOperationBehaviorAttribute " +
                    "only works for synchronous method invocations.");
            }
        }
    }
  [STAOperationBehavior]
        public string GetData(string value)

拿分,走人……

收获园豆:80
迹I柳燕 | 菜鸟二级 |园豆:366 | 2012-12-14 13:25
其他回答(1)
0

楼主看看这里,原理是一样的http://www.cnblogs.com/winzheng/archive/2009/05/11/1454328.html

收获园豆:10
az235 | 园豆:8483 (大侠五级) | 2012-12-10 08:39

还是不行啊,有没有其他办法啊?

支持(0) 反对(0) linda-1989 | 园豆:112 (初学一级) | 2012-12-10 22:59

@linda-1989: 报什么错误?

支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-12-11 09:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册