As I mentioned in the comments, the right and better way to do it is to create Restful services over your Python
code and make http-requests from the C#
code. I don't know how much you know about web-frameworks in Python but there are tons of them that you can use. For your need, I would suggest Flask which is light-weight micro web-framework to create Restful web services.
This is very simple Flask web-service for the example: (you can check a running version of it here, I hosted it on pythonOnEverywhere)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from Flask!'
@app.route('/math/add/<int:num1>/<int:num2>')
def add(num1, num2):
return '%d' % (num1+num2)
This simple service, adds two number and returns the sum of them.
And C#
Code:
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
// Always use HttpClient as a singleton object
public static HttpClient _httpClient = new HttpClient() { BaseAddress = new Uri("http://al1b.pythonanywhere.com") } ;
public async static Task Main()
{
var num1 = 1;
var num2 = 4;
Console.WriteLine("Making http-request wait ...\r\n");
var mathAddResult = await _httpClient.GetAsync($"/math/add/{num1}/{num2}");
// 200 = OK
if(mathAddResult.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine(await mathAddResult.Content.ReadAsStringAsync());
}
}
}
The output:
Making http-request wait ...
5
The running version of code above is now runnable on .NET Fiddle.
TL;DR:
For understanding and learning Flask, take a look on its documentions. (It's short and well). I'm sure you will have complex web-services, such as accepting complex or pocco objects as your web-service inputs and returning complex objects (as json) as web-serivce results.
In that case you need to know how Flask jsonify
works, This link will tell you how.
Ok, on the other hand, in your C# application you will have those complex objects and scenarios as well. You need to know how to serialize, deseriaze and etc.
Microsoft did a great job for its tutorials here:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
and
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8