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://[local] host/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/ と入力するとエラーが発生する理由を教えてください。また、ローカル ホストでホストされているサービスをデバッグする方法は?

よろしく、 アシフ・ハメド

4

2 に答える 2

13

デバッグする前に、dll と pdb を IIS ディレクトリに配置する必要があります。次に、VS で [debug] -> [Attach to process.] をクリックします。[Show process from all users] と [Show process in all sessions] を確認すると、使用可能なプロセスのリストに W3WP プロセスが表示されます。W3WP を選択し、[接続] をクリックします。VS アタッチのスクリーンショット

これで、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

于 2013-04-04T14:14:47.503 に答える