Context.Handler conversion error - asp.net

Trying to do the following conversion:
CType(Context.Handler, MyCustomHandler)
But it is throwing following error.
Unable to cast object of type 'ASP.MyWebForm_aspx' to type 'MyCustomHandler'.
Even both "Context.Handler" & "MyCustomHandler" are of type IHttpHandler.
**MyCustomHandler implements IHttpHandler.
Thanks.

I think there might be some more methods or properties in Handler than just what is exposed by the IHttpHandler interface. Try to cast to IHttpHandler this should work.
I don't think you can cast between two classes implementing an interface, as you can't cast between two classes derived from an abstract class, but you can cast both of them to the base class or interface

After some hours of effort, resolve this issue. The conversion was legal, but the custom handler I registered was in section compliant to IIS6 and older. Since I was running my website locally on IIS7 under Integrated mode, which looks registered handlers in section under , and this handler wasn't registered there. There were two possible solutions:
1) Add custom handler in section
2) Change Integrated to Classic mode in II7
I went with 2nd one.
**This also resolves the issue why it was working when I was publishing on server with IIS6 (Classic) and not locally (II7 with Integrated Mode)

Related

COM visible .NET class cannot get System.Web.HttpContext.Current when used in classic asp

My company would like to update legacy visual basic 6/classic asp code. We do not have access to a visual basic 6 compiler, so it seemed like a good idea to use Visual Studio 10 and recompile the visual basic 6 code as a VB.NET class. With some minor modifications everything compiled and we registered it as a com object to be called from classic asp.
Now we are trying to access global variables from the asp server. After an extensive search on Google, the only way mentioned was to use System.Web.HttpContext.Current or System.Web.HttpContext.Current.Application . However, this doesn't appear to work.
Here is the code leading up to error. We have imported System and System.Web.
Public Function DoSomethingUseful() As String
Dim objCurrent As System.Web.HttpContext
DoSomethingUseful = Nothing
objCurrent = System.Web.HttpContext.Current
If objCurrent Is Nothing Then
Throw New Exception("No Context.") '<--- THIS EXCEPTION IS THROWN
End If
The problem is we can't get a current instance of the http context and the exception is thrown. Every example I've seem shows that the line objCurrent = System.Web.HttpContext.Current should work but it doesn't. Perhaps it's because we are not using ASP.NET? If not then why isn't it working?
Yes, it is because you're not using ASP.NET.
Despite the similar sounding names, and to a certain degree similar design choices, there is no relationship whatsoever between ASP.NET anything and "Classic" ASP anything. They are completely independent environments.
To access directly the ASP context objects (not the similarly-named ASP.NET context objects which in your application don't even exist), your object must be a registered COM+ object, so:
Your class must inherit from System.EnterpriseServices.ServicedComponent *
On Windows 7/2008 and up, you must enable the "Allow IIS Intrinsic properties" from Component Services, just like you would have to in VB6 (open \Component Services\My Computer\COM+ Applications\YourComPlusApp\Components; select all the objects that need the ASP objects; right-click; click on Properties; go to the Advanced tab).
You can then retrieve the ASP objects (Application, Server, Request, Response, Session) from the COM+ ObjectContext.
Your code would look something like this:
objCurrent = System.EnterpriseServices.ContextUtil.GetNamedProperty("Application")
There are also attributes that you can add to your class to simplify the deployment of the code, but that's a bit outside the scope of this question.
For your reference:
ASP Built-in Objects
ContextUtil.GetNamedProperty Method
* I don't remember 100% if this is a hard requirement. It might actually depend on what the object is actually doing with COM+.

asp.net web service, Error Generating XML Document

I've created an asp.net web service and am trying to test it using my client. The web method I'm currently testing returns a custom object named GetAllTicketsSinceLastPullDateResult, which contains one ArrayList of custom clsTrip objects, and a custom object called FaultResponse. This is how I'm using my client:
ServiceReference1.ServiceSoapClient myClient = new ServiceReference1.ServiceSoapClient();
ServiceReference1.GetAllTicketsSinceLastPullDateResult result = new ServiceReference1.GetAllTicketsSinceLastPullDateResult();
result = myClient.getAllTicketsSinceLastPullDate(myUser);
But I get the following error:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type clsTicket was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
I Google the error and most of the answers I find have to do with serializing derived classes. But my clsTicket class is not a derived class. What can be causing this error? How do I use XmlInclude or SoapInclude?
Thanks in advance!
Okay I finally got it to work, I ended up putting the following line:
[XmlInclude(typeof(clsTicket))]
Between [WebMethod] and my method's definition. So far so good.
I don't think you need to 'new' result:
ServiceReference1.GetAllTicketsSinceLastPullDateResult result;
result = myClient.getAllTicketsSinceLastPullDate(myUser);
Also, how do you know the error isn't on the server?
Can you call the function with a web browser?
Have you updated your service since initially adding the reference to your project? If you're using Visual Studio's "Add Web Reference" feature and you update the service, often times there is a configuration or parameter chance, which can generate SOAP errors.
Try right clicking on the Web Service in question through visual studio and select "Update" option. Recompile the app and see if that resolves your issue.

Error when using DependencyResolver for Controller instantiation with MVC 3

I'm using MVC 3 and using the following code when the application starts...
UnityContainer container = new UnityContainer();
new UnityMappings(container);
DependencyResolver.SetResolver(new UnityServiceLocator(container));
Now when the app runs I'm getting the following error (but only sometimes)...
Activation error occured while trying to get instance of type
IControllerFactory, key ""
Interestingly, if I continue with the web request, the website works normally.
Any ideas? I can't see what I'm doing differently from before when this worked fine.
Cheers, Ian.
MVC3 requests a lot more than just controllers from the DependencyResolver. For most of them MVC3 falls back to the default implementation in case the DependencyResolver does not return an instance.
In your case it requests the IControllerFactory which is unknown to your IoC container and it throws an exception which is caught by the UnityServiceLocator implementation and null is returned. MVC then falls back to the default controller factory.
Unlike other IoC containers Unity does not provide an optional TryResolve and therefore does not support a proper exceptionless implementation of the DependencyResolver.
I would suggest first looking through the config and make sure everything is correct there, then I would make sure I had all the assemblies needed for Unity referenced in the project. That error message may (in my experience) point to an error in config or a missing DLL, perhaps an assembly you want to load in the container or another DLL needed by a DLL loaded by Unity?
Hope this helps.

How to create a System.Web.UI.Page object in a winform application?

I have a class which inherits from the System.Web.UI.Page class, like mysystem.
When I try to create an instance of mysystem in a winform appplication it throws an HttpException:
Session state can only be used when
enableSessionState is set to true,
either in a configuration file or in
the Page directive. Please also make
sure that
System.Web.SessionStateModule or a
custom session state module is
included in the
<configuration>\<system.web>\<httpModules>
section in the application
configuration.
I then googled this problem and try to enable the session state, but all are not affected.
So, can anybody tell me how to solve this case?
Any class that inherits from System.Web.UI.Page will attempt to utilise parts of the asp.net runtime when it's instantiated, so when it's used in a winforms application, it just won't work.
If there are methods that you need inside this class from another DLL, I'd suggest refactoring them into a separate class as:
If you want to use them in a winforms app, they're almost certainly not specific to the page so should be shared anyway
There's no good reason to try and use the asp.net runtime (other than, perhaps, caching) inside a winforms application

Method 'XYZ' cannot be reflected

We have consumed a third party web service and are trying to invoke it from an ASP.NET web application.
However when I instantiate the web service the following System.InvalidOperationException exception is thrown:
Method 'ABC.XYZ' can not be reflected.
System.InvalidOperationException:
Method 'ABC.XYZ' can not be reflected.
---> System.InvalidOperationException: The XML element 'MyDoc' from namespace
'http://mysoftware.com/ns' references
a method and a type. Change the
method's message name using
WebMethodAttribute or change the
type's root element using the
XmlRootAttribute.
From what I can gather there appears to be some ambiguity between a method and a type in the web service.
Can anyone clarify the probably cause of this exception and is there anything I can do to rectify this or should I just go to the web service owners to rectify?
Edit: Visual Studio 2008 has created the proxy class. Unfortunately I can't provide a link to the wsdl as it is a web service for a locally installed thrid party app.
I ran into the same problem earlier today.
The reason was - the class generated by Visual Studio and passed as a parameter into one of the methods did not have a default parameterless constructor. Once I have added it, the error had gone.
It seems the problem is down to data type issues between VS and the web service that was written in Java.
Ultimately it was fixed by manually editing the class and schema files that were created by VS.
I have come across the exact same problem when I was consuming a 3rd party web service. The problem in this instance was that the mustUndertand property in the reference file was looking for a Boolean, whereby the namespace property looked for a string.
By looking through the reference i was able to idenitfy the offending property and simply add "overrides" to the method signature.
Not ideal as any time you update the service you have to do this but I couldn't find any other way around this.
To find the reference file select "all files" from the solution explorer
Hope this helps
I'm guessing the wsdl emitted by or supplied with the service is not in a form that wsdl.exe or serviceutil can understand - can you post the wsdl or link to it?
how are you creating the proxy classes?
Also you might like to try and validate the wsdl against the wsdl schema to check its valid
In my case I was getting a "method cannot be reflected" error due to that fact that in the class being returned by method, I had failed to expose a default parameter-less constructor.
I was working in VB.NET. In my return class I had declared a "New(..)" method that took a couple parameters (because that is how I wanted to use it in my code). But by doing so, I had supressed the default (hidden) parameterless New() constructor that VB adds behind the scenes. Apparently the web service handler requires that a parameterless constructor be available. As soon as I added back into my class a parameterless New() constructor, it all worked fine.
I got the same message but mine was caused by a missing System.Runtime.Serialization.dll since I tried to run a 3.5 application on a machine with only .NET 2.0 installed.
I had the same issue but I found that one of the WebMethod parameters has a member that is of type interface that is why VS could not serialise it. here is the exception when trying to download the disco file
System.InvalidOperationException: Cannot serialize member
'Leopard.JobDespatchParameters.SendingUser'
of type 'Leopard.Interfaces.IUser', see inner exception for more
details. ---> System.NotSupportedException: Cannot serialize member
Leopard.JobDespatchParameters.SendingUser
of type Leopard.Interfaces.IUser because it is an interface.
Old thread but I had a different issue, Maybe of help to someone. referenced dlls were mixed up between two versions on data layer and service layer that caused the problem.
Another scenario where this error can happen: I simply had another web method with the same name (but different parameters)in my web service that slipped in during a code merge. After I deleted the old method it worked.

Resources