1

iこれは私が作成したサービスです

[ServiceContract]
public interface IRestWithXML
{
    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "DoWork", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string DoWork(Test objtest);

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "Method?test={strtest}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string Method(Test objtest,string strtest);

}          

[DataContract]
public class Test
{
    [DataMember]
    public string id { get; set; }
}

私が使用したwebconfigファイル。

   <service behaviorConfiguration="WcfService1.RestWithXMLBehavior"
    name="WcfService1.RestWithXML">
    <endpoint address="" behaviorConfiguration="JSONEndpointBehavior"
      binding="webHttpBinding" contract="WcfService1.IRestWithXML" />
    <endpoint address="ws" binding="wsHttpBinding" contract="WcfService1.IRestWithXML">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/" />
      </baseAddresses>
    </host>
  </service>
  <behaviors>
  <endpointBehaviors>
    <behavior name="JSONEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
      <behavior name="WcfService1.RestWithXMLBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

以下は、WCFサービスを呼び出すために使用されるコードです。メソッドが許可されていないというエラーが発生しました

       HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/Client/RestWithXML.svc/DoWork");
        request.Method = "POST";
        request.ContentType = "application/json";
        string postData = "{\"id\":\"1234\"}";

        // Set postData to byte type and set content length
        byte[] postBytes = System.Text.UTF8Encoding.UTF8.GetBytes(postData);
        request.ContentLength = postBytes.Length;

        // Write postBytes to request stream
        Stream s = request.GetRequestStream();
        s.Write(postBytes, 0, postBytes.Length);
        s.Close();

        // Get the reponse
        WebResponse response = request.GetResponse();

        // Status for debugging
        string ResponseStatus = (((HttpWebResponse)response).StatusDescription);

        // Get the content from server and read it from the stream
        s = response.GetResponseStream();
        StreamReader reader = new StreamReader(s);
        string responseFromServer = reader.ReadToEnd();

        // Clean up and close
        reader.Close();
        s.Close();
        response.Close();

メソッドが許可されていないというエラーが発生しました。このエラーにつながる私はしません

前もって感謝します

4

0 に答える 0