0

正在尝试很多并浏览,但我无法弄清楚我的 WCF 服务有什么问题。

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

我最终会在一个安卓应用程序中使用它来显示数据。

样机:

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.) 网络配置:

<?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 的建议之后的 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>
4

1 回答 1

0

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

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
        <serviceActivations>
            <add relativeAddress="SyncDBService.svc" service="WcfSyncDBService.SyncDBService" />
        </serviceActivations>
    </serviceHostingEnvironment>
于 2013-08-05T14:05:13.500 回答