【问题标题】:debugging asp.net WCF service hosted in IIS调试 IIS 中托管的 asp.net WCF 服务
【发布时间】:2013-04-04 11:29:15
【问题描述】:

我使用以下模板创建了 WCF 服务:

http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd

这个服务有这样一个方法:

[ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]    
    public class RecordingCompleted
    {

        [WebInvoke(UriTemplate = "", Method = "POST")]
        public string ProcessCall(string JsonData)
        {

        }

    }

我已经在 IIS 上托管了这个服务,方法的 url 是:

http://[本地] 主机/Notifications/RecordingCompleted/

当我在地址栏中输入这个地址时,我得到:

Method not allowed. Please see the service help page for constructing valid requests to the service.

我正在尝试使用以下代码使用此 Web 服务:

string serviceBaseUrl = "http://[local] host/Notifications/RecordingCompleted/";

        string resourceUrl = "";
        string method = "POST";
        //string jsonText = "}";
        UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, jsonText);

方法如下:

  private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
        {
            string responseMessage = null;
            var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
            if (request != null)
            {
                request.ContentType = "application/json";
                request.Method = method;                 
            }

            //var objContent = HttpContentExtensions.CreateDataContract(requestBody);
            if (method == "POST" && requestBody != null)
            {
                byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);
                request.ContentLength = requestBodyBytes.Length;
                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);

            }

            if (request != null)
            {
                var response = request.GetResponse() as HttpWebResponse;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        var reader = new StreamReader(responseStream);

                        responseMessage = reader.ReadToEnd();
                    }
                }
                else
                {
                    responseMessage = response.StatusDescription;
                }
            }
            return responseMessage;
        }



 private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
    {
        byte[] bytes = null;
        var serializer1 = new DataContractJsonSerializer(typeof(string));
        var ms1 = new MemoryStream();
        serializer1.WriteObject(ms1, requestBody);
        ms1.Position = 0;
        var reader = new StreamReader(ms1);
        bytes = ms1.ToArray();
        return bytes;
    }

我正在尝试调试服务方法,但我不知道该怎么做。请告诉我为什么我在输入时出现错误 地址栏中的 http://[local] host/Notifications/RecordingCompleted/。以及如何调试托管在本地主机上的服务?

问候, 阿西夫·哈米德

【问题讨论】:

  • 你为什么用http://[local] host/而不是http://localhost/?尝试修复你的 serviceBaseUrl。
  • 那只是因为在 Stackoverflow 中我们无法输入有问题的 locahost。

标签: asp.net wcf iis iis-7


【解决方案1】:

在调试之前,您必须将 dll 和 pdb 部署到 IIS 目录。接下来,在您的VS中单击调试->附加到进程。“确保检查所有用户的显示进程”和“在所有会话中显示进程”您现在应该在可用进程列表中看到W3WP进程。选择W3WP并点击附加。

您现在应该可以调试 WCF 服务了。更多调试技巧请参考以下博客

http://dhawalk.blogspot.com/2007/07/debugging-tips-in-c.html http://anilsharmadhanbad.blogspot.com/2009/07/debugging-wcf-service-hosted-in-local.html

【讨论】:

    【解决方案2】:

    尝试浏览到 WCF 的 URL 时出现“方法不允许”错误可能有多种原因:

    1) ASP.NET 版本未通过 IIS 正确配置(为适当版本的 .NET 重新运行 aspnet_regiis.exe 应用程序)

    2) .svc 文件未映射到使用 aspnet_isapi.dll(再次,重新运行 aspnet_regiis.exe)

    3) IIS 是在 WCF 框架之后安装的:对于这个,你需要运行这个应用程序,其中“xx”当然是版本: "%WINDIR%\Microsoft.Net\Framework\xx\Windows Communication Foundation\ServiceModelReg.exe" -r

    查看this article 以进一步了解 IIS/ASP.NET 配置问题...

    但是,我发现“方法不允许”的通常原因是由于我在 web.config 中犯的一个错误:绑定信息中的拼写错误一样简单会导致它。查看您的 web.config 并确保 URL 是正确的。

    如果您已经用尽了所有这些途径,但仍然遇到“方法不允许”,请尝试Service Trace Viewer Tool,它可以提供有关问题所在的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 2020-04-26
      相关资源
      最近更新 更多