Developer forum

Forum » Development » Web service functionality in ashx file error

Web service functionality in ashx file error

Vilius Janulis
Reply
Hi i need to create web service in the server along with DW solution.
So when it makes http request, it should return some data from DW.

 

But i faced the problem just in the start of the creation.
 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

[NullReferenceException: Object reference not set to an instance of an object.]
   Dynamicweb.Admin.Global.Application_OnPreRequestHandlerExecute(Object sender, EventArgs e) +502
   CustomModules.Global.Application_OnPreRequestHandlerExecute(Object sender, EventArgs e) in .......\Application - working\Global.asax.cs:62
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Any ideas why i am getting this one ?

My code is just simple testing file.


namespace CustomModules.CustomModules.CustomModuleName
{
    /// <summary>
    /// Summary description for WebRequests
    /// </summary>
    public class WebRequests : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("groups");
           
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        
    }
}



Tried to change web.config to add http handlers and etc. Nothing helped.



Version Information: Microsoft .NET Framework Version: 2.0.50727.3625; ASP.NET Version:2.0.50727.3634
DW version : 19.2.8.0
IIS 6
Cant change framework version since solution and custom modules are with version 3.5

How this should be done and what could be the problem ?


Replies

 
Pavel Volgarev
Reply
Hi Vilius,

Try implementing IRequiresSessionState interface (MSDN).

-- Pavel 
 
Vilius Janulis
Reply
 Tried it, did not really helped. Same error
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Are you implementing a handler or a web service? You mention a web service but your code shows a handler.

If you're implementing this with a web service, you need to mark the service methods with the WebMethod attribute:

[WebMethod(EnableSession = true)]
public void DoWork()
{ ... }
 

Also, what's happing in your Global.asax around that line that caused the error? Is that custom code or just the standard DW code?

I implemented both web services as well as handlers in DW sites, so it should definitely work.... ;-)

Cheers,

Imar

 
Vilius Janulis
Reply
 Sorry it is a handler. And yes global.asax is standard one.

public void Application_OnPreRequestHandlerExecute(object sender, EventArgs e)
{
	GlobalAsax.Application_OnPreRequestHandlerExecute(sender, e);
}

maybe you can provide with example, or step-by-step guide ? since i can not figure it out, why it through such error in DW
 
Vilius Janulis
Reply
 And yes, we also have made this work on other solution but it was framwork 4.0 and DW 8, so i am thinking maybe there should be something else used in version 7 or etc. or something with solution ... ideas ?
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
Hi Vilius,

To test this out, I just set up a new DW 7 web site, and added a Generic Handler with your code. I had an error first which wasn't legible (only garbled content). I removed the HttpCompression settings from web.config and found out the code mistake in the ASHX part of the file. After that I could successfully write to the Response object. Turning HttpCompression afterwards didn't make a different and it kept working, so I don't think that this has anything to do with it.

I moved my handler from the root of the site into the CustomModules folder and then I also got the Object Reference exception. I implemented IRequiresSessionState on the handler and instead of seeing the error I was redirected to /Admin to log in. Once I was logged in, I could successfully request the handler from the CustomModules folder. So, I see two solutions:

1. Move your handler outside of the CustomModules folder as Dynamicweb will perform an authorization check on the handler which requires session state.

2. Implement IRequiresSessionState. This should solve the session state problem, but obviously only works when users are logged in to the backend which doesn't make it a suitable solution for the public frontend.

Hope this helps,

Imar

 
Vilius Janulis
Reply
 Worked. Thanks for help everyone :)

 

You must be logged in to post in the forum