开发者

Problems with WCF and Transaction

I have declared field "_accessToken" in my service implementation and initializing it inside a method call GetToken(). GetToken is the first method called by client. When client first time calls GetEmployees method which annotated with transaction, it returns the data. It checks _accesstoken value inside GetEmployees method which is not null in during first call However becomes null in subsequent calls!

What is the problem? Can anyone tell me.

Service Contract:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IEmployeeService
{
    [OperationContract]
    LoginResponse Getoken(LoginRequest request);

    [OperationContract]
    [TransactionFlow(Tra开发者_运维百科nsactionFlowOption.Mandatory)]    
    EmployeeResponse GetEmployees(EmployeeRequest request);
}

Service Implementation

private string _accessToken;

public TokenResponse GetToken(TokenRequest request)
{
    _accessToken = new Guid();
}

[OperationBehavior(TransactionScopeRequired = true,TransactionAutoComplete = true)] 
public EmployeeResponse GetEmployees(EmployeeRequest request)
{
    if (_accessToken != null)
    {
         // Do processing
    }
}


I'm not sure this has anything to do with transactions, rather your services instance management. I suspect you're using percall, in which case all calls get their own service instance. Try using per session, this should resolve your problem.

Here is some reading material: http://msdn.microsoft.com/en-us/magazine/cc163590.aspx

HTH,

Steve


The problem is in ServiceBehaviorAttribute ReleaseServiceInstanceOnTransactionComplete, which is set to true by default. While the attribute is enabled service instance will be destroyed after any transaction.

You can set the attribute to false, however it will make very complex solution and is bad architecture. For example your service have two methods:

public void Method1()
{
 //transaction
 //use data base resources table1;
}
public void Method2()
{
 //transaction
 //use data base resources table1;
}

If the client calls Method1, then calls Method2 there can be in some cases deadlock, or Method2 will be waiting for releasing table1 by Method1.

Better use default settings. Or use PerCall session when session instance is recreated for every call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜