.NET Webservice不再返回JSON数据

时间:2009-11-19 14:05:34

标签: .net json web-services

上个月,我一直在忙着创建一个包含多个函数的.NET Web服务和一个用html和Javascript编写的网站,它充当用户界面并使用JSON调用webservice方法。 Web服务返回了JSON响应,它确实运行良好。然后我专注于web服务来完成所有功能,并且没有打开网站大约3周。

今天我完成了网络服务,并决定继续在网站上工作。但是当我打开它时,我发现JSON通信不再起作用了......

该网站仍然发送JSON请求(使用网络嗅探器检查)但Web服务不再响应,因此它必须是Web服务端的问题。

我试图整整一天找到问题,但找不到任何东西.. 这里有一些可能有用的代码片段

网络服务标题:

 [WebService(Namespace = "http://meijberg.topicus.local/zdfmobiel")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [ScriptService]
 [System.ComponentModel.ToolboxItem(false)]

 public class ZDFMobielWebservice : WebService {

Web.Config文件

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    <sectionGroup name="declaratieverwerking">
      <section name="db" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <section name="msmq" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </sectionGroup>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
    </sectionGroup>
    </configSections>



  <declaratieverwerking>
    <db>
      <add key="datasource" value="CARTMAN"/>
      <add key="database" value="DV_TRUNK"/>
      <add key="user" value="****"/>
      <add key="password" value="****"/>
      <add key="applicationName" value="DeclaratieVerwerking.WebApplication"/>
    </db>
    <msmq>
      <add key="DeclaratieRegelHA304Queue" value=".\Private$\DV_DeclaratieRegelHA304Queue"/>
      <add key="DeclaratieRegelAP304Queue" value=".\Private$\DV_DeclaratieRegelAP304Queue"/>
      <add key="DeclaratieRegelZH308Queue" value=".\Private$\DV_DeclaratieRegelZH308Queue"/>
      <add key="DeclaratieBerichtHA304Queue" value=".\Private$\DV_DeclaratieBerichtHA304Queue"/>
      <add key="DeclaratieBerichtAP304Queue" value=".\Private$\DV_DeclaratieBerichtAP304Queue"/>
      <add key="DeclaratieBerichtZH308Queue" value=".\Private$\DV_DeclaratieBerichtZH308Queue"/>
      <add key="ErrorQueue" value=".\Private$\DV_ErrorQueue"/>
    </msmq>
  </declaratieverwerking>


  <appSettings>
    <add key="RecreateHibernateMapping" value="True"/>
    <add key="UseUpdateSchema" value="True"/>
  </appSettings>

  <connectionStrings/>

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxEndpointBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="ZDFMobiel.Webservice.ZDFMobielWebservice">
        <endpoint adress=""
          behaviorConfiguration="AspNetAjaxBehavior"
          binding="webHttpBinding"
          contract="ZDFMobiel.Webservice.ZDFMobielWebservice" />
      </service>
    </services>
  </system.serviceModel>

  <spring>
    <context>
      <resource uri="~/Config/Spring.config"/>
    </context>
    <parsers>
      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>
    </parsers>
  </spring>


  <system.web>

    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>    

        <compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            </assemblies>
        </compilation>

        <authentication mode="Windows"/>

        <pages>
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
      <namespaces>
        <add namespace="Declaratieverwerking.WebApplication.Beheer.Controllers"/>
        <add namespace="Declaratieverwerking.WebApplication.Workspace.Controllers"/>
      </namespaces>
        </pages>
        <httpHandlers>     
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        </httpHandlers>
        <httpModules>
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate"/>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>
        <trust level="Full"/>
        <identity impersonate="true" password="*******" userName="Administrator"/>
    </system.web>

    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>
    <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </handlers>
        <httpRedirect enabled="false" destination="http://www.google.nl"/>
    </system.webServer>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
        <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
                <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
            </dependentAssembly> 

      <dependentAssembly>
        <assemblyIdentity name="Spring.Core" publicKeyToken="65E474D141E25E07" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.20214" newVersion="1.3.0.20214"/>
      </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

编辑:一些调用webservice的客户端javascript代码:

        var xhrGegevens = new XMLHttpRequest();
        xhrGegevens.open("POST", "http://meijberg.topicus.local/ZDFMobielWebservice.asmx/getGegevensVerzekerde", true);
        xhrGegevens.onreadystatechange = function(){

            if (xhrGegevens.readyState === 4) {

                var result = eval('(' + xhrGegevens.responseText + ')');

                //Functionality
            }

        }
        xhrGegevens.setRequestHeader("content-type", "application/json");
        var postData = '{"bsn": "999999999"}';
        xhrGegevens.send(postData);

编辑:webservice中的getGevensVerzekerde方法:

[WebMethod]
public VerzekerdeGegevens getGegevensVerzekerde(string bsn)
{
    ZorgVerzekerde verzekerde = Dao.GetVerzekerde(Convert.ToInt32(bsn));
    VerzekerdeGegevens result = new VerzekerdeGegevens();

    result.bsn = verzekerde.Bsn;
    result.geboortedatum = verzekerde.Geboortedatum.Value.ToShortDateString();
    result.geslacht = Convert.ToString(verzekerde.Geslacht);
    result.huisnummer = verzekerde.Huisnummer;
    result.naam = verzekerde.Naam;
    result.postcode = verzekerde.Postcode;
    result.voorletters = verzekerde.Voorletters;

    int i = 0;

    foreach(ZorgPolisInstantie polisInstantie in verzekerde.PolisInstanties)
    {
        PolisGegevens gegevens = new PolisGegevens();

        gegevens.begindatum = polisInstantie.Begindatum.ToShortDateString();
        gegevens.einddatum = polisInstantie.Einddatum.Value.ToShortDateString();
        if(polisInstantie.Type == (ZorgPolisInstantie.ZorgPolisType) 1)
        {
            gegevens.eigenrisico = polisInstantie.EigenRisico;
        }
        else { gegevens.eigenrisico = 0; }

        gegevens.polisnummer = Convert.ToString(polisInstantie.PolisNummer);
        gegevens.type = (int) polisInstantie.Type;
        result.polisgegevens.Add(gegevens);     
    }
    return result;
}

有谁知道问题可能是什么?

2 个答案:

答案 0 :(得分:1)

差不多2天后我发现了问题.... Webservice机器上的病毒扫描程序(AVG Anti Virus)阻止了我对Web服务的所有JSON请求.... ARGGHHHHH !!!

凯夫,感谢您的努力:)

答案 1 :(得分:0)

您是否缺少Web服务方法中的以下属性?:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

当您使用相对URL时会发生什么,例如:

xhrGegevens.open("POST", "ZDFMobielWebservice.asmx/getGegevensVerzekerde", true);