November 22, 2010 WCF
November 22, 2010 WCF
public class CalculatorService : ICalculatorService
{
public int Execute(int a, int b, string operation)
{
Console.WriteLine("Received request: ({0}{2}{1}=?)", a, b, operation);
int result = -1;
switch (operation)
{
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
result = a / b;
break;
default:
Console.WriteLine("Our calc executed hara-kiri.");
Process.GetCurrentProcess().Kill();
break;
}
return result;
}
} public class CustomEndpointBehavior : IEndpointBehavior
{
public void Validate(ServiceEndpoint endpoint)
{
return;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
return;
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
foreach (var clientOperation in clientRuntime.Operations)
{
clientOperation.ParameterInspectors.Add(new OpParameterInspector());
}
}
}
public class OpParameterInspector : IParameterInspector
{
public object BeforeCall(string operationName, object[] inputs)
{
switch (operationName)
{
case "Execute":
VerifyExecuteParams(inputs);
break;
}
return null;
}
private void VerifyExecuteParams(object[] inputs)
{
if (new string[]{"+", "-", "*", "/"}.Where(x => x == (string) inputs[2]).Count() == 0)
{
throw new ArgumentException("Your operation should belong to folowing list: '+', '-', '*', '/'. Please verify and try again.");
}
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
return;
}
}| Markdown | Result |
|---|---|
| *text* | text |
| **text** | text |
| ***text*** | text |
| `code` | code |
| ~~~ more code ~~~~ |
more code |
| [Link](https://www.example.com) | Link |
| * Listitem |
|
| > Quote | Quote |
Don't you think that
if (new string[]{"+", "-", "*", "/"}.Where(x => x == (string) inputs[2]).Count() == 0)
is quite ugly way to write equivalent:
if (new []{"+", "-", "*", "/"}.All(x => x != (string) inputs[2]))
or
if (new []{"+", "-", "*", "/"}.Contains((string) inputs[2]))
??
forgot "logical not" in second case
Andrii, you are right there!
How can we get inputs name in BeforeCall ?