locked
static variable count 0 when call of other project RRS feed

  • Question

  • I have been having a problem for a long time , but so far have not got a solution and I hope you can help me .

    I have a Silverlight application where I use WCF for queries to retrieve information from the database and also for the communication between client ( Duplex ) and Socket (receive and send information between my application and others).

    To control Duplex , when the client accesses a specific module of my application , I link that customer to a static class that I have on my Projeto.Web ( Application start where is my WCF ) , as in the code below :

    //static class and variable (list)
        public static class ncClientes
        {
            private static List<IncServicoDuplex> clientesSupRecAlarme = new List<IncServicoDuplex>();
            public static List<IncServicoDuplex> ClientesSupRecAlarme
            {
                get { return ncClientes.clientesSupRecAlarme; }
                set { ncClientes.clientesSupRecAlarme = value; }
            }
        }

    //Method that include the current client
            public void VinculaCliente(string strProjeto)
            {
                //GetCallbackChannel - obter o canal de comunicação entre o serviço e o cliente - retornará a instância do canal entre o serviço e o cliente.
                IncServicoDuplex cliente = OperationContext.Current.GetCallbackChannel<IncServicoDuplex>();
    
                switch (strProjeto)
                {
                    case "ncAlarme":
                        lock (ncClientes.ClientesSupRecAlarme)
                        {
                            ncClientes.ClientesSupRecAlarme.Add(cliente);
                        }
                        break;
                }
            }

    When one client makes a change to the database, all of my others online customers who are at the module will receive this alteration by the WCF service, using the following code :

    //Method that send the alteration in DataBase, per other user
            public void SupervisaoAlarmeOnline(ncAlarme objAlarme)
            {
                var varClientesAlarme = new List<IncServicoDuplex>(ncClientes.ClientesSupRecAlarme);
    
                foreach (var item in varClientesAlarme)
                {
                    try
                    {
                        item.SupervisaoAlarmeOnlineRetorno(objAlarme);
                    }
                    catch
                    {
                        ncClientes.ClientesSupRecAlarme.Remove(item);
                    }
                }
            }

    //Class that receive the socket message from other application and call the Service to send to all users online
            ncServico svcOnline;
            private void RecebeMensagemSocket(IAsyncResult iar)
            {
                Socket recvSock;
                EndPoint clientEPNEW;
    
                try
                {
                    //Get the received message.
                    recvSock = (Socket)iar.AsyncState;
                    EndPoint clientEP = recvSock.LocalEndPoint;
    
                    int tamMsg = recvSock.EndReceiveFrom(iar, ref clientEP);
    
                    string str = Encoding.Default.GetString(data, 0, tamMsg);
    
                    svcOnline.SupervisaoAcessoOnline(listAcesso[0]);
                }
            }

    My problem happens when I get some information by Socket ( class located in projeto.Web ) that creates an instance of my ServicoWCF and call the method to send the received information to clients. Apparently, my static variable is being reset when called on this side of the application.

    Is there any difference on " which side " I call the service ? When I call it on WCF client side my static variable gets the correct count , but when I call it on the Socket class (that is in the same project that my WCF Services) , count is set to 0.

    I hope you can help me , I tried to be as clear as possible , if there is any doubt please let me know.

    Thanks in advance !


    Atenciosamente, Seratti. Se esta resposta lhe ajudar "marque-a".

    Tuesday, December 10, 2013 4:43 PM

Answers

  • So you've confirmed they're in different web applications. You need to put the socket server and the WCF service in the same web application. Do not use two web projects. You can use two class libraries and reference them in the same web project, though. Alternatively, you can use two processes with a single external entity (such as a file) instead of a static variable. You need to implement some locking mechanism to deal with concurrency issues.
    • Proposed as answer by Yunjie Pei - MSFT Thursday, December 26, 2013 7:08 AM
    • Marked as answer by Yunjie Pei - MSFT Tuesday, December 31, 2013 2:02 AM
    • Unmarked as answer by Seratti Monday, January 13, 2014 10:14 AM
    • Marked as answer by Seratti Monday, January 13, 2014 10:31 AM
    Monday, December 23, 2013 3:21 AM

All replies

  • Hi Seratti,

    Could you tell what version of Windows, .NET Framework you are getting this problem.

    I would like to replicate it.

    Thanks,

    Raghu

    Tuesday, December 10, 2013 4:50 PM
  • Hi RaghuAriga!!!

    First case:

    - Windows 2008 Server 64bits;

    - IIS 7.5; and

    - .Net Framework 4.0.

    Second case:

    - Windows 7 32bits;

    - IIS 7.5; and

    - .Net Framework 4.0.

    Just a important information...

    My WCF is publish in a different local of my current application.

    E.g: Application (\\COMPUTER02\SOLUTION\APP) and my WCF Service at (\\COMPUTER02\SOLUTION\SERVICE)

    EDIT: This just occurs when I use my WCF Service in a different folder. But I need use so because I will have more than one folder APP (APP1, APP2 and APP3) using the same WCF Service.

    This is my serviceClient references:

    <configuration>
        <system.serviceModel>
            <bindings>
                <customBinding>
                    <binding name="CustomBinding_IncServico">
                        <binaryMessageEncoding />
                        <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                    </binding>
                </customBinding>
            </bindings>
            <client>
                <endpoint address="net.tcp://localhost:4502/SOLUTION/SERVICE/ncServico.svc"
                    binding="customBinding" bindingConfiguration="CustomBinding_IncServico"
                    contract="ncServico.IncServico" name="CustomBinding_IncServico" />
            </client>
        </system.serviceModel>
    </configuration>

    Thank you a lot!!


    Atenciosamente, Seratti. Se esta resposta lhe ajudar "marque-a".


    • Edited by Seratti Tuesday, December 10, 2013 5:06 PM "EDIT:"
    Tuesday, December 10, 2013 5:04 PM
  • Anybody else could help me?

    Atenciosamente, Seratti. Se esta resposta lhe ajudar "marque-a".

    Thursday, December 12, 2013 4:08 PM
  • Is the socket server and WCF server running inside the same web application? If so, there's no reason why the socket server sees a different value for a static variable. If they're in different web applications, then they will have different values.
    Saturday, December 14, 2013 2:59 PM
  • Thank ou Name-Dis for answers!

    I used a method that save a txt file at my C:/Temp/ to now where is my service. I use this code to take the local of my svc that I'm using.

    AppDomain.CurrentDomain.RelativeSearchPath.ToString();

    When I'm running using VS2010 I received this situation:

    GetSite("all"); //C:\inetpub\wwwroot\Services\system\bin
    VinculaCliente("access"); //C:\inetpub\wwwroot\Services\system\bin
    RecebeMensagemSocket(); //C:\Projects\Project_2013\MyProjectSolution.Web\bin
    SupervisaoOnlineRetorno(); //C:\Projects\Project_2013\MyProjectSolution.Web\bin

    When I'm running public all (application and service) in IIS:

    GetSite("all"); //C:\inetpub\wwwroot\Services\system\bin
    VinculaCliente("access"); //C:\inetpub\wwwroot\Services\system\bin
    RecebeMensagemSocket(); //C:\inetpub\wwwroot\Application\bin
    SupervisaoOnlineRetorno(); //C:\inetpub\wwwroot\Application\bin

    With this test We can have a conclusion!!  My application when receive a Socket are using a different service (service that are published where is my application) and my searchs and method that I'm included my clients are using the correct service (/system/). But how can I solve this? How can I do a publish that I will use for all situation my service that are on my "system" folder?

    Thanks for all help!!


    Atenciosamente, Seratti.


    • Edited by Seratti Tuesday, December 17, 2013 10:11 AM nothing
    Monday, December 16, 2013 11:03 AM
  • So you've confirmed they're in different web applications. You need to put the socket server and the WCF service in the same web application. Do not use two web projects. You can use two class libraries and reference them in the same web project, though. Alternatively, you can use two processes with a single external entity (such as a file) instead of a static variable. You need to implement some locking mechanism to deal with concurrency issues.
    • Proposed as answer by Yunjie Pei - MSFT Thursday, December 26, 2013 7:08 AM
    • Marked as answer by Yunjie Pei - MSFT Tuesday, December 31, 2013 2:02 AM
    • Unmarked as answer by Seratti Monday, January 13, 2014 10:14 AM
    • Marked as answer by Seratti Monday, January 13, 2014 10:31 AM
    Monday, December 23, 2013 3:21 AM