1
votes

UPDATE:

The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.

when i try to access wcf service i get this error: the reason is HttpContext.Current is null, what should i do in this case? any help?

Object reference not set to an instance of an object.

 System.Web.Script.Serialization.JavaScriptSerializer s = new            System.Web.Script.Serialization.JavaScriptSerializer();
        Person p = new Person() { FirstName = "First name", LastName= "last name" };
        string json = s.Serialize(p);
        System.Web.HttpContext.Current.Response.Write("jsoncallback" + json);} //error
2
Are you sure you added it declaratively. The error is pretty explicit. It looks as though you attempted to access your service without having ASP.Net compatibility mode enabled for that service. - Josh
i did and i double check that... - Nick Kahn
Ok so... a couple of questions. 1.) Is your WCF service hosted in IIS? 2.) Did you put the declaration on your service implementation or the contract? - Josh
not sure what was wrong but its working now, hosted in IIS - Nick Kahn

2 Answers

0
votes

HttpContext is an ASP.Net construct. If you want to be able to access it in your service, then you need to enable ASP.Net Compatibility for your service.

Either through the web.config:

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />  
</system.serviceModel>

Or declaratively:

[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]  
public class MyService : IMyService { ... }
0
votes

If simple response write is the thing for you, then consider using a simple HttpHandler (by implementing the IHttpHandler interface). The Response object is not meant be used in a WCF Service...

If WCF however is the thing for you (maybe the stack of tech it offers is something you need), then consider using the plumming already there to output json:

[ServiceContract] 
public interface IService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.**Json**)]
    String DoStuff();

}