Response.使用 JQuery 和 WCF 编写 JSONP

发布于 2024-10-03 14:07:41 字数 684 浏览 0 评论 0原文

更新:

该服务无法激活,因为它不支持 ASP.NET 兼容性。此应用程序启用了 ASP.NET 兼容性。在 web.config 中关闭 ASP.NET 兼容模式,或将 AspNetCompatibilityRequirements 属性添加到服务类型,并将 requestsMode 设置为“允许”或“必需”。

当我尝试访问 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-10-10 14:07:41

HttpContext 是一个 ASP.Net 构造。如果您希望能够在服务中访问它,则需要启用 ASP您的服务的.Net 兼容性

通过 web.config:

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

或以声明方式:

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

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 { ... }
花开半夏魅人心 2024-10-10 14:07:41

如果简单的响应写入适合您,那么请考虑使用简单的 HttpHandler(通过实现 IHttpHandler 接口)。 Response 对象并不意味着在 WCF 服务中使用...

如果 WCF 适合您(也许它提供的技术堆栈是您需要的),那么请考虑使用已有的 Plumming 来输出 json:

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

}

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();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文