【问题标题】:Unable to invoke method in .asmx file无法调用 .asmx 文件中的方法
【发布时间】:2020-01-07 11:33:40
【问题描述】:

我在下面创建了 asmx 文件:

namespace Webhook
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class WebhookEvent : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void ReceiveEvents()
        {
            string fileName = @"D:\myfile.txt";

            try
            {
                //HttpContext.Current.Response.Write("{property: value}");

                // Check if file already exists. If yes, delete it.     
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                // Create a new file     
                using (FileStream fs = File.Create(fileName))
                {
                    // Add some text to file    
                    Byte[] title = new UTF8Encoding(true).GetBytes("New Text File \n");
                    fs.Write(title, 0, title.Length);
                    /// byte[] author = new UTF8Encoding(true).GetBytes(jsonString);
                    // fs.Write(author, 0, author.Length);
                }

                // Open the stream and read it back.    
                using (StreamReader sr = File.OpenText(fileName))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
            }
        }


    }
}

在这个文件中,我在文本文件中写入简单的文本,并在 d 驱动器中写入。

现在另一个应用程序在此文件 (http://myip/Webhook/WebhookEvent.asmx/ReceiveEvents) 上发送数据(通过钩子),详细信息如下:

Content-Type: application/json
User-Agent: weebdly
Accept-Encoding: gzip
X-weebdly-Hook-Id: 123456

Body :
{
  "todayoff":"invitee"
}

因此,当另一个应用程序在我的应用程序上发送数据时,它不会调用此方法。

我还在 IIS 中启用了调试模式。所以我可以检查断点,但它不是调用这个方法。

我该如何解决这个问题?

【问题讨论】:

  • 您是否收到任何错误消息?

标签: c# asp.net web-services asp.net-web-api


【解决方案1】:

这里是解决方案: 我只需要添加“[ScriptService]”

原来是这样的:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]

使用 ScriptService 我可以获取数据。

【讨论】:

    【解决方案2】:

    您是否将以下内容添加到您的 web.config 文件中?

    <configuration>
      <system.web>
        <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
          </protocols>
        </webServices>
      </system.web>
     </configuration>
    

    要启用远程 Web 服务测试页面,只需在 web.config 的 webServices 部分添加 HttpPost/HttpGet 协议。默认情况下,该选项是禁用的,以增加实时环境的安全性,但您可以在 web.config 中启用它,如下所示,以调试已​​部署的 Web 服务。

    【讨论】:

    • 你收到 404 响应了吗?
    • 没有。我无法检查响应,因为其他应用程序不在我的控制范围内
    • 你能在本地调用方法吗?
    • 是的,我可以在本地调用。
    • 已解决。检查我的答案
    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 2013-08-24
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多