无法使用json请求调用服务

时间:2013-11-12 11:49:50

标签: c# asp.net json wcf

我是WCF的新手并编写了我的第一个WCF应用程序。 我无法从json请求中调用服务

以下是项目/解决方案的详细信息

我的解决方案共有3个项目 Solution Description 1.服务库项目(MyFirstRESTfulService) 2.主机应用程序(WCFService2) 3.服务客户端(ServiceClient)

1。 MyFirstRESTfulService

Class IEmployeeService.cs

namespace MyFirstRESTfulService
{
    [ServiceContract()]
    public interface IEmployeeService
    {
        [WebGet(UriTemplate = "Employee", ResponseFormat=WebMessageFormat.Json )]
        [OperationContract]
        List<Employee> GetAllEmployeeDetails();
    }
}

Class EmployeeService.cs

namespace MyFirstRESTfulService
{
    [AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed )]
   public  class EmployeeService: IEmployeeService 
    {

       public List <Employee> GetAllEmployeeDetails()
        {
            return EmployeeData.Instance.EmployeeList;
        }
    }
}

2。 WCFService2

Service.svc

的Web.config

    <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MyFirstRESTfulService.EmployeeService">
        <endpoint address="http://localhost:8046/WCFService2/Service.svc" behaviorConfiguration="Web" binding="webHttpBinding" contract="MyFirstRESTfulService.IEmployeeService">
          <identity>
            <dns value="localhost" />
          </identity>

        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="Web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

第3。 ServiceClient

Default.aspx的

<body>
    <script src="Script/jquery1.9.0.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery.support.cors = true;
        function RefreshPage() {
            var serviceUrl = "http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/GetAllEmployeeDetails";
            debugger;
            $.ajax({
                type: "GET",
                url: serviceUrl,
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    var itemRow = "<table>";
                    $.each(data, function (index, item) {
                        itemRow += "<tr><td>" + item.EmpId + "</td><td>" + item.Fname + "</td></tr>";
                    });
                    itemRow += "</table>";

                    $("#divItems").html(itemRow);

                },
                error: ServiceFailed
            });
        }
        </script>
    </script>
    <form id="form1" runat="server">
    <input type="button" onclick="RefreshPage()" name="btnRefesh" value="Refresh" />
    <div id="divItems">
    </div>
    </form>
</body>

但这些东西根本不起作用。点击按钮时没有任何事情发生。你能告诉我哪里出错了。

我收到以下错误

    <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Endpoint not found.</p>
    </div>
  </body>
</html>

先谢谢

1 个答案:

答案 0 :(得分:2)

如果您希望能够通过HTTP与POX或JSON进行通信,则需要使用webHttpBinding而不是wsHttpBinding。

您的配置应该类似于

<services> 
     <service behaviorConfiguration="Default" name="Example.MyService"> 
       <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="Example.IServiceContract" /> 
          <host> 
              <baseAddresses> 
                  <add baseAddress="http://localhost:9000/MYService" /> 
              </baseAddresses> 
          </host> 
        </service> 
    </services> 
    <behaviors> 
        <endpointBehaviors> 
            <behavior name="webBehavior"> 
                <webHttp /> 
            </behavior> 
        </endpointBehaviors> 

还使用“RequestFormat = WebMessageFormat.Json”&amp;注释您的操作合同。 “ResponseFormat = WebMessageFormat.Json”

希望这有帮助,