我正在创建WCF服务,并且在我将其连接到WinForms客户端时工作正常。但我想通过浏览器访问它。有些方法工作正常,但是当我尝试发回包含多个对象的对象时,它会返回错误消息"重置连接" ...我试图测试我的服务ARC和JQuery Ajax,以及我收到同样错误的所有情况。
我差点忘了告诉你,只有在我创建一个Period属性的实例时才会出现这个问题。如果我将对象清除,我就不会有任何问题。
我与您分享了我根据原始代码进行的测试。我希望你能帮助我。
WCF服务
using MercSoft.Conservatorio.DataModels;
using MercSoft.Conservatorio.Request;
using MercSoft.Conservatorio.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace Mercsoft.Conserv.WSv2
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Conservatorio
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
[OperationContract(Name = "EstaActivo"), WebGet]
public bool isActive()
{
return false;
}
[OperationContract(Name="PruebaOro"), WebInvoke(Method="POST", ResponseFormat= WebMessageFormat.Json)]
public PreRegisterResponse pruebaOro(PreRegisterRequest request)
{
PreRegisterResponse response = new PreRegisterResponse();
response.Period = new ModulesDataModel();
return response;
}
// Add more operations here and mark them with [OperationContract]
}
}
PreRegister Response
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using MercSoft.Conservatorio.DataModels;
using System.Runtime.Serialization;
using MercSoft.Conservatorio.Request;
namespace MercSoft.Conservatorio.Response
{
[DataContract]
public class PreRegisterResponse : BaseResponse
{
[DataMember]
public PreRegisterDataModel Period { get; set; }
public PreRegisterResponse()
: base()
{
}
}
}
PreRegisterDataModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace MercSoft.Conservatorio.DataModels
{
[DataContract(IsReference=true)]
public class SystemPeriodsDataModel
{
[DataMember]
public int Id { get; set; }
[DataMember]
public int PeriodType { get; set; }
[DataMember]
public String PeriodTypeString { get; set; }
[DataMember]
public DateTime? StartDate { get; set; }
[DataMember]
public DateTime? EndDate { get; set; }
[DataMember]
public String DatePeriod { get; set; }
[DataMember]
public bool WithInstument { get; set; } //Pre-register ONLY
[DataMember]
public bool Active { get; set; }
}
}
WebConfig
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="Mercsoft.Conserv.WSv2.Conservatorio">
<endpoint address="" behaviorConfiguration="Mercsoft.Conserv.WSv2.ConservatorioAspNetAjaxBehavior"
binding="webHttpBinding" contract="Mercsoft.Conserv.WSv2.Conservatorio" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Mercsoft.Conserv.WSv2.ConservatorioAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
答案 0 :(得分:0)
我刚刚在我正在使用的WCF服务中发现了一段相同的问题,即(现在)使用webHttpBinding(对于REST客户端)。罪魁祸首是财产&#34; IsReference = true&#34;在DataContract类中。我需要将该标志设置为TRUE,因为对象图包含循环引用;这样返回的响应比真实对象序列化的字节少得多。
我不知道为什么这适用于SOAP和其他绑定但不适用于webHttpBinding,但由于我需要添加REST支持,因此当返回一个IsReference = true的DataContract类时,服务停止工作。
我的临时解决方案是创建另一个具有与原始属性相同的属性的类,但没有IsReference属性。我需要进一步调查,但与此同时,我希望这会有所帮助。