有我的第一次尝试在实现RESTful WCF服务,但不能得到它后我的对象:(这崩溃的clientstuff代码(见下文)。这可能是什么?修复感谢
部分的web.config
<system.serviceModel>
<services>
<service name="MyRest.Service1" behaviorConfiguration="ServBehave">
<!--Endpoint for REST-->
<endpoint address="XMLService" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="MyRest.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServBehave">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<!--Behavior for the REST endpoint for Help enability-->
<behavior name="restPoxBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
客户代码:
public string ClientStuff()
{
var ServiceUrl = "http://localhost/MyRest/Service1.svc/XMLService/";
var empserializer = new DataContractSerializer(typeof(MyRest.Employee));
var url = new Uri(ServiceUrl + "PostEmp");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/XML";
var emp = new MyRest.Employee { DeptName = "sale", EmpName = "ted", EmpNo = 11112 };
using (var requeststream = request.GetRequestStream())
{
empserializer.WriteObject(requeststream, emp);
}
var response = (HttpWebResponse)request.GetResponse();// crashes here with error in title
var statuscode = response.StatusCode;
return statuscode.ToString();
}
service1.svc.cs
public bool PostEmp(Employee employee)
{
//something
return true;
}
服务合同
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/PostEmp", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool PostEmp(Employee employee);
// TODO: Add your service operations here
}