PHP SOAP returning wrong values to clients - c#

I'm trying to return a value from my (MySQL) database to a C# client, through SOAP.
The server is written in PHP, which I think is where everything goes wrong:
class foo{
function bar()
{
$result = $connection->query("SELECT value
FROM table
WHERE id='$id'");
$row = $result->fetch_assoc();
return (int)$row['value'];
}
}
Will turn up as 0 in C#, no matter what value was in the database. However,
class foo{
function bar()
{
return 5;
}
}
turns up as the correct value (5 in this case) in C#.
What am I missing here? It seems that data from the database will not be returned to C# correctly, but any other ways of writing the same value (statically) in PHP, will succeed.
EDIT:
I've now set up a PHP SOAP-client, which receives the same values as C# does. So it seems that the problem is with the PHP SOAP-server, communicating the wrong values to the client (whenever the value to return is not static).
EDIT:
I have now found the solution to my problem; My WSDL file had a complex request type, even though it only required a simple type. Changing this complex type to a simple type, fixed the problem. Do not ask me why - it does not make the slightest sense to me!

You are asking for an associative array. This may return more that one item.

Related

How to get value from GetElementById?

nxuybcbkbcbggkcwcbregrwyywbrgewbyrewyreyrwebyrwrwe
test
Browser.ExecuteScriptAsync();
Sends javascript to be executed and expects nothing in return, so trying to assign 'nothing' (ie void) to an HtmlElement variable is a no go.
If you are looking to send the page a bit of javascript, and use what is sent back, you need to use EvaluateScriptAsync()
This will return a Task<JavascriptResponse> which will still not work if you are trying to assign it to Size. Here is the bad news: JavascriptResponse can only be basic data types (int, bool, string, etc.). As per their documentation:
Only trivial values can be returned (like int, bool, string etc) - not
a complex (user-defined) type which you have defined yourself. This is
because there is no (easy) way to expose a random Javascript object to
the .NET world, at least not today. However, one possible technique is
to turn the Javascript object you wish to return to your .NET code
into a JSON string with the Javascript JSON.toStringify() method and
return that string to your .NET code. Then you can decode that string
into a .NET object with something like JSON.net. See this MSDN link
for more information.
(https://msdn.microsoft.com/en-us/library/ie/cc836459(v=vs.94).aspx)
For more info see:
https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions

Mapping Enum to string column with custom SqlMapper.ITypeHandler - Dapper

I have a large number of PL/SQL stored procs that return columns with single character strings representing some kind of status value from a fixed range. In the project I'm working on, these columns have been mapped by Dapper to string properties on the domain objects, which are awkward and unreliable to manage, so I'd like to switch to enums.
If I used enums with single character names like enum Foo {A, P} I'm pretty sure Dapper would map them correctly but I don't want that, I want enums with descriptive labels like so:
enum Foo {
[StringValue("A")]
Active,
[StringValue("P")]
Proposed
}
In the above example, StringValueAttribute is a custom attribute and I can use reflection to convert the "A" to Foo.Active, which works fine - except I need Dapper to perform that conversion logic for me. I wrote a custom type handler to do this:
public class EnumTypeHandler<T> : SqlMapper.TypeHandler<T>
{
public override T Parse(object value)
{
if (value == null || value is DBNull) { return default(T); }
return EnumHelper.FromStringValue<T>(value.ToString());
}
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.DbType = DbType.String;
parameter.Value = EnumHelper.GetStringValue(value as Enum);
}
}
//Usage:
SqlMapper.AddTypeHandler(typeof(Foo),
(SqlMapper.ITypeHandler)Activator.CreateInstance(typeof(EnumTypeHandler<>).MakeGenericType(typeof(Foo)));
The registration with SqlMapper.AddTypeHandler() seems to work fine, but when my DbConnection.Query() code runs, I get an error saying that the value 'A' could not be converted - the error is thrown from Enum.Parse, suggesting that Dapper isn't actually calling my type handler at all despite it being registered. Does anyone know a way around this?
Another user has reported this as an issue on Dapper's github site. Seems like it's a deliberate optimisation specifically around enums in Dapper, so I've changed my database model rather than trying to change the mapping code. I looked at trying to modify Dapper itself, but the source code of Dapper is optimised like nothing I've ever seen, emitting opcodes to perform conversions in the most performant way possible - no way I want to start trying to work out how to make changes there.

Deserializing a model from an api that contains Enums

We're developing a web application using ASP.Net MVC 5 C#, I'm having difficulties coming up with a nice solution for my problem.
We're accessing an api that has a few enums coming across as text (i.e. "yes", "no", "notfound", "na") on the model itself I'd like to have strongly typed enums that store in the database as an integer (save a little space I think) the issue comes when we're deserializing the response from the api. In the response it will comes across as text (see above) where as the enums will be expecting an integer.
How is this done? I really hate asking questions where I haven't tried anything but my web searches hasn't resulted in anything. If you needed any code please let me know and I'll update the question with the segments needed. As of right now the model has several strongly typed enums and the api is returning strings of the enum values. by the way The enum texts are the same as the return values.
In-case it makes any difference we're also using EF Code First 6
You can use Enum.TryParse to convert the string to its enum value
public enum MyEnum
{
NA
Yes,
No
}
then
MyEnum value = MyEnum.NA;
Enum.TryParse<MyEnum>(theTextValue, out value );

Is this a namespacing problem?

I am experiencing a strange behavior with very basic web service development. This question might be dumb but I think someone would be able to explain this observation.
I am developing a web service with a web method, MyWebMethod
MyWebMethod(MyEnum Param, .....)
Where,
public enum MyEnum : int
{
Type_1 =1;
Type_2 =2;
Type_3 =3;
}
Now I am using my client to communicate with this service but for every request type, Type_1, Type_2 etc the service captures it as Type_1. As an example, if I create a break point at MyWebMethod in my web service, I see Type_1 as param1 type. I guess this is a problem with Namespacing. I cannot see any other defects on the code. Any Idea based on the experiences?
When enum is serialized, only its string representation is transferred through wire (names), not the values. I believe thats the reason you are getting the wrong values.
Check out this 2 articles for more info
WebServices_and_Enums
Using enum in web service parameter
If this is a WCF web service and a .NET 2.0 client generated with wsdl.exe for each value type in the method signature there will be a boolean parameter added called XXXSpecified which you need to set to true. Check this blog post for more details.
I guess your enum does not need to inherit from int. You are providing name and value in the enumeration, that should suffice. I am assuming all your code is .NET 2.0. As test , return an enumeration value from the webservice. Just to make sure XML Serialization is working as expected when the service is hit directly by the browser.

Serialization of non-required fields in protobuf-net

I have a working java client that is communicating with Google, through ProtoBuf serialized messages. I am currently trying to translate that client into C#.
I have a .proto file where the parameter appId is an optional string. Its default value in the C# representation as generated by the protobuf-net library is an empty string, just as it is in the java representation of the same file.
message AppsRequest {
optional AppType appType = 1;
optional string query = 2;
optional string categoryId = 3;
optional string appId = 4;
optional bool withExtendedInfo = 6;
}
I find that when I explicitly set appId to "" in the java client, the client stops working (403 Bad Request from Google). When I explicitly set appId to null in the java client, everything works, but only because hasAppId is being set to false (I'm uncertain as to how that affects the serialization).
In the C# client, I always get 403 responses. I don't see any logic behind the distinction between not setting a value, and setting the default value, that seems to make all the difference in the java client. Since the output is always a binary stream, I am not sure if the successful java messages are being serialized with an empty string, or not serialized at all.
In the C# client, I've tried setting IsRequired to true on the ProtoMember attribute, to force them to serialize, and I've tried setting the default value to null, and explicitly set "", so I'm quite sure I've tried some configuration where the value is being serialized. I've also played around with ProtoBuf.ProtoIgnore and at some point, removing the appId parameter altogether, but I haven't been able to avoid the 403 errors in C#.
I've tried manually copying the serialized string from java, and that resolved my issues, so I'm certain that the rest of the HTTP Request is working, and the error can be traced to the serialized object.
My serialization is simply this:
var clone = ProtoBuf.Serializer.DeepClone(request);
MemoryStream ms = new MemoryStream(2000);
ProtoBuf.Serializer.Serialize(ms, clone);
var bytearr = ms.ToArray();
string encodedData = Convert.ToBase64String(bytearr);
I'll admit to not being quite sure about what DeepClone does. I've tried both with and without it...
It sounds like we want to force it to be excluded; for a first thing to try, you could try using the "detectmissing" option in the code-generation. This is possible from the IDE and command-line, but differently (let me know which you are using and I'll add more).
Another similar option is to add (in a partial class) a bool {memberName}Specified {get;set;}. There is an existing open report of an oddity involving default empty strings, that I am looking at.

Categories

Resources