By default, when you create an new WCF Service in a web site, they live side-by-side and the WCF service cannot access the HttpContext.Current-object (and the other ASP.NET-features like File/URL-authorization, HttpModules and ASP.NET impersonation), and in some Ajax-scenarios, we really need it.
There are two simple steps for enabling it.
First, you need to turn it on in the web config, look for "system.serviceModel" and add the "serviceHostingEnvironment"-tag like this:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
Second, add the attribute "[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]" to your service-class (it can't be in the interface):
using System.ServiceModel.Activation;namespace Alaz.DotNetDiscoveries.JSONWithWCF{ // NOTE: If you change the class name "TrueJSON" here, you must also update the reference to "TrueJSON" in Web.config.[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class TrueJSON : ITrueJSON
{public MyClass DoWork()
{var returnObject = new MyClass
{Id = 1,
IHaveNoDataMemberAttribute = "Meaningless", Name = "Satchmo", NoPublicGet = "Hello world!",InternalProperty = 155
};
return returnObject;}
}
}
And you are done!
MSDN documentation here.

1 kommentar:
This was GREAT. It helped me immediately without difficult explanations and non-related stuff.
Skicka en kommentar