更新
无法激活该服务,因为它不支持ASP.NET兼容性。为此应用程序启用了ASP.NET兼容性。在web.config中关闭ASP.NET兼容模式,或者将AspNetCompatibilityRequirements属性添加到服务类型,其RequirementsMode设置为“Allowed”或“Required”。
当我尝试访问wcf服务时,我收到此错误:原因是HttpContext.Current为null,在这种情况下我该怎么办?有什么帮助吗?
未将对象引用设置为对象的实例。
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
答案 0 :(得分:0)
HttpContext
是ASP.Net构造。如果您希望能够在服务中访问它,则需要为您的服务启用ASP.Net Compatibility。
通过web.config:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
或以声明的方式:
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
public class MyService : IMyService { ... }
答案 1 :(得分:0)
如果简单的响应写入适合您,那么请考虑使用简单的HttpHandler(通过实现IHttpHandler接口)。 Response对象不应在WCF服务中使用...
如果WCF适合你(也许它提供的技术堆栈是你需要的东西),那么考虑使用已经存在的plumming来输出json:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.**Json**)]
String DoStuff();
}