在IIS中托管的ASP.NET WCF服务调试

6

我使用以下模板创建了一个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.

我将尝试使用以下代码调用此网络服务:

字符串 serviceBaseUrl = "http://[local] host/Notifications/RecordingCompleted/";


请注意,本文中的 HTML 标签已保留。
        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://[本地]host/Notifications/RecordingCompleted/时会出现错误。以及如何调试托管在本地主机上的服务?

谢谢, Asif Hameed


为什么你在使用 http://[local] host/ 而不是 http://localhost/?尝试修复你的 serviceBaseUrl。 - Alex G.P.
这是因为在Stackoverflow中我们不能在问题中输入localhost。 - DotnetSparrow
2个回答

13

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

现在您应该能够调试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


1
当尝试浏览WCF的URL时出现“方法不允许”错误可能有多种原因:
1)ASP.NET版本与IIS配置不正确(重新运行适当版本的aspnet_regiis.exe应用程序)
2).svc文件未映射为使用aspnet_isapi.dll(再次运行aspnet_regiis.exe)
3)在安装WCF框架之后安装了IIS:对于这个问题,您需要运行此应用程序,其中“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,它可以提供更多关于出错原因的信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接