WCF可靠性问题

时间:2011-04-01 19:13:03

标签: wcf wcf-binding wcf-rest webhttpbinding

尝试测试我的wcf服务的可靠性。我在客户端的循环中调用wcf服务。 wcf rest服务(webhttpbinding)进行一些数据处理和插入 记录到数据库中。整个操作在一个事务中完成。

我发现,如果我将InstanceContextMode设置为PerCall,那么大约60条消息(从循环内部调用服务的60倍)中只有40条正在进入数据库。没有错误没有例外。这些消息正在被删除。

如果我将InstanceContextMode设置为Single,那么我会看到所有消息都到达db。 InstanceContextMode.Percall是否会损失预期的行为?另外,我没有并发集。任何澄清都会非常有帮助。添加了代码。使用MySQL作为数据库...

编辑我的不好 - 我刚注意到我在服务器端遇到异常 - {“尝试锁定时发现死锁;尝试重新启动交易”}

这是因为在事务正在进行时,在相同记录上调用了另一个事务。如果失败一次,则通过重新启动事务来修复它。

服务

    [WebInvoke(UriTemplate = "employee", Method = "POST")]
    public long AddNewEmployee(EmployeeEntity newEmployee)
    {
        EmployeeRepository eRep = new EmployeeRepository();
        return eRep.AddNewEmployee(newEventEntity);                     
    }

存储库类构造函数初始化对象上下文

    public EmployeeRepository()
    {
        bd = new EmployeeEntity();
    }

代码 - 服务电话

    //bd is the object context 
    //there are two tables 
    internal long AddNewEmployee(EmployeeEntity newEmployee)
    {
        bool tSuccess = false;
        using (TransactionScope transaction = new TransactionScope())
        {
            try
            {
                //get existing employees
                var existingEmployees = from employee
                                        in bd.employees select employee;


                List<employee> returnedEmployees = new List<employee>();                    

                //Do some processing 
                returnedEmployees = DoSomeprocessing(existingEmployees);

                //Insert returned employees as updates
                foreach (employee e in returnedEmployees)
                {
                    bd.employees.AddObject(e);
                }

                bd.SaveChanges();
                returnedEmployees.Clear();

                //add to second table 
                bd.otherdetails.AddObject(newEmployee);
                bd.SaveChanges();


                //Transaction Complete                        
                transaction.Complete();
                tSuccess = true;

            }
            catch (Exception e)
            {
                //return something meaningful 
                return -1;                    
            }
        }

        if (tSuccess)
        {
            //End Transaction
            bd.AcceptAllChanges();
            return 200;
        }            
        else
        {
           return -1;
        }

    }

客户端只是在循环中调用服务

1 个答案:

答案 0 :(得分:0)

我强烈建议为任何WCF添加全局异常处理。它帮助我节省了许多小时的调试,并将捕获任何未处理的异常。它比ASP.NET中的global.ascx更具参与性。

第1步 - 实施IErroHander和IServiceBehavior

注意HandleError内部我正在使用Enterprise Library来处理异常。您也可以在此处使用自定义实现。

 public class ErrorHandler : IErrorHandler, IServiceBehavior
        {        
            public bool HandleError(Exception error)
            {
                // Returning true indicates you performed your behavior.
                return true;
            }


            public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
            {            
                // Log Exception

                ExceptionPolicy.HandleException(error, "ExceptionPolicy");

                // Shield the unknown exception
                FaultException faultException = new FaultException(
                    "Server error encountered. All details have been logged.");
                MessageFault messageFault = faultException.CreateMessageFault();

                fault = Message.CreateMessage(version, messageFault, faultException.Action);
            }

            private IErrorHandler errorHandler = null;

            public ErrorHandler()
            {

            }

            public ErrorHandler(IErrorHandler errorHandler)
            {
                this.errorHandler = errorHandler;
            }

            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
                {
                    ChannelDispatcher cd = cdb as ChannelDispatcher;

                    if (cd != null)
                    {
                        cd.ErrorHandlers.Add(new ErrorHandler());
                    }
                }
            }

            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
                foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
                {
                    ChannelDispatcher cd = cdb as ChannelDispatcher;

                    if (cd != null)
                    {
                        cd.ErrorHandlers.Add(new ErrorHandler());
                    }
                }
            }

            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {

            }
        }

    }

第2步 - 创建错误元素

public class ErrorHandlerElement : BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            return new ErrorHandler();
        }

        public override Type BehaviorType
        {
            get
            {
                return typeof(ErrorHandler);
            }
        }
    }

第3步 - 将元素添加到web.config

<serviceBehaviors>
        <behavior>                              
          <ErrorHandler />
        </behavior>
      </serviceBehaviors>