Should Semaphoreslim be in the controller instead of further down in the chain?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















We have a deadlock like behaviour in our production-environment and I wonder if we use semaphoreslim correctly.



In our restapi the code looks like this:



public async Task<IActionResult> CreateAsync([FromBody] SomeModel someModel)
{
var result = await _someClass.CreateArende(someModel);

return result;
}

public async Task<IActionResult> RegisterAsync([FromBody] SomeModel someModel)
{
var result = await _someClass.RegisterArende(someModel);

return result;
}


No semphoreslim in controller level of our API but in someClass looks like this:



public class SomeClass {

protected static SemaphoreSlim _semphoreSlimCreateArende = new SemaphoreSlim(1, 1);

public async virtual Task<SomeResponseDto> CreateArende(SomeModel someModel)
{
try
{
await _semphoreSlimCreateArende.WaitAsync();

}
finally
{
try
{
_semphoreSlimCreateArende.Release();
}
catch (Exception)
{
}
}

return new SomeResponseDto()
{
...
};
}

public async virtual Task<SomeResponseDto> RegisterArende(SomeModel someModel)
{
try
{
await _semphoreSlimCreateArende.WaitAsync();

}
finally
{
try
{
_semphoreSlimCreateArende.Release();
}
catch (Exception)
{
}
}

return new SomeResponseDto()
{
...
};
}

}


Should the semaphoreslim be in the controller level instead? Or should i change the controllers actions to not be async?










share|improve this question































    0















    We have a deadlock like behaviour in our production-environment and I wonder if we use semaphoreslim correctly.



    In our restapi the code looks like this:



    public async Task<IActionResult> CreateAsync([FromBody] SomeModel someModel)
    {
    var result = await _someClass.CreateArende(someModel);

    return result;
    }

    public async Task<IActionResult> RegisterAsync([FromBody] SomeModel someModel)
    {
    var result = await _someClass.RegisterArende(someModel);

    return result;
    }


    No semphoreslim in controller level of our API but in someClass looks like this:



    public class SomeClass {

    protected static SemaphoreSlim _semphoreSlimCreateArende = new SemaphoreSlim(1, 1);

    public async virtual Task<SomeResponseDto> CreateArende(SomeModel someModel)
    {
    try
    {
    await _semphoreSlimCreateArende.WaitAsync();

    }
    finally
    {
    try
    {
    _semphoreSlimCreateArende.Release();
    }
    catch (Exception)
    {
    }
    }

    return new SomeResponseDto()
    {
    ...
    };
    }

    public async virtual Task<SomeResponseDto> RegisterArende(SomeModel someModel)
    {
    try
    {
    await _semphoreSlimCreateArende.WaitAsync();

    }
    finally
    {
    try
    {
    _semphoreSlimCreateArende.Release();
    }
    catch (Exception)
    {
    }
    }

    return new SomeResponseDto()
    {
    ...
    };
    }

    }


    Should the semaphoreslim be in the controller level instead? Or should i change the controllers actions to not be async?










    share|improve this question



























      0












      0








      0








      We have a deadlock like behaviour in our production-environment and I wonder if we use semaphoreslim correctly.



      In our restapi the code looks like this:



      public async Task<IActionResult> CreateAsync([FromBody] SomeModel someModel)
      {
      var result = await _someClass.CreateArende(someModel);

      return result;
      }

      public async Task<IActionResult> RegisterAsync([FromBody] SomeModel someModel)
      {
      var result = await _someClass.RegisterArende(someModel);

      return result;
      }


      No semphoreslim in controller level of our API but in someClass looks like this:



      public class SomeClass {

      protected static SemaphoreSlim _semphoreSlimCreateArende = new SemaphoreSlim(1, 1);

      public async virtual Task<SomeResponseDto> CreateArende(SomeModel someModel)
      {
      try
      {
      await _semphoreSlimCreateArende.WaitAsync();

      }
      finally
      {
      try
      {
      _semphoreSlimCreateArende.Release();
      }
      catch (Exception)
      {
      }
      }

      return new SomeResponseDto()
      {
      ...
      };
      }

      public async virtual Task<SomeResponseDto> RegisterArende(SomeModel someModel)
      {
      try
      {
      await _semphoreSlimCreateArende.WaitAsync();

      }
      finally
      {
      try
      {
      _semphoreSlimCreateArende.Release();
      }
      catch (Exception)
      {
      }
      }

      return new SomeResponseDto()
      {
      ...
      };
      }

      }


      Should the semaphoreslim be in the controller level instead? Or should i change the controllers actions to not be async?










      share|improve this question
















      We have a deadlock like behaviour in our production-environment and I wonder if we use semaphoreslim correctly.



      In our restapi the code looks like this:



      public async Task<IActionResult> CreateAsync([FromBody] SomeModel someModel)
      {
      var result = await _someClass.CreateArende(someModel);

      return result;
      }

      public async Task<IActionResult> RegisterAsync([FromBody] SomeModel someModel)
      {
      var result = await _someClass.RegisterArende(someModel);

      return result;
      }


      No semphoreslim in controller level of our API but in someClass looks like this:



      public class SomeClass {

      protected static SemaphoreSlim _semphoreSlimCreateArende = new SemaphoreSlim(1, 1);

      public async virtual Task<SomeResponseDto> CreateArende(SomeModel someModel)
      {
      try
      {
      await _semphoreSlimCreateArende.WaitAsync();

      }
      finally
      {
      try
      {
      _semphoreSlimCreateArende.Release();
      }
      catch (Exception)
      {
      }
      }

      return new SomeResponseDto()
      {
      ...
      };
      }

      public async virtual Task<SomeResponseDto> RegisterArende(SomeModel someModel)
      {
      try
      {
      await _semphoreSlimCreateArende.WaitAsync();

      }
      finally
      {
      try
      {
      _semphoreSlimCreateArende.Release();
      }
      catch (Exception)
      {
      }
      }

      return new SomeResponseDto()
      {
      ...
      };
      }

      }


      Should the semaphoreslim be in the controller level instead? Or should i change the controllers actions to not be async?







      c# semaphore asp.net-core-webapi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 11:01









      marc_s

      585k13011261272




      585k13011261272










      asked Nov 24 '18 at 7:17









      NetProvokeNetProvoke

      582515




      582515
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This question is a little disjointed, however lets try to make sense of it a little



          Firstly lets get the pattern right



          try
          {
          await _semphoreSlimCreateArende.WaitAsync();

          // do your sync work here

          }
          finally
          {
          // if this throws you are certainly doing something wrong
          _semphoreSlimCreateArende.Release();
          }


          Secondly, you should have your keyboard taken away from you for this



          catch (Exception)
          {
          }


          Don't ever blindly eat exceptions, and if you are getting them on _semphoreSlimCreateArende.Release you have serious problems already and you have to work out why




          Should the semaphoreslim be in the controller level instead?




          Use them at the level that makes the most sense, i mean if you need to sync a piece of code sync it there, not 13 levels up.




          Or should i change the controllers actions to not be async?




          if you have asnyc work make your controller async and let it propagate down the stack to your async code




          We have a deadlock like behaviour in our production-environment and i
          wonder if we use semaphoreslim correctly.




          Woah, are we talking DataBase Deadlocks, or Context DeadLocks. Either way this all sounds a little fishy



          FYI



          Using a SemaphoreSlim in a web based service can be a nasty approach. SemaphoreSlim works on a spin lock, and shouldn't be used for anything that blocks for any period of time. Spinlocks chew through the processor waiting for the lock to be released. Ideal you use them when there is a little of the lock biting, and in situations that don't block further down the chain.



          If you are deadlocking code, then you need to work out exactly what is causing the deadlock, if its a task based deadlock, then SemaphoreSlim is not the right tool for the job, if it is a non blocking race condition then maybe its a little more acceptable. If its a Database deadlock, then you have bigger problem.






          share|improve this answer
























          • ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

            – NetProvoke
            Nov 25 '18 at 11:44












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456051%2fshould-semaphoreslim-be-in-the-controller-instead-of-further-down-in-the-chain%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This question is a little disjointed, however lets try to make sense of it a little



          Firstly lets get the pattern right



          try
          {
          await _semphoreSlimCreateArende.WaitAsync();

          // do your sync work here

          }
          finally
          {
          // if this throws you are certainly doing something wrong
          _semphoreSlimCreateArende.Release();
          }


          Secondly, you should have your keyboard taken away from you for this



          catch (Exception)
          {
          }


          Don't ever blindly eat exceptions, and if you are getting them on _semphoreSlimCreateArende.Release you have serious problems already and you have to work out why




          Should the semaphoreslim be in the controller level instead?




          Use them at the level that makes the most sense, i mean if you need to sync a piece of code sync it there, not 13 levels up.




          Or should i change the controllers actions to not be async?




          if you have asnyc work make your controller async and let it propagate down the stack to your async code




          We have a deadlock like behaviour in our production-environment and i
          wonder if we use semaphoreslim correctly.




          Woah, are we talking DataBase Deadlocks, or Context DeadLocks. Either way this all sounds a little fishy



          FYI



          Using a SemaphoreSlim in a web based service can be a nasty approach. SemaphoreSlim works on a spin lock, and shouldn't be used for anything that blocks for any period of time. Spinlocks chew through the processor waiting for the lock to be released. Ideal you use them when there is a little of the lock biting, and in situations that don't block further down the chain.



          If you are deadlocking code, then you need to work out exactly what is causing the deadlock, if its a task based deadlock, then SemaphoreSlim is not the right tool for the job, if it is a non blocking race condition then maybe its a little more acceptable. If its a Database deadlock, then you have bigger problem.






          share|improve this answer
























          • ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

            – NetProvoke
            Nov 25 '18 at 11:44
















          1














          This question is a little disjointed, however lets try to make sense of it a little



          Firstly lets get the pattern right



          try
          {
          await _semphoreSlimCreateArende.WaitAsync();

          // do your sync work here

          }
          finally
          {
          // if this throws you are certainly doing something wrong
          _semphoreSlimCreateArende.Release();
          }


          Secondly, you should have your keyboard taken away from you for this



          catch (Exception)
          {
          }


          Don't ever blindly eat exceptions, and if you are getting them on _semphoreSlimCreateArende.Release you have serious problems already and you have to work out why




          Should the semaphoreslim be in the controller level instead?




          Use them at the level that makes the most sense, i mean if you need to sync a piece of code sync it there, not 13 levels up.




          Or should i change the controllers actions to not be async?




          if you have asnyc work make your controller async and let it propagate down the stack to your async code




          We have a deadlock like behaviour in our production-environment and i
          wonder if we use semaphoreslim correctly.




          Woah, are we talking DataBase Deadlocks, or Context DeadLocks. Either way this all sounds a little fishy



          FYI



          Using a SemaphoreSlim in a web based service can be a nasty approach. SemaphoreSlim works on a spin lock, and shouldn't be used for anything that blocks for any period of time. Spinlocks chew through the processor waiting for the lock to be released. Ideal you use them when there is a little of the lock biting, and in situations that don't block further down the chain.



          If you are deadlocking code, then you need to work out exactly what is causing the deadlock, if its a task based deadlock, then SemaphoreSlim is not the right tool for the job, if it is a non blocking race condition then maybe its a little more acceptable. If its a Database deadlock, then you have bigger problem.






          share|improve this answer
























          • ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

            – NetProvoke
            Nov 25 '18 at 11:44














          1












          1








          1







          This question is a little disjointed, however lets try to make sense of it a little



          Firstly lets get the pattern right



          try
          {
          await _semphoreSlimCreateArende.WaitAsync();

          // do your sync work here

          }
          finally
          {
          // if this throws you are certainly doing something wrong
          _semphoreSlimCreateArende.Release();
          }


          Secondly, you should have your keyboard taken away from you for this



          catch (Exception)
          {
          }


          Don't ever blindly eat exceptions, and if you are getting them on _semphoreSlimCreateArende.Release you have serious problems already and you have to work out why




          Should the semaphoreslim be in the controller level instead?




          Use them at the level that makes the most sense, i mean if you need to sync a piece of code sync it there, not 13 levels up.




          Or should i change the controllers actions to not be async?




          if you have asnyc work make your controller async and let it propagate down the stack to your async code




          We have a deadlock like behaviour in our production-environment and i
          wonder if we use semaphoreslim correctly.




          Woah, are we talking DataBase Deadlocks, or Context DeadLocks. Either way this all sounds a little fishy



          FYI



          Using a SemaphoreSlim in a web based service can be a nasty approach. SemaphoreSlim works on a spin lock, and shouldn't be used for anything that blocks for any period of time. Spinlocks chew through the processor waiting for the lock to be released. Ideal you use them when there is a little of the lock biting, and in situations that don't block further down the chain.



          If you are deadlocking code, then you need to work out exactly what is causing the deadlock, if its a task based deadlock, then SemaphoreSlim is not the right tool for the job, if it is a non blocking race condition then maybe its a little more acceptable. If its a Database deadlock, then you have bigger problem.






          share|improve this answer













          This question is a little disjointed, however lets try to make sense of it a little



          Firstly lets get the pattern right



          try
          {
          await _semphoreSlimCreateArende.WaitAsync();

          // do your sync work here

          }
          finally
          {
          // if this throws you are certainly doing something wrong
          _semphoreSlimCreateArende.Release();
          }


          Secondly, you should have your keyboard taken away from you for this



          catch (Exception)
          {
          }


          Don't ever blindly eat exceptions, and if you are getting them on _semphoreSlimCreateArende.Release you have serious problems already and you have to work out why




          Should the semaphoreslim be in the controller level instead?




          Use them at the level that makes the most sense, i mean if you need to sync a piece of code sync it there, not 13 levels up.




          Or should i change the controllers actions to not be async?




          if you have asnyc work make your controller async and let it propagate down the stack to your async code




          We have a deadlock like behaviour in our production-environment and i
          wonder if we use semaphoreslim correctly.




          Woah, are we talking DataBase Deadlocks, or Context DeadLocks. Either way this all sounds a little fishy



          FYI



          Using a SemaphoreSlim in a web based service can be a nasty approach. SemaphoreSlim works on a spin lock, and shouldn't be used for anything that blocks for any period of time. Spinlocks chew through the processor waiting for the lock to be released. Ideal you use them when there is a little of the lock biting, and in situations that don't block further down the chain.



          If you are deadlocking code, then you need to work out exactly what is causing the deadlock, if its a task based deadlock, then SemaphoreSlim is not the right tool for the job, if it is a non blocking race condition then maybe its a little more acceptable. If its a Database deadlock, then you have bigger problem.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 7:52









          Michael RandallMichael Randall

          38.4k84573




          38.4k84573













          • ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

            – NetProvoke
            Nov 25 '18 at 11:44



















          • ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

            – NetProvoke
            Nov 25 '18 at 11:44

















          ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

          – NetProvoke
          Nov 25 '18 at 11:44





          ”Using a SemaphoreSlim in a web based service can be a nasty approach.” - What is best practice in async code for a web based solution? Google async lock c# and semaphoreslim shows up

          – NetProvoke
          Nov 25 '18 at 11:44




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456051%2fshould-semaphoreslim-be-in-the-controller-instead-of-further-down-in-the-chain%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()