gpt4 book ai didi

c# - 请求中对象的值在wcf服务中始终为null

转载 作者:行者123 更新时间:2023-11-30 12:20:19 25 4
gpt4 key购买 nike

我有一个 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 网络模式服务?我复制了你的代码并在IIS上托管了web模式的服务,终于成功访问了该方法。我建议你可以暴露默认的GetData()方法,然后测试它是否能正确获取返回结果,然后我们测试强类型方法。

这是我的demo,希望对你有用。

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; }

网络配置

<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" />

结果。

获取方法。 GetData Method

预订方法。 Booking Method

如果您有任何问题,请随时告诉我。

关于c# - 请求中对象的值在wcf服务中始终为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52518426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com