gpt4 book ai didi

wcf - REST WCF 服务返回 XML 响应但不返回 JSON 响应

转载 作者:行者123 更新时间:2023-12-04 18:26:56 25 4
gpt4 key购买 nike

我是 WCF 的新手,所以我认为这是非常基础的。我有一个返回单个“订单”对象的简单方法。使用默认 XML 时它工作得很好,但是,当我应用

ResponseFormat = WebMessageFormat.Json

属性,返回JSON失败。代码成功执行并到达返回行,但随后该方法立即再次调用,最后第三次,然后浏览器返回错误,指出与本地主机的连接已中断

当我删除 ResponseFormat = WebMessageFormat.Json 时,将调用该方法并返回 XML。不确定我是否缺少 JSON。

IProductSales.cs

namespace ProductsSalesService
{
[ServiceContract(Name = "ProductsSales")]
public interface IProductsSales
{

[OperationContract]
[WebGet(UriTemplate = "Orders/{orderID}", ResponseFormat = WebMessageFormat.Json)]
[Description("Returns the details of an order")]
SalesOrderHeader GetOrder(string orderID);

}
}

产品销售

public SalesOrderHeader GetOrder(string orderID)
{
SalesOrderHeader header = null;

try
{
int id = Convert.ToInt32(orderID);
AdventureWorksEntities database = new AdventureWorksEntities();

header = (from order in database.SalesOrderHeaders
where order.SalesOrderID == id
select order).FirstOrDefault();

}
catch
{
throw new WebFaultException(HttpStatusCode.BadRequest);
}

return header;
}

我正在研究 WCF 书中的示例,所以他们让我构建一个小型控制台应用程序作为主机,所以这是我为主机客户端准备的 app.config 文件。

<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="AdventureWorksEntities" connectionString="metadata=res://*/ProductsSalesModel.csdl|res://*/ProductsSalesModel.ssdl|res://*/ProductsSalesModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=BINGBONG;Initial Catalog=AdventureWorks;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><system.serviceModel>
<services>
<service name="ProductsSalesService.ProductsSales">
<endpoint address="http://localhost:8000/Sales" binding="webHttpBinding"
bindingConfiguration="" name="ProductsSalesService.ProductsSales"
contract="ProductsSalesService.IProductsSales" />
</service>
</services>
</system.serviceModel>
</configuration>

最后,这只是主机客户端代码。

public class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(ProductsSalesService.ProductsSales));
host.Open();
Console.WriteLine("Service running");
Console.WriteLine("Press ENTER to stop the service");
Console.ReadLine();
host.Close();
}
}

因此,当我转到 http://localhost:8000/Sales/Orders/43659 提取我的订单时,它点击了 3 次,页面在 Chrome 中取消并出现以下错误:

This webpage is not available The connection to localhost was interrupted. Here are some suggestions: Reload this webpage later. Check your Internet connection. Restart any router, modem, or other network devices you may be using. Add Google Chrome as a permitted program in your firewall's or antivirus software's settings. If it is already a permitted program, try deleting it from the list of permitted programs and adding it again. If you use a proxy server, check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server, adjust your proxy settings: Go to the wrench menu > Settings > Show advanced settings... > Change proxy settings...

LAN Settings and deselect the "Use a proxy server for your LAN" checkbox. Error 101 (net::ERR_CONNECTION_RESET): The connection was reset.

如果我删除 WebMessageFormat.Json 一切正常!

感谢您的帮助!

最佳答案

对于初学者,请尝试 WCF 跟踪/日志记录,看看它是否能说明问题。

将其放入服务器的配置文件中(<configuration> 元素中的某处):-

<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Error" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\server.svclog"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\Temp\server_messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

然后将其放入 <system.serviceModel> 中元素:-

<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>

尝试再次访问您的服务并检查这(希望)生成的 .svclog 文件以寻找线索。这些文件将在“服务跟踪查看器”工具中打开 - 如果没有,可以从 MS 下载(我认为是 Win SDK 的一部分)。

关于wcf - REST WCF 服务返回 XML 响应但不返回 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016228/

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