When calling an extension method from another extension method, my solution was building ok, but in the published site (or the virtual asp.net server) I was getting the Compile Error "Ambiguous call".
public static string ExtensionMethodA(this ObjectToExtend myObj){//code here}
public static string ExtensionMethodB(this ObjectToExtend myObj){
string a = myObj.ExtensionMethodA(); // this line causes the error.
return a;
}
I haven't read enough to know exactly why, but here is the solution:
public static string ExtensionMethodA(this ObjectToExtend myObj){//code here}
public static string ExtensionMethodB(this ObjectToExtend myObj){
string a = ExtensionMethodA(myObj); // correct call.
return a;
}
Related
I have followed this steps to setup JSB. In my bounded-class, I need to return the source of an API request (I cannot, therefore, use GetPageSourceAsync because no JS context is ever created). So, I use the trick of copying and immediately pasting the page content to a string variable (see result). This code is not working, I get the following exception:
The ChromiumWebBrowser instance creates the underlying Chromium Embedded Framework (CEF) browser instance in an async fashion. The undelying CefBrowser instance is not yet initialized. Use the IsBrowserInitializedChanged event and check the IsBrowserInitialized property to determine when the browser has been initialized.
which is happening on bro.SelectAll();:
public class boundClass
{
ChromiumWebBrowser bro;
public boundClass()
{
bro = new ChromiumWebBrowser("https://google.com");
}
public string getAuthSource(string url)
{
string source = String.Empty;
bro.Load(url);
while (bro.IsLoading)
{
}
bro.SelectAll();
bro.Copy();
string result = Clipboard.GetText();
Clipboard.Clear();
return result;
}
}
What's the problem? The ChromiumWebBrowser seems well-initialized to me...
I have a method marked with Spring's #RequestMapping that includes an HttpServletRequest method parameter.
If I print out the results of a call to "request.getServletPath()" when the path is, say, "/things/{thingId}", I will get "/things/2489sdfjk43298f," where the {thingId} path parameter has been replaced with the actual value.
I want to print out the literal request path "/things/{thingId}"; I.e. with the curly-braced, un-replaced path parameter "{thingId}."
Is this possible in any way?
Edit: After looking at Sotirios's second comment below, I realize I may be looking at the problem backward. Here's what I'm actually trying to do...
I am trying to making a single endpoint under "/**" that gets the path from the HttpServletRequest, which I use to look up a value in an enum. This enum has several fields, one of which is obviously the aforementioned path, but another is the path of a target JSP file. I then put this path into a ModelAndView object and return it to display the page.
This was going just fine until I hit the first endpoint with a path parameter, because I obviously can't place the value "/things/2489sdfjk43298f" into the enum, because that will only match for that one specific thing with that one specific ID.
So perhaps the actual question would be: How would I do that look-up when parts of the path will change due to path parameters? Is there some sort of wildcard-containing String format I can use?
I guess this is turning into more of a enum-lookup/String-matching question. My bad.
Edit 2: Shortened example of the enum thing I'm talking about:
public enum JspEndpointType {
HOME("/home", "jsp/home");
private static final Map<String, String> pathMap;
private String requestPath;
private String jspPath;
static {
pathMap = new HashMap<>();
for (JspEndpointType jspEndpointType : JspEndpointType.values()) {
pathMap.put(jspEndpointType.getRequestPath(), jspEndpointType.getJspPath());
}
}
private JspEndpointValue(String requestPath, String jspPath) {
this.requestPath = requestPath;
this.jspPath = jspPath;
}
public String getRequestPath() {
return requestPath;
}
public String getJspPath() {
return jspPath;
}
public static String getByRequestPath(String requestPath) {
return pathMap.get(requestPath);
}
}
Shortened example of my endpoint:
#RequestMapping(value = "/**", method = RequestMethod.GET)
public ModelAndView showPage(HttpServletRequest request) {
return new ModelAndView(JspEndpointType.getByRequestPath(request.getServletPath()));
}
So things essentially boil down to trying to add to the enum a value like this:
THINGS("/things/{thingId}", "jsp/things/whatever")
..and then being able to pass in the path "/things/2489sdfjk43298f" and get back "/jsp/things/whatever."
Edit 3: I found this StackoverFlow question which directed me to Spring's UriComponentsBuilder, specifically the "fromPath" method. However, that seems to be the reverse of what I'm trying to do...
You may look for the #RequestMapping annotation on your own, using reflection.
We are using Spring MVC 3.2.3.RELEASE (annotations) with google app engine 1.8.0.
We added a StringTrimmerEditor for converting blank strings from form as null values and it is working fine, but as a side effect, all methods which use #RequestParams in controllers are expecting all the #RequestParams to be populated, otherwise they are throwing http error 400. We tried with different #RequestParam settings like (required=false) and (defaultValue="some value"), but it is not working.
here is how we are using it
#ControllerAdvice
public class ControllerSetup
{
#InitBinder
public void initBinder ( WebDataBinder binder )
{
StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);
binder.registerCustomEditor(String.class, stringtrimmer);
}
}
controller
#RequestMapping(value="/addreportitems", method=RequestMethod.POST)
public String saveEditForm(#ModelAttribute DCReport dcReport,
#ModelAttribute("loggedInEmployee") Employee someEmployee,
#RequestParam Integer someInteger,
Model m) {
....
}
If we dont put someInteger in form, we get an error
Error 400 Required Integer parameter 'someInteger' is not present
If we remove StringTrimmer, it works perfectly fine, are we missing something here? Thanks very much in advance for helping out.
The following is working for me:
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
#ResponseBody
#RequestMapping("/foo")
public String renderFoo(#RequestParam(required=false) String bar) {
return bar;
}
You must have made mistake somewhere when you were testing with required=false.
I'm trying to make a simple REST service in VS2010 (.NET 4) with one method that receives three string parameters. I want it to be accessed using both GET and POST (because 3'rd param could be quite big sometimes)
In class MyREST.cs I have this code
[WebGet(UriTemplate = "s={s}&sp={sp}&p={p}")]
public string Process(string s, string sp, string p)
{
// some processing
return result;
}
Same for POST
When I try to access the service with an url like
http://localhost:57129/OneTestREST/s=str1&sp=str2&p=str3
I get error
Exception Details: System.Web.HttpException: A potentially dangerous
Request.Path value was detected from the client (&).
If I change the attribute to
[WebGet(UriTemplate = "/{s}/{sp}/{p}")]
It works ok. But I would like to access it with first syntax
Any idea why is this happening?
Thank you
[WebGet]
public string Process(string s, string sp, string p)
{
// some processing
return result;
}
then
http://localhost:57129/OneTestREST/?s=str1&sp=str2&p=str3
I have a COM object that I am trying to wrap in a C# class in order to make it more readily available for other applications that wish to consume it.
I have the following code that creates an instance of the COM object, and then using reflection makes a call to a method to retrieve user data. This code works fine when it is located in an aspx page.
object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
However, when I move the code to a class file (JD_API.cs) in order to abstract it from the actual website, I can no longer get it to work. For example, I have the following static method that is declared like such:
public static string GetUserName() {
object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName",
System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
return username;
}
Unfortunately, the Server object is restricted to some ASP.NET libraries that are included by default in web applications, and so the above code was a no-go. So at this point I decided to try to create an instance of the COM object like such:
public static string GetUserName() {
Type type = Type.GetTypeFromProgID("jd_api.UserCookie");
object jdObj = Activator.CreateInstance(type);
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
return username;
}
However at runtime I get an error that says, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.".
I'm not sure where to go from here. Any help on how to abstract creating an instance of this COM object to a layer that is not within the web application itself would greatly appreciated. Thanks!!
Declare DLL functions within a class. Then define a static method for each DLL function you want to call.
The following code sample creates a wrapper named Win32MessageBox that calls the MessageBox function in User32.dll each time a .NET app calls the object Show method.
It requeres the System.Runtime.InteropServices namespace.
using System;
using System.Runtime.InteropServices;
class Win32MessageBox
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text,
String caption, uint type);
public static void Show(string message, string caption)
{
MessageBox(new IntPtr(0), message, caption, 0);
}
}
To call it, just type:
Win32MessageBox.Show("StackOverflow!", "my stack box");
The method where you call the above line doesn't need to be aware that it's a actually calling a function in an unmanaged DLL.
Resources: the MCTS Self-Paced Training Kit (Exam 70-536) by Tony Northrup.
Hove you tried usinsing interoperating
I've done the following in the past (working from memory so you might need to fiddle with this a bit):
Right Click "References" in your project
Select "Add Reference"
Selelct the "Com" Tab
Find and add your Com Instnace
In your class file
using yourComName;
public static string GetUserName()
{
yourComName.yourComClass jdObj = new yourComClass();
string username = jdObj.GetUserName(someParameters);
return username;
}
Hope this a) works and b) helps!