【发布时间】:2011-07-22 09:06:44
【问题描述】:
情景。我将“启用 AJAX 的 WCF 服务”添加到我的 ASP.NET 应用程序(网络表单)。这会导致以下结果(长命名空间被截断):
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
}
我改变 [ServiceContract(命名空间 = "")] 到 [ServiceContract(Namespace = "http://domain.com/services")]
在我的页面上添加:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference path="~/Services/MyService.svc" />
</Services>
</asp:ScriptManagerProxy>
我还添加了一个 javascript 函数,以便我可以调用该服务:
函数调用DoWork(){ domain.com.services.MyService.DoWork(); }
该网站使用 Windows 身份验证运行。在我运行页面的时候,生成了jsdebug文件,找到了,domain.com.services.MyService.DoWork();工作没有问题。 FireBug 证实了这一点。所以我的下一步是创建一个接口并让服务类实现它。所以现在我有:
[ServiceContract(Namespace = "http://domain.com/services")]
public interface IMyService
{
[OperationContract]
void DoWork();
}
和
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 公共类 MyService : IMyService { 公共无效 DoWork() { // 在此处添加您的操作实现 返回; } }
并将 web.config 更改为 contract="Namespace.IMyService"
现在,我在 jsdebug 文件中收到 401 错误。有人见过这个吗?
感谢您的帮助。
【问题讨论】:
标签: wcf windows-authentication webforms