将 WCF 服务更改为使用接口时,jsdebug 文件上出现 401 错误

发布于 2024-10-26 11:33:49 字数 2034 浏览 5 评论 0原文

设想。我将“支持 AJAX 的 WCF 服务”添加到我的 ASP.NET 应用程序(Web 表单)中。这会产生以下结果(长命名空间被截断):

[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 函数,以便我可以调用该服务:

function callDoWork(){ 域名.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 错误。有人见过这个吗?

感谢您的帮助。

Scenario. I add an "AJAX-enabled WCF Service" to my ASP.NET application (webforms). This results in the following (long namespaces truncated):

[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]
}

I change
[ServiceContract(Namespace = "")]
to
[ServiceContract(Namespace = "http://domain.com/services")]

To my page I add:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <Services>
        <asp:ServiceReference path="~/Services/MyService.svc" />
    </Services>
</asp:ScriptManagerProxy>

I also add a javascript function so I can call the service:

function callDoWork(){
domain.com.services.MyService.DoWork();
}

The website is run with Windows authentication. When I run the page, the jsdebug file is generated, found, and domain.com.services.MyService.DoWork(); works with no problem. This is confirmed with FireBug. So my next step is to create an interface and have the service class implement it. So now I have:

[ServiceContract(Namespace = "http://domain.com/services")]
public interface IMyService
{
    [OperationContract]
    void DoWork();
}

and

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public void DoWork()
{
// Add your operation implementation here
return;
}
}

and change the web.config to say contract="Namespace.IMyService"

Now, I get a 401 error on the jsdebug file. Anyone ever seen this?

Thanks for your help.

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

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

发布评论

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

评论(1

帅哥哥的热头脑 2024-11-02 11:33:49

这是一个转移注意力的事情。无论出于何种原因,服务上也有一个 mex 端点会导致这种情况发生。我关闭 mex 端点,401 就消失了。这并不是一个解决方案,但至少我克服了我的错误。

This was a red herring. For whatever reason, also having a mex endpoint on the service caused this to occur. I take the mex endpoint off, and the 401 goes away. Not that this is a solution, but at least I get past my error.

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