![](/img/trans.png)
[英]Wcf service (ajax enabled) passed XML child object is always null
[英]The value of the object in the request always null in wcf service
我有一個WCF服務和客戶端網站來測試服務。 WCF服務未獲取對象的值。 我搜索了網頁並修改了我的代碼。 但是我還沒有解決它。 有人會幫助我嗎? 提前致謝。
有我的服務:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "BookInfo/")]
BookingResult Booking(BookInfo bookInfo);
public BookingResult Booking(BookInfo bookInfo)
{
BookingResult result = new BookingResult();
if (bookInfo.Name == null)
{
result.isSucceed = false;
}
else
{
result.isSucceed = true;
}
return result;
}
我網站上有一種方法可以調用該服務。
using Booking; //this is WCF service reference
private string callService(BookInfo input)
{
string serviceUrl = "http://localhost:1599026/Booking.svc/BookInfo/";
string stringPayload = "{\"bookInfo\":[" +JsonConvert.SerializeObject(input) +"]}";
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
string rtn = client.UploadString(serviceUrl,"POST", stringPayload);
return rtn;
}
您從哪里獲得錯誤信息,請與我分享? 您是否正確配置並發布了WCF Web模式服務? 我復制你的代碼並在IIS上托管web模式服務,最后我成功訪問了該方法。 我建議您可以公開默認的GetData()方法,然后測試它是否可以正確獲取返回的結果,然后我們測試強類型方法。
這是我的演示,希望它對您有用。
IService1.cs
public interface IService1
{
[OperationContract]
[WebGet]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "BookInfo/")]
BookingResult Booking(BookInfo bookInfo);
}
Service1.svc.cs
public class Service1 : IService1
{
public BookingResult Booking(BookInfo bookInfo)
{
BookingResult result = new BookingResult();
if (bookInfo==null)
{
result.isSucceed = false;
}
else
{
result.isSucceed = true;
}
return result;
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
[DataContract]
public class BookInfo
{
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class BookingResult
{
[DataMember]
public bool isSucceed { get; set; }
}
Webconfig
<system.serviceModel>
<services>
<service name="WcfService4.Service1" behaviorConfiguration="svbehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfService4.IService1" behaviorConfiguration="webbehavior">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="svbehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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 name="webbehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
結果。
如果您有任何疑問,請隨時告訴我。
您發送給服務的有效負載存在問題。 更改
string stringPayload = "{\\"bookInfo\\":[" +JsonConvert.SerializeObject(input) +"]}";
至
string stringPayload = "{\\"bookInfo\\":" +JsonConvert.SerializeObject(input) +"}";
(注意不需要在[]中包裝JSON值,並且您的WebClient
請求將起作用,並且您的服務將具有參數的非null值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.