System.MissingMethodException: 'Method not found for XmlRpcProxyGen.Create - c#

I'm trying to communicate with a python server using XML-RCP but when I try to compile the following code, the exception "System.MissingMethodException: 'Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.'" displays, I've found no documentation about this issue
using CookComputing.XmlRpc;
using System;
[XmlRpcUrl("http://192.168.5.211:8000")]
public interface FlRPC : IXmlRpcProxy
{
[XmlRpcMethod("add")]
int add(int x, int y);
}
class Program
{
static void Main(string[] args)
{
FlRPC proxy = XmlRpcProxyGen.Create<FlRPC>();
Console.WriteLine(proxy.add(2, 3));
}
}
I just want to skip this issue to continue to work on my project!

If the application you are trying to build is a .NET Core Application the xmlrpcnet package might be exclusively for the .NET Framework.
Try to replace it with Horizon's .NET Core port, Horizon.XmlRpc.Core and see if it helps.

Related

How do I set up an environment to solve LeetCode problems in Visual Studio? C#

When making the file, I am thinking of selecting a console application. But which target framework do I choose? Is this incorrect? Also, I am having trouble figuring out how to make a method in the class Program that is able to be called in the Main method. Can someone give me some advice?
one thing you can do is using interface to keep your code clean; for example :
you create an interface like this:
public interface IQuestionSolving
{
public void Solution();
}
you create some question class :
public class Question1 : IQuestionSolving
{
public void Solution()
{
}
}
and you use it like this :
static void Main(string[] args)
{
IQuestionSolving solve = new Question1();
solve.Solution();
Console.ReadKey();
}
now each time you solve a question you need to change
IQuestionSolving solve = new Question1();
to
IQuestionSolving solve = new Question2(); // 2 3 4 .. etc
you can extract your project as template so you dont have to do this each time .
or you can just use one solution and many classes .
This will get you started with Visual Studio:
Create a new console project - use the latest version of C#, which is probably what VS will "suggest" to you. Currently that's .NET 6 or .NET 7
A modern (net 6 or later) console app lets you start writing code immediately. You could create a method and then call the method right in this little Program.cs file that you start out with. However, I would probably do the following instead:
a) Create a new class for your "problem"
b) In that class create a method that solves the problem.
c) In your Program.cs add a using statement to use the namespace that your new class uses
d) In your program.cs instantiate that class and call its method/test its method
Here is an example:
Program.cs
using LeetCodeProject;
var solver = new Problem001_CalculateSquareRoot();
var solution = solver.calculate_square_root(8);
Console.WriteLine(solution);
Console.WriteLine("Press any key...");
Console.ReadKey();
Problem001_CalculateSquareRoot.cs (solves one leetcode problem)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCodeProject
{
public class Problem001_CalculateSquareRoot
{
public double calculate_square_root(int number)
{
double root = 1;
int i = 0;
while (true)
{
i = i + 1;
root = (number / root + root) / 2;
if (i == number + 1)
{
break;
}
}
return root;
}
}
}
Now you can just add new classes for each problem, and as you work on them just edit Program.cs to create the class you are currently working with and calls its solution methods.
I can (and would - and actually have, in similar cases) implement an interface for this, but the goal here is not to get into OO design principles, but just to get you started so you can get to work on the leetcode problems...once you have a few done you can start thinking about better organization of the code.

CS0246 error with OdbcConnection line in C#

Ive been chasing this CS0246 error for a couple hours and am not sure how to resolve it. Given this simple C# code:
using System;
// using Microsoft.Data.Odbc;
using System.Data.Odbc;
namespace dotnetdb
{
class Program
{
static private void SelectRows(string[] args)
{
string passWord = "PWD=password";
string uName = "UID=username";
string dbServer = "SERVER=server";
string dbName = "DATABASE=db";
string driver = "DRIVER={ODBC Driver 13 for SQL Server}"
string connString = // assembled from above
string sql = // sql;
OdbcConnection conn = new OdbcConnection(connString);
conn.Open();
OdbcCommand cmd = new OdbcCommand(sql, conn);
// work with cmd
Console.WriteLine("Didnt kick the bucket!");
}
}
}
The Microsoft stanza on line 2 yields a CS0234 error. The stanza on line 3 (from the Microsoft docs) gives me the CS0246:
Program.cs(20,13): error CS0246: The type or namespace name 'OdbcConnection' could not be found
I use this ODBC connection in go and python all the time but this is my first attempt at using it with C#. Also my first ever C# program. The code above is scraped almost directly from the MS docs - what am I missing? How do I get access to System.Data.Odbc?
Am I trying to run before I learn how to walk with C#?
Note that applications created with dotnet build [console|webapi] build and run just fine.
Thanks!
You need to add it as a reference. Refer to this question on how to add a reference in Visual Studio Code. I also noticed that your program doesn't have a Main() and that'll prevent it from compiling also.
Change this:
static private void SelectRows(string[] args)
to
static void Main(string[] args)
Or call it from Main() like this:
static void Main(string[] args)
{
SelectRows(args);
}
private static void SelectRows(String[] args)
{
...
}
In general references are a piece of compiled code, mostly in .DLL format which you can include in your own project so you can use the methods/code that the reference provides.
For example,
Let's say I have MyMath.dll which was created by somebody and I want to use this method in it.
int Add(int a, int b) {
return a + b;
}
I have to include that MyMath.dll that somebody else created in order to use that Add() method. So when I want to use it, I use something like this.
using MyMath; // MyMath.dll
static void Main(string[] args)
{
MyMath calculator = new MyMath();
int result = calculator.Add(1, 2);
}
If you don't know, Visual Studio has a free community version that's pretty powerful too.

XSockets - custom controller is not registering

I've been trying to evaluate XSockets but it appears I've hit my first stumbling block pretty early. I can connect to Generic controller just fine but custom controllers do not seem to work at all - I get a custom message: "The handler name was not found in loaded plugins". A Google search shows one other person having this problem in SE, but their solution did not work for me.
I've created a console project and installed the latest XSockets 3.03 from NuGet. My code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSockets.Core.Common.Socket;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using XSockets.Core.Common.Socket.Event.Interface;
namespace XSockets2
{
class Program
{
static void Main(string[] args)
{
using (var server = XSockets.Plugin.Framework.Composable.GetExport<IXSocketServerContainer>())
{
Console.WriteLine("running!");
server.StartServers();
Console.ReadLine();
server.StopServers();
}
}
}
public class TestCont: XSocketController
{
public override void OnMessage(ITextArgs textArgs)
{
this.SendToAll(textArgs);
}
}
}
And my Javascript
function connect2() {
var host = "ws://localhost:4502/testcont";
var conn;
conn = new XSockets.WebSocket(host);
conn.on(XSockets.Events.open, function (clientInfo) {
message(clientInfo.ClientGuid); //appends message to textarea
console.log('Open', clientInfo);
});
conn.on('OnMessage', function (d) {
message(d);
console.log('Message', d);
});
conn.on(XSockets.Events.onError, function (err) {
message(err.CustomMessage);
console.log('Error', err);
});
conn.on(XSockets.Events.close, function () {
message('Closed');
console.log('Closed');
});
First of all the latest version is 3.0.2 (not 3.0.3) but that is not important :)
There is a well known and documented bug in the plugin framework for the latest version. The bug only affect you if you run a console application (or any other *.exe) project since xsockets by default only looks in *.dll and not *.exe.
The issue and work around is described here
But your code will not work anyway since you have an error (from what I can see).
Your controller is named "TestCont" but you connect to "testcont". The connectionstring is case sensitive.
EDIT: I also think you are missunderstanding the method OnMessage since you have added a subscription to that exact name.

Why can't I read a db4o file created by a Java app in a C# app?

I have a db4o database that was generate by a Java app and I'm trying to read it using a C# app.
However, when running the following line of code:
IObjectContainer db = Db4oEmbedded.OpenFile(#"..\..\..\Databases\people.db4o");
I get the following error:
Unable to cast object of type
'Db4objects.Db4o.Reflect.Generic.GenericObject' to type
'Db4objects.Db4o.Ext.Db4oDatabase'.
Any ideas? I know there are person objects that contain personId fields (along with others) in the DB. I'm using db4o version 8. I'm not sure what version was used to generate the database.
The entire program is:
using System;
using System.Collections.Generic;
using System.Linq;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;
using MyCompany.Domain;
namespace MyCompany.Anonymizer
{
internal class Program
{
// Private methods.
private static IEmbeddedConfiguration ConfigureAlias()
{
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.AddAlias(new TypeAlias("com.theircompany.Person", "MyCompany.Domain.Person, MyCompany.Domain"));
configuration.Common.Add(new JavaSupport());
return configuration;
}
private static void Main(string[] args)
{
IObjectContainer db = Db4oEmbedded.OpenFile(#"..\..\..\Databases\people.db4o");
try
{
IList<Person> result = db.Query<Person>();
for (int i = 0; i < result.Count; i++)
{
Person person = result[i];
Console.WriteLine(string.Format("Person ID: {0}", person.personId));
}
}
finally
{
db.Close();
}
}
}
}
The most common scenario in which this exception is thrown is when db4o fails to resolve the type of a stored object.
In your case, db4o is failing to read one of its internal objects which makes me believe you have not passed the configuration to the OpenFile() method (surely, the code you have posted is not calling ConfigureAlias() method);
Keep in mind that as of version 8.0 no further improvement will be done regarding cross platform support (you can read more details here).

.net 2.0 Causing Error: ' New type requires () ' - Only happens in .net 2.0

The following code is being produced in CodeDom. When I set the target framework to .net 4.0 it works fine - no errors or warnings. When I set the target framework to .net 2.0, I get the following error:
CS1526: A new expression requires () or [] after type
test soVar;
soVar = new test { foo = 0x10007 }; // Error occurs on this line
[StructLayout(LayoutKind.Sequential)]
struct test
{
public uint foo;
}
What is going on here?! Why would switching to .net 2.0 all of the sudden raise an error?
Look forward to any ideas here.
Thanks,
Evan
test soVar;
soVar = new test { foo = 0x10007 }; // Error occurs on this line
.net 2.0 does not support object initializers. it will have to do
test soVar;
soVar = new test();
soVar.foo = 0x10007;
I am running Visual Studio 2010, and do not experience the problem you describe.
I created a project with .Net Framework 2.0 as the target, and this program compiles:
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
[StructLayout(LayoutKind.Sequential)]
struct test
{
public uint foo;
}
class Program
{
static void Main(string[] args)
{
test soVar;
soVar = new test { foo = 0x10007 };
}
}
}
Maybe you are somehow using C# 2.0 (e.g. Visual Studio 2005), not .Net 2.0?
To solve this problem, use Bala's solution (don't use object initializers).

Categories

Resources