Webservice Reflection Exception - c#

Good morning,
I have a bit of a dilemma.
I have a webservice reference in a C# project. That webservice calls upon a method called "PlaceOrder". That object has an property Item that is of type object.
When i create an instance of the webservice client i get a exception with "There was an error reflecting 'Item'.". This is a normal generated webservice reference in .net 3.5.
OrderServiceClient orderService = new OrderServiceClient();
orderService.Open(); <== exception occures here
orderService.Close();
Any help is welcome.

It has no clue what to expect in that property; have you included [XmlInclude] etc? But object simply doesn't work on a web service.
The ideal answer is to make the web-service much more specific, so it has (for example) a Customer (typed as such). Can you perhaps include an example of what the server-side code looks like for this? I'm surprised that it works at the server...
Re comment; Hmm... if the is 3rd party, this might be very tricky; you might have to resort to either editing the generating code (if XmlInclude works, which I can't guarantee), or hacking the wsdl... not simple. It sounds like you're using the 2.0 web-service reference (wsdl.exe, indirectly). You could also try:
WSE3 (wse3wsdl.exe)
WCF (in .NET 3.0 and avove; svcutil.exe)

Ok i did find a solution for this problem.
We managed to change the webservice to give a different response.
I discussed it with my manager and a couple of senior engineers and architects and solved it on a business level.
Since other .net and java applications also use this service (who havent been build yet). So any hack that we need to implement is no sollution to this problem.
To summerize:
There is no good sollution for this problem (yet)
Always think about how reusable your design is :)
EDIT:
I did found out, that if you make the object into a object[] it suddenly works. Why? i dont know.

Related

Atlassian.Net SDK / Jira - Getting available fields when creating new issue

I'm currently working on a connection to a cloud test environment (xyz.atlassian.net). So far I managed to authenticate a user but when I try to do some "silly" stuff like setting a priority on a Task issue the API call will fail with a 400 Bad request. I understand that the field Priority is not available for issue type Task but indeed for the type Bug.
My question is how would I know which fields can be set before I create the issue? I found some help in the REST reference where they say one could evaluate the result of:
/rest/api/2/issue/createmeta?expand=projects.issuetypes.fields
That indeed gives me a JSON containing a lot of data regarding issue types and their fields but I'm having a hard time parsing that. It looked like the records there would match the definition of IssueFieldEditMetadata but the serializer just can't seem to create an instance from the JSON.
Since there's a central Jira class within the root namespace with several service getters I'd expect to see some service that would provide something like GetFieldsForIssueType(string issueTypeKey) but I haven't found anything similar.
Am I doing it the wrong way? I'm pretty new to REST and Jira in general, so please correct me if I messed things up in this post. Always happy to learn ;-)
Best, Killian

SQL 2008: returning data rows as JSON?

I think this question is like clay pidgeon shooting.. "pull... bang!" .. shot down.. but nevertheless, it's worth asking I believe.
Lots of JS frameworks etc use JSON these days, and for good reason I know. The classic question is "where to transform the data to JSON".
I understand that at some point in the pipeline, you have to convert the data to JSON, be it in the data access layer (I am looking at JSON.NET) or I believe in .NET 4.x there are methods to output/serialize as JSON.
So the question is:
Is it really a bad idea to contemplate a SQL function to output as JSON?
Qualifier:
I understand trying to output 1000's of rows like that isn't a good idea - in fact not really a good idea for web apps either way unless you really have to.
For my requirement, I need possibly 100 rows at a time...
The answer really is: it depends.
If your application is a small one that doesn't receive much use, then by all means do it in the database. The thing to bear in mind though is, what happens when your application is being used by 10x as many users in 12 months time?
If it makes it quick, simple and easy to implement JSON encoding in your stored procedures, rather than in your web code and allows you to get your app out and in use, then that's clearly the way to go. That said, it really doesn't take that much work to do it "properly" with solutions that have been suggested in other answers.
The long and short of it is, take the solution that best fits your current needs, whilst thinking about the impact it'll have if you need to change it in the future.
This is why [WebMethod] (WebMethodAttribute) exists.
Best to load the data to to the piece of program and then return it as JSON.
.NET 4 has a support for returning json, and i did it as a part of one ASP.NET MVC site and it was fairly simple and straightforward.
I recommend to move the transformation out of the sql server
I agree with the other respondents that this is better done in your application code. However... this is theoretically possible using SQL Server's ability to include CLR assemblies in the database using create assembly syntax. The choice is really yours. You could create an assembly to do the translation in .net, define that assembly to SQL Server and then use contained method(s) to serialize to JSON as return values from your stored procedures...
Better to load it using your standard data access technique and then convert to JSON. You can then use it in standard objects in .NET as well as your client side javascript.
If using .net mvc you serialize your results in your controllers and output a JsonResult, there's a method Controller.Json() that does this for you. If using webforms an http handler and the JavascriptSerializer class would be the way to go.
Hey thanks for all the responses.. it still amazes me how many people out there have the time to help.
All very good points, and certainly confirmed my feeling of letting the app/layer do the conversion work - as the glue between the actual data and frontend. I guess I haven't kept up too much with MVC or SQL-2008, and so was unsure if there were some nuggets worth tracking down.
As it worked out (following some links posted here, and further fishing) I have opted to do the following for the time being (stuck back using .NET 3.5 and no MVC right now..):
Getting the SQL data as a datatable/datareader
Using a simple datatable > collection (dictionary) conversion for a serializable list
Because right now I am using an ASHX page to act as the broker to the javascript (i.e.
via a JQuery AJAX call), within my ASHX page I have:
context.Response.ContentType = "application/json";
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
I can then issue: json.serialize(<>)
Might seem a bit backward, but it works fine.. and the main caveat is that it is not ever returning huge amounts of data at a time.
Once again, thanks for all the repsonses!

Dynamically invoke web service at runtime

So, our application needs support for dynamically calling web services which are unknown at compile time. The user should therefore be able to specify a URL to a WSDL, and specify some data bindings for the request and reply parameters.
When Googling for answers, it seems like the way to do this is by actually compiling a web service proxy class at runtime, loading it, and invoking the methods using reflection.
I think this seems like a rather clunky approach, given that I don't really need a strongly typed set of classes when I'm going to cast my data dynamically anyway. Dynamically compiling code for doing something that simple also just seems like The Wrong Way To Do It.
Restricting ourself to the SOAP protocol, is there any library for C# that implements this protocol for dynamic use? I can imagine that it would be possible to generate runtime key/value data structures from the WSDL, which could be used to specify the request messages, as well as reading the replies. The library should then be able to send well-formed SOAP messages to the server, and parse the replies, without the programmer having to generate the XML manually (at least not the headers and other plumbing).
I can't seem to find any library that actually does this. Is what I want to do really that esoteric, or have I just searched the wrong places?
Thanks,
Ulrik
I think that you will need to reflect the signature of the unknown web service in order to know how to call it.
But instead of compiling a webservice proxy you could use a javascript ajax call at runtime once you know what you need to call. You could use, for example, jquery.ajax() to make an XMLHttpRequest to the unknown web service (once you know what to call).
I'm looking for such a solution, it seems that WSIF is the solution

Preprocessing C# - Detecting Methods

I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives.
I would use reflection, but the MethodInfo class does not reference the point in the original source.
EDIT: What I am really trying to do here is to support pre-conditions on methods, that pre-condition code being determined by attributes on the method. My initial thought being that I could look for the beginning of the method, and then insert generated code for handling the pre-conditions.
Is there a better way to do this? I am open to creating a Visual Studio Addin if need be.
This is a .NET 2.0 project.
Cheers
PostSharp or Mono.Cecil will let you do this cleanly by altering the generated code without getting into writing a C# parser which is unlikely to be core business for you...
Havent done anything of consequence with PostSharp but would be guessing its more appropriate than Mono for implementing something like preconditions or AOP. Alternately you might be able to do something AOPy with a DI container like Ninject
But of course the applicability of this idea Depends - you didnt say much other than that you wanted to insert code at the start of methods...
EDIT: In light of your desire to do preconditions... Code Contracts in .net 4 is definitely in that direction.
What sort of a tool do you have? Whats wrong with having a single Mono.Cecil.dll DLL shipped? Either way something other than a parser is the tool for the job.
I am sure there is an easier way but this might be a good excuse to take MGrammer for a spin.

How to make a "Call stack diagram"

Creating a call stack diagram
We have just recently been thrown into a big project that requires us to get into the code (duh).
We are using different methods to get acquainted with it, breakpoints etc. However we found that one method is to make a call tree of the application, what is the easiest /fastest way to do this?
By code? Plugins? Manually?
The project is a C# Windows application.
With the static analyzer NDepend, you can obtain a static method call graph, like the one below. Disclaimer: I am one of the developers of the tool
For that you just need to export to the graph the result of a CQLinq code query:
Such a code query, can be generated actually for any method, thanks to the right-click menu illustrated below.
Whenever I start a new job (which is frequently as I am a contractor) I spend two to three days reading through every single source file in the repository, and keep notes against each class in a simple text file. It is quite laborious but it means that you get a really good idea how the project fits together and you have a trusty map when you need to find the class that does somethnig.
Altought I love UML/diagramming when starting a project I, personally, do not find them at all useful when examining existing code.
Not a direct answer to your question, but NDepend is a good tool to get a 100ft view of a codebase, and it enables you to drill down into the relationships between classes (and many other features)
Edit: I believe the Microsoft's CLR Profiler is capable of displaying a call tree for a running application. If that is not sufficient I have left the link I posted below in case you would like to start on a custom solution.
Here is a CodeProject article that might point you in the right direction:
The download offered here is a Visual
Studio 2008 C# project for a simple
utility to list user function call
trees in C# code.
This call tree lister seems to work OK
for my style of coding, but will
likely be unreliable for some other
styles of coding. It is offered here
with two thoughts: first, some
programmers may find it useful as is;
second, I would be appreciative if
someone who is up-to-speed on C#
parsing would upgrade it by
incorporating an accurate C# parser
and turn out an improved utility that
is reliable regardless of coding style
The source code is available for download - perhaps you can use this as a starting point for a custom solution.
You mean something like this: http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/
Not to be a stuck record, but if I get it running and pause it a few times, and each time capture the call stack, that gives me a real good picture of the call structure that accounts for the most time. It doesn't give me the call structure for things that happen real fast, however.

Categories