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
}