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服务的代码。我开始使用Method not allowed错误
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();
我得到方法不允许错误。我不知道导致这个错误
先谢谢