WCF JSON数据未在浏览器中显示

时间:2013-08-05 13:53:53

标签: c# json wcf

尝试了很多但是我不知道我的WCF服务有什么问题。

我想要实现的目标: 我正在构建一个WCF,它将一些数据作为HTTP上的json结构公开

我会在一个andorid应用程序中使用它来显示数据。

模型:

1。)界面:

namespace WcfSyncDBService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISyncDBService" in both code and config file together.
    [ServiceContract]
    public interface ISyncDBService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
                          ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetTodoItems")]
        TodoItem[] GetTodoItems();
    }

    [DataContract(Name = "TodoItems")]
    public class TodoItem
    {
        [DataMember(Name = "Id")]
        public int Id { get; set;}
        [DataMember(Name = "Category")]
        public string Category { get; set; }
        [DataMember(Name = "Summary")]
        public string Summary { get; set; }
        [DataMember(Name = "Description")]
        public string Description { get; set; }
    }
}

2。)服务:

namespace WcfSyncDBService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SyncDBService" in code, svc and config file together.
    public class SyncDBService : ISyncDBService
    {

        public TodoItem[] GetTodoItems()
        {
            var context = new SyncDBEntities();
            var query = from i in context.todo select i;
            var itemList = query.ToList();

            List<TodoItem> todoList = new List<TodoItem>();

            foreach (var item in itemList)
            {
                TodoItem i = new TodoItem
                                 {
                                     Id =  item.C_id,
                                     Category = item.category,
                                     Summary = item.summary,
                                     Description = item.description
                                 };
                todoList.Add(i);
            }
            return todoList.ToArray();
        }
    }
}

3.。)Web配置:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
        <service name="WcfSyncDBService.SyncDBService">
            <!-- Service Endpoints -->
            <!-- Unless fully qualified, address is relative to base address supplied above -->
            <endpoint address="" binding="webHttpBinding" contract="WcfSyncDBService.ISyncDBService" behaviorConfiguration="web" />
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
          <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="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="true"/>
          </behavior>
      </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
    <connectionStrings>
        <add name="SyncDBEntities" connectionString="metadata=res://*/SyncDBmodel.csdl|res://*/SyncDBmodel.ssdl|res://*/SyncDBmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=SyncDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>  
</configuration>

当我运行服务时,我得到了定义和wsdl:

http://localhost:18131/SyncDBService.svc

但是当我尝试调用函数http://localhost:18131/SyncDBService.svc/GetTodoItems/时,我收到错误“找不到端点。”

我知道错误可能在web.config中,但我发现它无法找到希望有人可以帮助我。

EDIT1 :(在Siva sugesstion之后的web.config)

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
        <service name="WcfSyncDBService.SyncDBService">
            <!-- Service Endpoints -->
            <!-- Unless fully qualified, address is relative to base address supplied above -->
            <endpoint address="" binding="webHttpBinding" contract="WcfSyncDBService.ISyncDBService" behaviorConfiguration="web" />
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
          <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="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="true"/>
          </behavior>
      </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
        <serviceActivations>
            <add relativeAddress="SyncDBService.svc" service="WcfSyncDBService.SyncDBService" />
        </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
    <connectionStrings>
        <add name="SyncDBEntities" connectionString="metadata=res://*/SyncDBmodel.csdl|res://*/SyncDBmodel.ssdl|res://*/SyncDBmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=SyncDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>  
</configuration>

1 个答案:

答案 0 :(得分:0)

您需要在服务主机上配置相对地址,

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
        <serviceActivations>
            <add relativeAddress="SyncDBService.svc" service="WcfSyncDBService.SyncDBService" />
        </serviceActivations>
    </serviceHostingEnvironment>