Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make partial method async

I have a generated code with partial method

{
    ...
    partial void InterceptOperationCall(IOperationContext context);
    ...

    async Task SomeMethod()
    {
        InterceptOperationCall(cntx);
        await LongOperation(cntx);
    }
}

and handwrited partial

{
    partial void InterceptOperationCall(IOperationContext context)
    {
    }
}

I need to do async calls inside InterceptOperationCall Does any one knows some way to workaround partial method restrictions?

Another words: I want to do InterceptOperationCall asynchronously and guaranteed before long operation, at the same time i want to optionaly declare body of this method in another file.

UPD as workaround solution i chose to:

  • not use generated partial method stubs, and wrap with dynamic proxy (Castle.DynamicProxy) and intercept with AsyncInterceptorBase from (Nito.AsyncEx)
  • another option I see rewrite codegenerator

Any way I keep looking for better solution, and if someone know another ways to provide optional ability to wrap async calls with somoe async logic please help me.

like image 767
gabba Avatar asked Oct 22 '18 19:10

gabba


People also ask

How do you implement a partial method?

In order to create a partial method, it must be declared first(like an abstract method), with a signature only and no definition. After it is declared, its body can be defined in the same component or different component of the partial class/struct . A partial method is implicitly private.

Can a void method be async?

With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started. Figure 2 illustrates that exceptions thrown from async void methods can't be caught naturally.

How do you call a partial method in C#?

cs partial class A { partial void Method(); } in class2. cs partial class A { partial void Method() { Console. WriteLine("Hello World"); } } now in class3. cs class MainClass { static void Main() { A obj = new A(); obj.

Should I make all methods async?

If a method has no async operations inside it there's no benefit in making it async . You should only have async methods where you have an async operation (I/O, DB, etc.). If your application has a lot of these I/O methods and they spread throughout your code base, that's not a bad thing.


1 Answers

You can use the async keyword when implementing the partial method.

So

async partial void InterceptOperationCall(IOperationContext context) {

}

should be no problem.

like image 62
Peter Schneider Avatar answered Sep 19 '22 03:09

Peter Schneider